Skip to content

Commit

Permalink
CATL-1613: Refactor CRM_Utils_Mail_CaseMail class - add method to get…
Browse files Browse the repository at this point in the history
… email subject patterns
  • Loading branch information
i-grou committed Aug 20, 2020
1 parent 4141643 commit ed3e70a
Showing 1 changed file with 82 additions and 2 deletions.
84 changes: 82 additions & 2 deletions CRM/Utils/Mail/CaseMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,42 @@
*/
class CRM_Utils_Mail_CaseMail {

/**
* A word that is used for cases by default (in email subject).
*
* @var string
*/
private $caseLabel = 'case';

/**
* Default cases related email subject regexp patterns.
*
* All emails related to cases have case hash/id in the subject, e.g:
* [case #ab12efg] Magic moment
* [case #1234] Magic is here
* This variable is defined in constructor.
*
* @var array|string[]
*/
private $subjectPatterns = [];

/**
* Cases related email subject regexp patterns extended by hooks.
*
* @var array|string[]
*/
private $subjectPatternsHooked = [];

/**
* CRM_Utils_Mail_CaseMail constructor.
*/
public function __construct() {
$this->subjectPatterns = [
'/\[' . $this->caseLabel . ' #([0-9a-h]{7})\]/i',
'/\[' . $this->caseLabel . ' #(\d+)\]/i',
];
}

/**
* Checks if email is related to cases.
*
Expand All @@ -31,10 +67,54 @@ class CRM_Utils_Mail_CaseMail {
*/
public function isCaseEmail ($subject) {
$subject = trim($subject);
$res = preg_match('/\[case #([0-9a-h]{7})\]/', $subject) === 1
|| preg_match('/\[case #(\d+)\]/', $subject) === 1;
$patterns = $this->getSubjectPatterns();
$res = FALSE;

for ($i = 0; !$res && $i < count($patterns); $i++) {
$res = preg_match($patterns[$i], $subject) === 1;
}

return $res;
}

/**
* Returns cases related email subject patterns.
*
* These patterns could be used to check if email is related to cases.
*
* @return array|string[]
*/
public function getSubjectPatterns() {
return !empty($this->subjectPatternsHooked)
? $this->subjectPatternsHooked
: $this->subjectPatterns;
}

/**
* Returns value of some class property.
*
* @param string $name
* Property name.
*
* @return mixed|null
* Property value or null if property does not exist.
*/
public function get($name) {
return $this->{$name} ?? NULL;
}

/**
* Sets value of some class property.
*
* @param string $name
* Property name.
* @param mixed $value
* New property value.
*/
public function set($name, $value) {
if (isset($this->{$name})) {
$this->{$name} = $value;
}
}

}

0 comments on commit ed3e70a

Please sign in to comment.