Completed
Push — develop ( f3c189...e92436 )
by Alejandro
08:55
created

SendMailPlugin::applyArgsToMailService()   F

Complexity

Conditions 10
Paths 288

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 10

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 44
ccs 33
cts 33
cp 1
rs 3.1304
cc 10
eloc 25
nc 288
nop 1
crap 10

How to fix   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
    ];
35
36 7
    public function __construct(MailServiceInterface $mailService)
37
    {
38 7
        $this->mailService = $mailService;
39 7
    }
40
41
    /**
42
     * If no arguments are provided, the mail service is returned.
43
     * If any argument is provided, they will be used to configure the MailService and send an email.
44
     * The result object will be returned in that case
45
     *
46
     * @param null|string|ViewModel|array $bodyOrConfig
47
     * @param null|string $subject
48
     * @param null|array $to
49
     * @param null|string|array $from
50
     * @param null|array $cc
51
     * @param null|array $bcc
52
     * @param null|array $attachments
53
     * @return MailServiceInterface|ResultInterface
54
     */
55 5
    public function __invoke(
56
        $bodyOrConfig = null,
0 ignored issues
show
Unused Code introduced by
The parameter $bodyOrConfig is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
        $subject = null,
0 ignored issues
show
Unused Code introduced by
The parameter $subject is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
        $to = null,
0 ignored issues
show
Unused Code introduced by
The parameter $to is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
        $from = null,
0 ignored issues
show
Unused Code introduced by
The parameter $from is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
        $cc = null,
0 ignored issues
show
Unused Code introduced by
The parameter $cc is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
        $bcc = null,
0 ignored issues
show
Unused Code introduced by
The parameter $bcc is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
        $attachments = null
0 ignored issues
show
Unused Code introduced by
The parameter $attachments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    ) {
64 5
        $args = func_get_args();
65 5
        if (empty($args)) {
66 1
            return $this->mailService;
67
        }
68
69 4
        $args = $this->normalizeMailArgs($args);
70 4
        $this->applyArgsToMailService($args);
71 4
        return $this->mailService->send();
72
    }
73
74
    /**
75
     * Normalizes the arguments passed when invoking this plugin so that they can be treated in a consistent way
76
     *
77
     * @param array $args
78
     * @return array
79
     */
80 4
    protected function normalizeMailArgs(array $args)
81
    {
82
        // If the first argument is an array, use it as the mail configuration
83 4
        if (is_array($args[0])) {
84 1
            return $args[0];
85
        }
86
87 3
        $result = [];
88 3
        $length = count($args);
89
        // FIXME This is a weak way to handle the arguments, since a change in the order will break it
90 3
        for ($i = 0; $i < $length; $i++) {
91 3
            $result[$this->argumentsMapping[$i]] = $args[$i];
92 3
        }
93
94 3
        return $result;
95
    }
96
97
    /**
98
     * Applies the arguments provided while invoking this plugin to the MailService,
99
     * discarding any previous configuration
100
     *
101
     * @param array $args
102
     */
103 4
    protected function applyArgsToMailService(array $args)
104
    {
105 4
        if (isset($args['body'])) {
106 4
            $body = $args['body'];
107
108 4
            if (is_string($body)) {
109 3
                $this->mailService->setBody($body);
110 3
            } else {
111 1
                $this->mailService->setTemplate($body);
112
            }
113 4
        }
114
115 4
        if (isset($args['subject'])) {
116 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...
117 3
        }
118
119 4
        if (isset($args['to'])) {
120 2
            $this->mailService->getMessage()->setTo($args['to']);
121 2
        }
122
123 4
        if (isset($args['from'])) {
124 2
            $from = $args['from'];
125
126 2
            if (is_array($from)) {
127 1
                $fromAddress = array_keys($from);
128 1
                $fromName = array_values($from);
129 1
                $this->mailService->getMessage()->setFrom($fromAddress[0], $fromName[0]);
130 1
            } else {
131 1
                $this->mailService->getMessage()->setFrom($from);
132
            }
133 2
        }
134
135 4
        if (isset($args['cc'])) {
136 1
            $this->mailService->getMessage()->setCc($args['cc']);
137 1
        }
138
139 4
        if (isset($args['bcc'])) {
140 1
            $this->mailService->getMessage()->setBcc($args['bcc']);
141 1
        }
142
143 4
        if (isset($args['attachments'])) {
144 1
            $this->mailService->setAttachments($args['attachments']);
145 1
        }
146 4
    }
147
148
    /**
149
     * @param MailServiceInterface $mailService
150
     * @return $this
151
     */
152 1
    public function setMailService(MailServiceInterface $mailService)
153
    {
154 1
        $this->mailService = $mailService;
155 1
        return $this;
156
    }
157
158
    /**
159
     * @return MailServiceInterface
160
     */
161 1
    public function getMailService()
162
    {
163 1
        return $this->mailService;
164
    }
165
}
166