Test Failed
Pull Request — master (#12)
by nicolas
03:42
created

MailjetSendApiTransport::send()   F

Complexity

Conditions 12
Paths 385

Size

Total Lines 66

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 12.0016

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 3.7984
c 0
b 0
f 0
ccs 43
cts 44
cp 0.9773
cc 12
nc 385
nop 2
crap 12.0016

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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