Completed
Push — develop ( 8dce02...a5156e )
by Alejandro
13:02
created

SendMailPlugin::applyArrayArgs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
ccs 5
cts 5
cp 1
cc 3
eloc 10
nc 3
nop 2
crap 3
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 7
37
    public function __construct(MailServiceInterface $mailService)
38 7
    {
39 7
        $this->mailService = $mailService;
40
    }
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 5
     * @return MailServiceInterface|ResultInterface
56
     */
57
    public function __invoke(
58
        $bodyOrConfig = null,
59
        $subject = null,
60
        $to = null,
61
        $from = null,
62
        $cc = null,
63
        $bcc = null,
64 5
        $attachments = null,
65 5
        $replyTo = null
66 1
    ) {
67
        $args = func_get_args();
68
        if (empty($args)) {
69 4
            return $this->mailService;
70 4
        }
71 4
72
        $args = $this->normalizeMailArgs($args);
73
        $this->applyArgsToMailService($args);
74
        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 4
     * @param array $args
81
     * @return array
82
     */
83 4
    protected function normalizeMailArgs(array $args)
84 1
    {
85
        // If the first argument is an array, use it as the mail configuration
86
        if (is_array($args[0])) {
87 3
            return $args[0];
88 3
        }
89
90 3
        $result = [];
91 3
        $length = count($args);
92 3
        // FIXME This is a weak way to handle the arguments, since a change in the order will break it
93
        for ($i = 0; $i < $length; $i++) {
94 3
            $result[$this->argumentsMapping[$i]] = $args[$i];
95
        }
96
97
        return $result;
98
    }
99
100
    /**
101
     * Applies the arguments provided while invoking this plugin to the MailService,
102
     * discarding any previous configuration
103 4
     *
104
     * @param array $args
105 4
     */
106 4
    protected function applyArgsToMailService(array $args)
107
    {
108 4
        if (isset($args['body'])) {
109 3
            $body = $args['body'];
110 3
111 1
            if (is_string($body)) {
112
                $this->mailService->setBody($body);
113 4
            } else {
114
                $this->mailService->setTemplate($body);
115 4
            }
116 3
        }
117 3
118
        if (isset($args['subject'])) {
119 4
            $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 2
        }
121 2
122
        if (isset($args['to'])) {
123 4
            $this->mailService->getMessage()->setTo($args['to']);
124 2
        }
125
126 2
        if (isset($args['cc'])) {
127 1
            $this->mailService->getMessage()->setCc($args['cc']);
128 1
        }
129 1
130 1
        if (isset($args['bcc'])) {
131 1
            $this->mailService->getMessage()->setBcc($args['bcc']);
132
        }
133 2
134
        if (isset($args['attachments'])) {
135 4
            $this->mailService->setAttachments($args['attachments']);
136 1
        }
137 1
        
138
        $this->applyArrayArgs($args, 'from');
139 4
        $this->applyArrayArgs($args, 'replyTo');
140 1
    }
141 1
    
142
     /**
143 4
     * @param array $args
144 1
     * @param string $key
145 1
     */
146 4
    protected function applyArrayArgs(array $args, $key)
147
    {
148
        if (!isset($args[$key])) {
149
            return;
150
        }
151
152 1
        $arg    = $args[$key];
153
        $setter = 'set'.ucfirst($key);
154 1
155 1
        if (is_array($arg)) {
156
            $this->mailService->getMessage()->{$setter}(array_keys($arg)[0],
157
                array_values($arg)[0]);
158
            return;
159
        }
160
161 1
        $this->mailService->getMessage()->{$setter}($arg);
162
    }
163 1
164
    /**
165
     * @param MailServiceInterface $mailService
166
     * @return $this
167
     */
168
    public function setMailService(MailServiceInterface $mailService)
169
    {
170
        $this->mailService = $mailService;
171
        return $this;
172
    }
173
174
    /**
175
     * @return MailServiceInterface
176
     */
177
    public function getMailService()
178
    {
179
        return $this->mailService;
180
    }
181
}
182