Completed
Push — master ( eeedf4...3b415d )
by Alejandro
02:34
created

src/Options/MailOptions.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace AcMailer\Options;
3
4
use Zend\Mail\Transport\FileOptions;
5
use Zend\Mail\Transport\SmtpOptions;
6
use Zend\Stdlib\AbstractOptions;
7
use Zend\Mail\Transport\TransportInterface;
8
use AcMailer\Exception\InvalidArgumentException;
9
10
/**
11
 * Module options
12
 * @author Alejandro Celaya Alastrué
13
 * @link http://www.alejandrocelaya.com
14
 */
15
class MailOptions extends AbstractOptions
16
{
17
    /**
18
     * Standard adapters aliasses
19
     * @var array
20
     */
21
    private $adapterMap = [
22
        'sendmail'  => ['\Zend\Mail\Transport\Sendmail'],
23
        'smtp'      => ['\Zend\Mail\Transport\Smtp'],
24
        'in_memory' => ['\Zend\Mail\Transport\InMemory', '\Zend\Mail\Transport\Null'],
25
        'file'      => ['\Zend\Mail\Transport\File'],
26
        'null'      => ['\Zend\Mail\Transport\InMemory', '\Zend\Mail\Transport\Null'],
27
    ];
28
    
29
    /**
30
     * @var TransportInterface|string
31
     */
32
    private $mailAdapter = '\Zend\Mail\Transport\Sendmail';
33
    /**
34
     * @var MessageOptions;
35
     */
36
    private $messageOptions;
37
    /**
38
     * @var SmtpOptions;
39
     */
40
    private $smtpOptions;
41
    /**
42
     * @var FileOptions
43
     */
44
    private $fileOptions;
45
    /**
46
     * @var array
47
     */
48
    private $mailListeners = [];
49
    
50
    /**
51
     * @return TransportInterface|string
52
     */
53 16
    public function getMailAdapter()
54
    {
55 16
        return $this->mailAdapter;
56
    }
57
58
    /**
59
     * @param string|TransportInterface $mailAdapter
60
     * @return $this
61
     */
62 8
    public function setMailAdapter($mailAdapter)
63
    {
64
        // Map adapter aliases to the real class name
65 8
        if (is_string($mailAdapter) && array_key_exists(strtolower($mailAdapter), $this->adapterMap)) {
66 3
            $mailAdapter = $this->adapterMap[strtolower($mailAdapter)];
67 3
            foreach ($mailAdapter as $class) {
68 3
                if (class_exists($class)) {
69 3
                    $mailAdapter = $class;
70 3
                    break;
71
                }
72
            }
73
        }
74
75 8
        $this->mailAdapter = $mailAdapter;
76 8
        return $this;
77
    }
78
79
    /**
80
     * Alias for method getMailAdapter
81
     * @return string|TransportInterface
82
     */
83 2
    public function getTransport()
84
    {
85 2
        return $this->getMailAdapter();
86
    }
87
88
    /**
89
     * Alias for method setMailAdapter
90
     * @param string|TransportInterface $transport
91
     * @return $this
92
     */
93 1
    public function setTransport($transport)
94
    {
95 1
        return $this->setMailAdapter($transport);
96
    }
97
98
    /**
99
     * @return MessageOptions
100
     */
101 18
    public function getMessageOptions()
102
    {
103 18
        if (! isset($this->messageOptions)) {
104 10
            $this->setMessageOptions([]);
105
        }
106
107 18
        return $this->messageOptions;
108
    }
109
110
    /**
111
     * @param MessageOptions|array $messageOptions
112
     * @return $this
113
     * @throws \AcMailer\Exception\InvalidArgumentException
114
     */
115 20 View Code Duplication
    public function setMessageOptions($messageOptions)
0 ignored issues
show
This method seems to be duplicated in 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...
116
    {
117 20
        if (is_array($messageOptions)) {
118 19
            $this->messageOptions = new MessageOptions($messageOptions);
119
        } elseif ($messageOptions instanceof MessageOptions) {
120 1
            $this->messageOptions = $messageOptions;
121
        } else {
122 1
            throw new InvalidArgumentException(sprintf(
123 1
                'MessageOptions should be an array or an AcMailer\Options\MessageOptions object. %s provided.',
124 1
                is_object($messageOptions) ? get_class($messageOptions) : gettype($messageOptions)
125
            ));
126
        }
127
128 19
        return $this;
129
    }
130
131
    /**
132
     * @return SmtpOptions
133
     */
134 3
    public function getSmtpOptions()
135
    {
136 3
        if (! isset($this->smtpOptions)) {
137 1
            $this->setSmtpOptions([]);
138
        }
139
140 3
        return $this->smtpOptions;
141
    }
142
143
    /**
144
     * @param SmtpOptions|array $smtpOptions
145
     * @return $this
146
     */
147 4 View Code Duplication
    public function setSmtpOptions($smtpOptions)
0 ignored issues
show
This method seems to be duplicated in 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...
148
    {
149 4
        if (is_array($smtpOptions)) {
150 3
            $this->smtpOptions = new SmtpOptions($smtpOptions);
151
        } elseif ($smtpOptions instanceof SmtpOptions) {
152 1
            $this->smtpOptions = $smtpOptions;
153
        } else {
154 1
            throw new InvalidArgumentException(sprintf(
155 1
                'SmtpOptions should be an array or an Zend\Mail\Transport\SmtpOptions object. %s provided.',
156 1
                is_object($smtpOptions) ? get_class($smtpOptions) : gettype($smtpOptions)
157
            ));
158
        }
159
160 3
        return $this;
161
    }
162
163
    /**
164
     * @return FileOptions
165
     */
166 3
    public function getFileOptions()
167
    {
168 3
        if (! isset($this->fileOptions)) {
169 1
            $this->setFileOptions([]);
170
        }
171
172 3
        return $this->fileOptions;
173
    }
174
175
    /**
176
     * @param FileOptions|array $fileOptions
177
     * @return $this
178
     */
179 4 View Code Duplication
    public function setFileOptions($fileOptions)
0 ignored issues
show
This method seems to be duplicated in 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...
180
    {
181 4
        if (is_array($fileOptions)) {
182 3
            $this->fileOptions = new FileOptions($fileOptions);
183
        } elseif ($fileOptions instanceof FileOptions) {
184 1
            $this->fileOptions = $fileOptions;
185
        } else {
186 1
            throw new InvalidArgumentException(sprintf(
187 1
                'FileOptions should be an array or an Zend\Mail\Transport\FileOptions object. %s provided.',
188 1
                is_object($fileOptions) ? get_class($fileOptions) : gettype($fileOptions)
189
            ));
190
        }
191
192 3
        return $this;
193
    }
194
195
    /**
196
     * @return array
197
     */
198 13
    public function getMailListeners()
199
    {
200 13
        return $this->mailListeners;
201
    }
202
203
    /**
204
     * @param array $mailListeners
205
     * @return $this
206
     */
207 3
    public function setMailListeners($mailListeners)
208
    {
209 3
        $this->mailListeners = (array) $mailListeners;
210 3
        return $this;
211
    }
212
}
213