1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Rizart Dokollari <[email protected]> |
4
|
|
|
* @since 6/3/16 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace ElasticEmail\V2\Requests\Email; |
8
|
|
|
|
9
|
|
|
use ElasticEmail\V2\Requests\BaseRequest; |
10
|
|
|
use ElasticEmail\V2\Requests\RequestInterface; |
11
|
|
|
use ElasticEmail\V2\Responses\Email\EmailResponse; |
12
|
|
|
|
13
|
|
|
class EmailRequest extends BaseRequest implements RequestInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param array $emailData to, subject, from keys required. |
17
|
|
|
* @return EmailResponse |
18
|
|
|
*/ |
19
|
|
|
public function send(array $emailData) |
20
|
|
|
{ |
21
|
|
|
$this->handlerRequestValidation($emailData); |
22
|
|
|
|
23
|
|
|
$guzzleResponse = $this->getHttpClient()->request('POST', 'email/send', [ |
24
|
|
|
'form_params' => array_merge($this->config, $emailData), |
25
|
|
|
]); |
26
|
|
|
|
27
|
|
|
return new EmailResponse($guzzleResponse); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function handlerRequestValidation($emailData) |
31
|
|
|
{ |
32
|
|
|
if (!array_key_exists('to', $emailData)) { |
33
|
|
|
throw new RequestException("At least one recipient must be specified. Array key: 'to'"); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (!filter_var($emailData['to'], FILTER_VALIDATE_EMAIL)) { |
37
|
|
|
throw new RequestException('Invalid recipient email.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!array_key_exists('subject', $emailData)) { |
41
|
|
|
throw new RequestException('Subject field must be specified.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (!array_key_exists('from', $emailData)) { |
45
|
|
|
throw new RequestException('Invalid FROM email address.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getRecipientEmail() |
52
|
|
|
{ |
53
|
|
|
return '[email protected]'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getSenderEmail() |
57
|
|
|
{ |
58
|
|
|
return '[email protected]'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getSubjectEmail() |
62
|
|
|
{ |
63
|
|
|
return 'Elastic Email Subject'; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.