Completed
Pull Request — master (#132)
by
unknown
03:54
created

SendMailPlugin::applyArgsToMailService()   F

Complexity

Conditions 12
Paths 864

Size

Total Lines 56
Code Lines 33

Duplication

Lines 22
Ratio 39.29 %

Code Coverage

Tests 34
CRAP Score 12.9955

Importance

Changes 0
Metric Value
dl 22
loc 56
ccs 34
cts 42
cp 0.8095
rs 3.5714
c 0
b 0
f 0
cc 12
eloc 33
nc 864
nop 1
crap 12.9955

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
namespace AcMailer\Controller\Plugin;
3
4
use AcMailer\Result\ResultInterface;
5
use AcMailer\Service\MailServiceAwareInterface;
6
use AcMailer\Service\MailServiceInterface;
7
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
8
use Zend\View\Model\ViewModel;
9
10
/**
11
 * Class SendMailPlugin
12
 * @author Alejandro Celaya Alastrué
13
 * @link http://www.alejandrocelaya.com
14
 */
15
class SendMailPlugin extends AbstractPlugin implements MailServiceAwareInterface
16
{
17
    /**
18
     * @var MailServiceInterface
19
     */
20
    protected $mailService;
21
22
    /**
23
     * The list of possible arguments in the order they should be provided
24
     * @var array
25
     */
26
    private $argumentsMapping = [
27
        0 => 'body',
28
        1 => 'subject',
29
        2 => 'to',
30
        3 => 'from',
31
        4 => 'cc',
32
        5 => 'bcc',
33
        6 => 'attachments',
34
        7 => 'replyTo',
35
    ];
36
37 7
    public function __construct(MailServiceInterface $mailService)
38
    {
39 7
        $this->mailService = $mailService;
40 7
    }
41
42
    /**
43
     * If no arguments are provided, the mail service is returned.
44
     * If any argument is provided, they will be used to configure the MailService and send an email.
45
     * The result object will be returned in that case
46
     *
47
     * @param null|string|ViewModel|array $bodyOrConfig
48
     * @param null|string $subject
49
     * @param null|array $to
50
     * @param null|string|array $from
51
     * @param null|array $cc
52
     * @param null|array $bcc
53
     * @param null|array $attachments
54
     * @param null|array $replyTo
55
     * @return MailServiceInterface|ResultInterface
56
     */
57 5
    public function __invoke(
58
        $bodyOrConfig = null,
59
        $subject = null,
60
        $to = null,
61
        $from = null,
62
        $cc = null,
63
        $bcc = null,
64
        $attachments = null,
65
        $replyTo = null
66
    ) {
67 5
        $args = func_get_args();
68 5
        if (empty($args)) {
69 1
            return $this->mailService;
70
        }
71
72 4
        $args = $this->normalizeMailArgs($args);
73 4
        $this->applyArgsToMailService($args);
74 4
        return $this->mailService->send();
75
    }
76
77
    /**
78
     * Normalizes the arguments passed when invoking this plugin so that they can be treated in a consistent way
79
     *
80
     * @param array $args
81
     * @return array
82
     */
83 4
    protected function normalizeMailArgs(array $args)
84
    {
85
        // If the first argument is an array, use it as the mail configuration
86 4
        if (is_array($args[0])) {
87 1
            return $args[0];
88
        }
89
90 3
        $result = [];
91 3
        $length = count($args);
92
        // FIXME This is a weak way to handle the arguments, since a change in the order will break it
93 3
        for ($i = 0; $i < $length; $i++) {
94 3
            $result[$this->argumentsMapping[$i]] = $args[$i];
95 3
        }
96
97 3
        return $result;
98
    }
99
100
    /**
101
     * Applies the arguments provided while invoking this plugin to the MailService,
102
     * discarding any previous configuration
103
     *
104
     * @param array $args
105
     */
106 4
    protected function applyArgsToMailService(array $args)
107
    {
108 4
        if (isset($args['body'])) {
109 4
            $body = $args['body'];
110
111 4
            if (is_string($body)) {
112 3
                $this->mailService->setBody($body);
113 3
            } else {
114 1
                $this->mailService->setTemplate($body);
115
            }
116 4
        }
117
118 4
        if (isset($args['subject'])) {
119 3
            $this->mailService->setSubject($args['subject']);
0 ignored issues
show
Deprecated Code introduced by
The method AcMailer\Service\MailSer...Interface::setSubject() has been deprecated with message: Use $mailService->getMessage()->setSubject() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
120 3
        }
121
122 4
        if (isset($args['to'])) {
123 2
            $this->mailService->getMessage()->setTo($args['to']);
124 2
        }
125
126 4 View Code Duplication
        if (isset($args['from'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
127 2
            $from = $args['from'];
128
129 2
            if (is_array($from)) {
130 1
                $fromAddress = array_keys($from);
131 1
                $fromName = array_values($from);
132 1
                $this->mailService->getMessage()->setFrom($fromAddress[0], $fromName[0]);
133 1
            } else {
134 1
                $this->mailService->getMessage()->setFrom($from);
135
            }
136 2
        }
137
138 4
        if (isset($args['cc'])) {
139 1
            $this->mailService->getMessage()->setCc($args['cc']);
140 1
        }
141
142 4
        if (isset($args['bcc'])) {
143 1
            $this->mailService->getMessage()->setBcc($args['bcc']);
144 1
        }
145
146 4
        if (isset($args['attachments'])) {
147 1
            $this->mailService->setAttachments($args['attachments']);
148 1
        }
149
        
150 4 View Code Duplication
        if (isset($args['replyTo'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
            $replyTo = $args['replyTo'];
152
153
            if (is_array($replyTo)) {
154
                $replyToAddress = array_keys($replyTo);
155
                $replyToName = array_values($replyTo);
156
                $this->mailService->getMessage()->setReplyTo($replyToAddress[0], $replyToName[0]);
157
            } else {
158
                $this->mailService->getMessage()->setReplyTo($replyTo);
159
            }
160
        }
161 4
    }
162
163
    /**
164
     * @param MailServiceInterface $mailService
165
     * @return $this
166
     */
167 1
    public function setMailService(MailServiceInterface $mailService)
168
    {
169 1
        $this->mailService = $mailService;
170 1
        return $this;
171
    }
172
173
    /**
174
     * @return MailServiceInterface
175
     */
176 1
    public function getMailService()
177
    {
178 1
        return $this->mailService;
179
    }
180
}
181