Completed
Pull Request — master (#132)
by
unknown
43:39
created

SendMailPlugin::applyArgsToMailService()   C

Complexity

Conditions 8
Paths 96

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 19
cts 19
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 19
nc 96
nop 1
crap 8
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
        if (isset($args[$key])) {
148
            $arg = $args[$key];
149
            if (is_array($arg)) {
150
                $argKey = array_keys($arg);
151
                $argValue = array_values($arg);
152 1
                if ($key === 'from') {
153
                    $this->mailService->getMessage()->setFrom($argKey[0], $argValue[0]);
154 1
                } elseif ($key === 'replyTo') {
155 1
                    $this->mailService->getMessage()->setReplyto($argKey[0], $argValue[0]);
156
                }
157
            } else {
158
                if ($key === 'from') {
159
                    $this->mailService->getMessage()->setFrom($arg);
160
                } elseif ($key === 'replyTo') {
161 1
                    $this->mailService->getMessage()->setReplyTo($arg);
162
                }
163 1
            }
164
        }
165
    }
166
167
    /**
168
     * @param MailServiceInterface $mailService
169
     * @return $this
170
     */
171
    public function setMailService(MailServiceInterface $mailService)
172
    {
173
        $this->mailService = $mailService;
174
        return $this;
175
    }
176
177
    /**
178
     * @return MailServiceInterface
179
     */
180
    public function getMailService()
181
    {
182
        return $this->mailService;
183
    }
184
}
185