MandrillAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Mail\Adapter;
6
7
use Linio\Component\Mail\AdapterInterface;
8
use Linio\Component\Mail\Contact;
9
use Linio\Component\Mail\Message;
10
11
class MandrillAdapter implements AdapterInterface
12
{
13
    /**
14
     * @var \Mandrill
15
     */
16
    protected $mandrill;
17
18
    public function __construct(array $config = [])
19
    {
20
        $this->mandrill = new \Mandrill($config['api_key']);
21
    }
22
23
    public function send(Message $message): void
24
    {
25
        $result = $this->mandrill->messages->sendTemplate($message->getTemplate(), null, $this->prepareMessage($message))[0];
0 ignored issues
show
Bug introduced by
The property messages does not seem to exist in Mandrill.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
26
27
        if (isset($result['status']) && in_array($result['status'], ['queued', 'sent'])) {
28
            return;
29
        }
30
31
        throw new \RuntimeException(sprintf('Mandrill could not send the email to "%s" due to: %s', $result['email'], $result['reject_reason']));
32
    }
33
34
    protected function prepareData(array $data): array
35
    {
36
        $result = [];
37
38
        foreach ($data as $key => $value) {
39
            $result[] = [
40
                'name' => $key,
41
                'content' => $value,
42
            ];
43
        }
44
45
        return $result;
46
    }
47
48
    protected function prepareMessage(Message $message): array
49
    {
50
        return [
51
            'subject' => $message->getSubject(),
52
            'from_email' => $message->getFrom()->getEmail(),
53
            'from_name' => $message->getFrom()->getName(),
54
            'to' => $this->getDestinations($message),
55
            'track_opens' => true,
56
            'track_clicks' => true,
57
            'preserve_recipients' => true,
58
            'global_merge_vars' => $this->prepareData($message->getData()),
59
        ];
60
    }
61
62
    protected function getDestinations(Message $message): array
63
    {
64
        $preparedTo = $this->prepareContacts($message->getTo(), 'to');
65
        $preparedBcc = $this->prepareContacts($message->getBcc(), 'bcc');
66
67
        return array_merge($preparedTo, $preparedBcc);
68
    }
69
70
    /**
71
     * @param Contact[] $contacts
72
     */
73
    protected function prepareContacts(array $contacts, string $type): array
74
    {
75
        $result = [];
76
77
        foreach ($contacts as $contact) {
78
            $result[] = [
79
                'name' => $contact->getName(),
80
                'email' => $contact->getEmail(),
81
                'type' => $type,
82
            ];
83
        }
84
85
        return $result;
86
    }
87
88
    public function getMandrill(): \Mandrill
89
    {
90
        return $this->mandrill;
91
    }
92
93
    public function setMandrill(\Mandrill $mandrill): void
94
    {
95
        $this->mandrill = $mandrill;
96
    }
97
}
98