1
|
|
|
<?php namespace RuleCom\Notifier\Channels; |
2
|
|
|
|
3
|
|
|
use GuzzleHttp\Client; |
4
|
|
|
|
5
|
|
|
class Email implements Channel |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Client |
9
|
|
|
*/ |
10
|
|
|
private $guzzle; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $apiKey; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $subject = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $from = ['name' => '', 'email' => '']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $to = ['name' => '', 'email' => '']; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $content = ['html' => '', 'plain' => '']; |
36
|
|
|
|
37
|
|
|
public function __construct(Client $guzzle) |
38
|
|
|
{ |
39
|
|
|
$this->guzzle = $guzzle; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $apiKey |
44
|
|
|
* @return $this |
45
|
|
|
*/ |
46
|
|
|
public function apiKey($apiKey) |
47
|
|
|
{ |
48
|
|
|
$this->apiKey = $apiKey; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $subject |
54
|
|
|
* @return $this |
55
|
|
|
*/ |
56
|
|
|
public function subject($subject) |
57
|
|
|
{ |
58
|
|
|
$this->subject = $subject; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $from |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function from(array $from) |
67
|
|
|
{ |
68
|
|
|
$this->from = $from; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $to |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function to(array $to) |
77
|
|
|
{ |
78
|
|
|
$this->to = $to; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $content |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function content($content) |
87
|
|
|
{ |
88
|
|
|
$this->content = $content; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Dispatches notification message to Rule |
94
|
|
|
*/ |
95
|
|
|
public function dispatch() |
96
|
|
|
{ |
97
|
|
|
foreach ($this->extractRecipients() as $recipient) { |
98
|
|
|
$this->guzzle->post('https://app.rule.io/api/v2/transactionals', [ |
99
|
|
|
'json' => [ |
100
|
|
|
'apikey' => $this->apiKey, |
101
|
|
|
'transaction_type' => 'email', |
102
|
|
|
'transaction_name' => $this->subject, |
103
|
|
|
'subject' => $this->subject, |
104
|
|
|
'from' => $this->from, |
105
|
|
|
'to' => $recipient, |
106
|
|
|
'content' => [ |
107
|
|
|
'html' => base64_encode($this->content['html']), |
108
|
|
|
'plain' => base64_encode($this->content['plain']) |
109
|
|
|
] |
110
|
|
|
] |
111
|
|
|
]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
private function extractRecipients() |
119
|
|
|
{ |
120
|
|
|
if (is_array(reset($this->to))) { |
121
|
|
|
return $this->to; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return [$this->to]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|