|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\MessengerBundle\Channel; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
6
|
|
|
use Yokai\MessengerBundle\Channel\Twilio\Factory\ClientFactoryInterface; |
|
7
|
|
|
use Yokai\MessengerBundle\Delivery; |
|
8
|
|
|
use Yokai\MessengerBundle\Recipient\PhoneRecipientInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Matthieu Crinquand <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class TwilioChannel implements ChannelInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var ClientFactoryInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $twilioClientFactory; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $defaults; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param ClientFactoryInterface $twilioClientFactory |
|
27
|
|
|
* @param array $defaults |
|
28
|
|
|
*/ |
|
29
|
16 |
|
public function __construct( |
|
30
|
|
|
ClientFactoryInterface $twilioClientFactory, |
|
31
|
|
|
array $defaults |
|
32
|
|
|
) { |
|
33
|
16 |
|
$this->twilioClientFactory = $twilioClientFactory; |
|
34
|
16 |
|
$this->defaults = $defaults; |
|
35
|
16 |
|
} |
|
36
|
|
|
|
|
37
|
5 |
|
public function supports($recipient) |
|
38
|
|
|
{ |
|
39
|
5 |
|
if (is_object($recipient)) { |
|
40
|
4 |
|
return $recipient instanceof PhoneRecipientInterface; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
if (is_string($recipient)) { |
|
44
|
1 |
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function configure(OptionsResolver $resolver) |
|
51
|
|
|
{ |
|
52
|
|
|
$resolver |
|
53
|
1 |
|
->setRequired(['from']) |
|
54
|
1 |
|
->setRequired(['api_id']) |
|
55
|
1 |
|
->setRequired(['api_token']) |
|
56
|
|
|
; |
|
57
|
|
|
|
|
58
|
1 |
|
foreach ($resolver->getDefinedOptions() as $option) { |
|
59
|
1 |
|
if (isset($this->defaults[$option])) { |
|
60
|
1 |
|
$resolver->setDefault($option, $this->defaults[$option]); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
10 |
|
public function handle(Delivery $delivery) |
|
66
|
|
|
{ |
|
67
|
10 |
|
$recipient = $delivery->getRecipient(); |
|
68
|
10 |
|
$options = $delivery->getOptions(); |
|
69
|
|
|
|
|
70
|
10 |
|
$client = $this->twilioClientFactory->createClient($options['api_id'], $options['api_token']); |
|
71
|
|
|
|
|
72
|
10 |
|
$phone = $recipient instanceof PhoneRecipientInterface ? $recipient->getPhone() : $recipient; |
|
73
|
|
|
|
|
74
|
10 |
|
$client->messages->create($phone, [ |
|
75
|
10 |
|
'from' => $options['from'], |
|
76
|
10 |
|
'body' => $delivery->getBody(), |
|
77
|
|
|
]); |
|
78
|
2 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|