Completed
Pull Request — master (#132)
by
unknown
31:24 queued 26:50
created

SendMailPlugin::applyArrayArgs()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 6
cts 6
cp 1
rs 7.551
c 0
b 0
f 0
cc 7
eloc 15
nc 7
nop 2
crap 7
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
        // 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
111 1
            if (is_string($body)) {
112
                $this->mailService->setBody($body);
113
            } else {
114
                $this->mailService->setTemplate($body);
115 4
            }
116 3
        }
117
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
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
        if (isset($args['bcc'])) {
131 1
            $this->mailService->getMessage()->setBcc($args['bcc']);
132
        }
133
134
        if (isset($args['attachments'])) {
135 4
            $this->mailService->setAttachments($args['attachments']);
136 1
        }
137
        
138
        $this->applyArrayArgs($args, 'from');
139 4
        $this->applyArrayArgs($args, 'replyTo');
140 1
    }
141
    
142
     /**
143 4
     * @param array $args
144 1
     * @param string $key
145
     */
146 4
    protected function applyArrayArgs(array $args, $key)
147
    {
148
        if (isset($args[$key])) {
149
            $arg = $args[$key];
150
            if (is_array($arg)) {
151
                $argKey = array_keys($arg);
152 1
                $argValue = array_values($arg);
153
                if ($key === 'from') {
154 1
                    $this->mailService->getMessage()->setFrom($argKey[0], $argValue[0]);
155 1
                } elseif ($key === 'replyTo') {
156
                    $this->mailService->getMessage()->setReplyto($argKey[0], $argValue[0]);
157
                }
158
            } else {
159
                if ($key === 'from') {
160
                    $this->mailService->getMessage()->setFrom($arg);
161 1
                } elseif ($key === 'replyTo') {
162
                    $this->mailService->getMessage()->setReplyTo($arg);
163 1
                }
164
            }
165
        }
166
    }
167
168
    /**
169
     * @param MailServiceInterface $mailService
170
     * @return $this
171
     */
172
    public function setMailService(MailServiceInterface $mailService)
173
    {
174
        $this->mailService = $mailService;
175
        return $this;
176
    }
177
178
    /**
179
     * @return MailServiceInterface
180
     */
181
    public function getMailService()
182
    {
183
        return $this->mailService;
184
    }
185
}
186