Completed
Push — master ( 7e3ecd...7dbba3 )
by dan
02:01
created

NexmoMessageDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Dispatcher;
4
5
use IrishDan\NotificationBundle\Message\MessageInterface;
6
use Nexmo\Client;
7
8
class NexmoMessageDispatcher implements MessageDispatcherInterface
9
{
10
    protected $nexmoConfiguration;
11
    /**
12
     * @var Client $client
13
     */
14
    protected $client;
15
16
    public function __construct(array $nexmoConfiguration)
17
    {
18
        $this->nexmoConfiguration = $nexmoConfiguration;
19
    }
20
21
    public function dispatch(MessageInterface $message)
22
    {
23
        // Get the dispatch and message data from the message.
24
        $dispatchData = $message->getDispatchData();
25
        $messageData = $message->getMessageData();
26
27
        if (empty($this->client)) {
28
            $credentials = new Client\Credentials\Basic(
29
                $this->nexmoConfiguration['api_key'],
30
                $this->nexmoConfiguration['api_secret']
31
            );
32
            $this->client = new Client($credentials);
33
        }
34
35
        $sent = $this->client->message()->send(
36
            [
37
                'to' => $dispatchData['to'],
38
                'from' => $dispatchData['from'],
39
                'text' => $messageData['body'],
40
            ]
41
        );
42
43
        return ! empty($sent);
44
    }
45
}