Passed
Pull Request — master (#7)
by nicolas
02:52
created

MailjetSendApiTransport   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 9
dl 0
loc 135
ccs 62
cts 64
cp 0.9688
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A isStarted() 0 4 1
A start() 0 4 1
A stop() 0 4 1
D send() 0 65 13
A registerPlugin() 0 4 1
1
<?php
2
3
namespace Dekalee\MailjetBundle\Transport;
4
5
use Dekalee\MailjetBundle\Guesser\TemplateIdGuesserManager;
6
use Mailjet\Client;
7
use Mailjet\Resources;
8
use Swift_Events_EventListener;
9
use Swift_Mime_Message;
10
11
/**
12
 * Class MailjetSendApiTransport
13
 */
14
class MailjetSendApiTransport implements \Swift_Transport
15
{
16
    protected $client;
17
    protected $dispatcher;
18
    protected $started = true;
19
    protected $templateIdGuesserManager;
20
21
    /**
22
     * @param Client                        $client
23
     * @param \Swift_Events_EventDispatcher $dispatcher
24
     * @param TemplateIdGuesserManager      $templateIdGuesserManager
25
     */
26 9
    public function __construct(
27
        Client $client,
28
        \Swift_Events_EventDispatcher $dispatcher,
29
        TemplateIdGuesserManager $templateIdGuesserManager
30
    ) {
31 9
        $this->client = $client;
32 9
        $this->dispatcher = $dispatcher;
33 9
        $this->templateIdGuesserManager = $templateIdGuesserManager;
34 9
    }
35
36
    /**
37
     * Test if this Transport mechanism has started.
38
     *
39
     * @return bool
40
     */
41 1
    public function isStarted()
42
    {
43 1
        return $this->started;
44
    }
45
46
    /**
47
     * Start this Transport mechanism.
48
     */
49 1
    public function start()
50
    {
51 1
        $this->started = true;
52 1
    }
53
54
    /**
55
     * Stop this Transport mechanism.
56
     */
57 1
    public function stop()
58
    {
59 1
        $this->started = false;
60 1
    }
61
62
    /**
63
     * Send the given Message.
64
     *
65
     * Recipient/sender data will be retrieved from the Message API.
66
     * The return value is the number of recipients who were accepted for delivery.
67
     *
68
     * @param Swift_Mime_Message $message
69
     * @param string[]           $failedRecipients An array of failures by-reference
70
     *
71
     * @return int
72
     */
73 5
    public function send(Swift_Mime_Message $message, &$failedRecipients = null)
74
    {
75 5
        $failedRecipients = (array) $failedRecipients;
76
77 5
        if ($evt = $this->dispatcher->createSendEvent($this, $message)) {
78 4
            $this->dispatcher->dispatchEvent($evt, 'beforeSendPerformed');
79 4
            if ($evt->bubbleCancelled()) {
80 1
                return 0;
81
            }
82 3
        }
83
84 4
        $from = $message->getFrom();
85 4
        $recipients = [];
86 4
        $headers = [];
87
88 4
        foreach ($message->getTo() as $email => $name) {
89 4
            $recipients[] = ['Email' => $email, 'Name' => $email];
90 4
        }
91
92 4
        foreach ($message->getHeaders()->getAll() as $header) {
93 1
            $headers[$header->getFieldName()] = $header->getFieldBody();
94 4
        }
95
96 4
        if (array_key_exists('Content-Type', $headers) && false !== strpos('multipart', $headers['Content-Type'])) {
97
            unset($headers['Content-Type']);
98
        }
99
100
        $body = [
101 4
            'FromEmail' => key($from),
102 4
            'Subject' => $message->getSubject(),
103 4
            'Vars' => ['content' => $message->getBody()],
104 4
            'Recipients' => $recipients,
105 4
            'MJ-TemplateID' => $this->templateIdGuesserManager->guess($message),
106 4
            'MJ-TemplateLanguage' => 'True',
107 4
            'Headers' => $headers,
108 4
        ];
109
        
110 4
        if (null !== $fromName = current($from)) {
111 1
            $body['FromName'] = $fromName;
112 1
        }
113
114 4
        foreach ($message->getChildren() as $child) {
115 2
            if ($child instanceof \Swift_Attachment) {
116 2
                $body['Attachments'][] = [
117 2
                    'Content-type' => $child->getContentType(),
118 2
                    'Filename' => $child->getFilename(),
119 2
                    'content' => base64_encode($child->getBody())
120 2
                ];
121 2
            }
122 4
        }
123
124 4
        $response = $this->client->post(Resources::$Email, ['body' => $body]);
125 4
        $success = $response->success();
126
127 4
        if ($evt) {
128 3
            $evt->setResult($success ? \Swift_Events_SendEvent::RESULT_SUCCESS : \Swift_Events_SendEvent::RESULT_FAILED);
129 3
            $this->dispatcher->dispatchEvent($evt, 'sendPerformed');
130 3
        }
131
132 4
        if ($success) {
133 2
            return count((array) $message->getTo());
134
        }
135
136 2
        return 0;
137
    }
138
139
    /**
140
     * Register a plugin in the Transport.
141
     *
142
     * @param Swift_Events_EventListener $plugin
143
     */
144 1
    public function registerPlugin(Swift_Events_EventListener $plugin)
145
    {
146 1
        $this->dispatcher->bindEventListener($plugin);
147 1
    }
148
}
149