Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
13 / 13 |
| Recipient | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
13 / 13 |
| __construct | |
100.00% |
1 / 1 |
4 | |
100.00% |
9 / 9 |
|||
| getEmail | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getName | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getHeader | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| __set_state | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Miniframe\Mailer\Model; |
| 4 | |
| 5 | class Recipient |
| 6 | { |
| 7 | /** |
| 8 | * Header constants |
| 9 | */ |
| 10 | public const |
| 11 | HEADER_TO = 1, |
| 12 | HEADER_CC = 2, |
| 13 | HEADER_BCC = 3; |
| 14 | |
| 15 | /** |
| 16 | * Recipient email address. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | private $email; |
| 21 | /** |
| 22 | * Recipient name. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private $name; |
| 27 | /** |
| 28 | * Required mail header |
| 29 | * |
| 30 | * @var int |
| 31 | */ |
| 32 | private $header; |
| 33 | |
| 34 | /** |
| 35 | * Recipient data model |
| 36 | * |
| 37 | * @param string $email Email address of the recipient. |
| 38 | * @param string|null $name Name of the recipient. |
| 39 | * @param integer $header One of the Header constants (self::HEADER_TO, self::HEADER_CC, self::HEADER_BCC). |
| 40 | */ |
| 41 | public function __construct(string $email, string $name = null, int $header = self::HEADER_TO) |
| 42 | { |
| 43 | if (!in_array($header, [self::HEADER_TO, self::HEADER_CC, self::HEADER_BCC])) { |
| 44 | throw new \InvalidArgumentException( |
| 45 | 'The header must be one of the constants HEADER_TO, HEADER_CC or HEADER_BCC' |
| 46 | ); |
| 47 | } |
| 48 | if (!trim($email) || !filter_var(trim($email), FILTER_VALIDATE_EMAIL)) { |
| 49 | throw new \InvalidArgumentException('Invalid email address specified'); |
| 50 | } |
| 51 | |
| 52 | $this->email = trim($email); |
| 53 | $this->name = trim($name ?? ucfirst(explode('@', $email, 2)[0])); |
| 54 | $this->header = $header; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the mail address. |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public function getEmail(): string |
| 63 | { |
| 64 | return $this->email; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the name. |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | public function getName(): string |
| 73 | { |
| 74 | return $this->name; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns the header. |
| 79 | * |
| 80 | * @return integer |
| 81 | */ |
| 82 | public function getHeader(): int |
| 83 | { |
| 84 | return $this->header; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Magic method; sets this model based on a specific state |
| 89 | * |
| 90 | * @param array $properties The actual state. |
| 91 | * |
| 92 | * @return Recipient |
| 93 | * @see var_export() |
| 94 | */ |
| 95 | public static function __set_state(array $properties): self |
| 96 | { |
| 97 | return new self($properties['email'], $properties['name'], $properties['header']); |
| 98 | } |
| 99 | } |