DansMaCulotte /
laravel-mail-template
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DansMaCulotte\MailTemplate\Drivers; |
||
| 4 | |||
| 5 | use DansMaCulotte\MailTemplate\Exceptions\InvalidConfiguration; |
||
| 6 | use DansMaCulotte\MailTemplate\Exceptions\SendError; |
||
| 7 | use Mailgun\Exception\HttpClientException; |
||
| 8 | use Mailgun\Mailgun; |
||
| 9 | |||
| 10 | class MailgunDriver implements Driver |
||
| 11 | { |
||
| 12 | |||
| 13 | /** @var Mailgun|null */ |
||
| 14 | public $client = null; |
||
| 15 | |||
| 16 | /** @var array */ |
||
| 17 | public $message = []; |
||
| 18 | |||
| 19 | /** @var string */ |
||
| 20 | public $domain; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Driver constructor. |
||
| 24 | * @param array $config |
||
| 25 | * @throws InvalidConfiguration |
||
| 26 | */ |
||
| 27 | public function __construct(array $config) |
||
| 28 | { |
||
| 29 | if (!isset($config['key'])) { |
||
| 30 | throw InvalidConfiguration::invalidCredential('mailgun', 'key'); |
||
| 31 | } |
||
| 32 | |||
| 33 | if (!isset($config['domain'])) { |
||
| 34 | throw InvalidConfiguration::invalidCredential('mailgun', 'domain'); |
||
| 35 | } |
||
| 36 | |||
| 37 | $this->client = Mailgun::create($config['key']); |
||
| 38 | $this->domain = $config['domain']; |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @return Driver |
||
| 43 | */ |
||
| 44 | public function make(): Driver |
||
| 45 | { |
||
| 46 | $this->message = []; |
||
| 47 | |||
| 48 | return $this; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string $name |
||
| 53 | * @param string $email |
||
| 54 | * @return Driver |
||
| 55 | */ |
||
| 56 | public function setFrom(string $name, string $email): Driver |
||
| 57 | { |
||
| 58 | $this->message['from'] = "${name} <${email}>"; |
||
| 59 | |||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $template |
||
| 65 | * @return Driver |
||
| 66 | */ |
||
| 67 | public function setTemplate($template): Driver |
||
| 68 | { |
||
| 69 | $this->message['template'] = $template; |
||
| 70 | |||
| 71 | return $this; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $subject |
||
| 76 | * @return Driver |
||
| 77 | */ |
||
| 78 | public function setSubject(string $subject): Driver |
||
| 79 | { |
||
| 80 | $this->message['subject'] = $subject; |
||
| 81 | |||
| 82 | return $this; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $name |
||
| 87 | * @param string $email |
||
| 88 | * @return Driver |
||
| 89 | */ |
||
| 90 | public function setRecipient(string $name, string $email): Driver |
||
| 91 | { |
||
| 92 | $this->message['to'] = "${name} <${email}>"; |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $name |
||
| 99 | * @param string $email |
||
| 100 | * @return Driver |
||
| 101 | */ |
||
| 102 | public function setBcc(string $name, string $email): Driver |
||
| 103 | { |
||
| 104 | $this->message['bcc'] = "${name} <${email}>"; |
||
| 105 | |||
| 106 | return $this; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param array $variables |
||
| 111 | * @return Driver |
||
| 112 | */ |
||
| 113 | public function setVariables(array $variables): Driver |
||
| 114 | { |
||
| 115 | $this->message['h:X-Mailgun-Variables'] = json_encode($variables); |
||
| 116 | |||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $language |
||
| 122 | * @return Driver |
||
| 123 | */ |
||
| 124 | public function setLanguage(string $language): Driver |
||
| 125 | { |
||
| 126 | return $this; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @return array |
||
| 131 | * @throws SendError |
||
| 132 | */ |
||
| 133 | public function send(): array |
||
| 134 | { |
||
| 135 | try { |
||
| 136 | $response = $this->client->messages()->send( |
||
|
0 ignored issues
–
show
|
|||
| 137 | $this->domain, |
||
| 138 | $this->message |
||
| 139 | ); |
||
| 140 | |||
| 141 | return [ |
||
| 142 | 'id' => $response->getId(), |
||
| 143 | 'message' => $response->getMessage(), |
||
| 144 | ]; |
||
| 145 | } catch (HttpClientException $exception) { |
||
| 146 | throw SendError::responseError('mailgun', 0, $exception); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function toArray(): array |
||
| 154 | { |
||
| 155 | return [ |
||
| 156 | 'body' => [ |
||
| 157 | 'Messages' => [ |
||
| 158 | $this->message, |
||
| 159 | ], |
||
| 160 | ], |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | } |
||
| 164 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.