Total Complexity | 41 |
Total Lines | 238 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Mailer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mailer, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Helpers; |
||
15 | class Mailer |
||
16 | { |
||
17 | /** |
||
18 | * @var MODxMailer $mail |
||
19 | */ |
||
20 | protected $mail = null; |
||
21 | protected $modx = null; |
||
22 | public $config = array(); |
||
23 | protected $debug = false; |
||
24 | protected $queuePath = 'assets/cache/mail/'; |
||
25 | protected $noemail = false; |
||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $Body = ''; |
||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $Subject = ''; |
||
35 | |||
36 | /** |
||
37 | * Mailer constructor. |
||
38 | * @param DocumentParser $modx |
||
39 | * @param $cfg |
||
40 | * @param bool $debug |
||
41 | */ |
||
42 | public function __construct(DocumentParser $modx, $cfg, $debug = false) |
||
43 | { |
||
44 | $this->modx = $modx; |
||
45 | $this->noemail = (bool)(isset($cfg['noemail']) ? $cfg['noemail'] : 0); |
||
46 | if (!$this->noemail) { |
||
47 | $this->mail = new MODxMailer(); |
||
48 | if (method_exists('MODxMailer', 'init')) { |
||
49 | $this->mail->init($modx); |
||
50 | } |
||
51 | $this->config = $cfg; |
||
52 | $this->debug = $debug; |
||
53 | $this->applyMailConfig(); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param string $type |
||
59 | * @param string $addr |
||
60 | * @return $this |
||
61 | */ |
||
62 | public function addAddressToMailer($type, $addr) |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param array $filelist |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function attachFiles($filelist = array()) |
||
91 | { |
||
92 | if (!$this->noemail) { |
||
93 | $fs = FS::getInstance(); |
||
94 | $contentType = "application/octetstream"; |
||
95 | foreach ($filelist as $file) { |
||
96 | if (is_file($file['filepath']) && is_readable($file['filepath'])) { |
||
97 | $this->mail->AddAttachment($file['filepath'], $file['filename'], "base64", $contentType); |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | |||
102 | return $this; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param $report |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function send($report) |
||
110 | { |
||
111 | //если отправлять некуда или незачем, то делаем вид, что отправили |
||
112 | if (!$this->getCFGDef('to') || $this->noemail) { |
||
113 | return true; |
||
114 | } elseif (empty($report)) { |
||
115 | return false; |
||
116 | } |
||
117 | |||
118 | $this->mail->Body = $this->getCFGDef('isHtml',1) ? $this->mail->msgHTML($report, MODX_BASE_PATH) : $report; |
||
119 | |||
120 | $result = $this->mail->send(); |
||
121 | if ($result) { |
||
122 | $this->mail->ClearAllRecipients(); |
||
123 | $this->mail->ClearAttachments(); |
||
124 | } |
||
125 | |||
126 | return $result; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param $report |
||
131 | * @return bool |
||
132 | */ |
||
133 | public function toQueue($report) |
||
134 | { |
||
135 | //если отправлять некуда или незачем, то делаем вид, что отправили |
||
136 | if (!$this->getCFGDef('to') || $this->noemail) { |
||
137 | return true; |
||
138 | } elseif (empty($report)) { |
||
139 | return false; |
||
140 | } |
||
141 | |||
142 | $this->mail->Body = $this->getCFGDef('isHtml',1) ? $this->mail->msgHTML($report, MODX_BASE_PATH) : $report; |
||
143 | |||
144 | $this->Body = $this->modx->removeSanitizeSeed($this->mail->Body); |
||
145 | $this->Subject = $this->modx->removeSanitizeSeed($this->mail->Subject); |
||
146 | try { |
||
147 | $result = $this->mail->preSend() && $this->saveMessage(); |
||
148 | } catch (phpmailerException $e) { |
||
149 | $this->mail->SetError($e->getMessage()); |
||
150 | |||
151 | $result = false; |
||
152 | } |
||
153 | |||
154 | if ($result) { |
||
155 | $this->mail->ClearAllRecipients(); |
||
156 | $this->mail->ClearAttachments(); |
||
157 | $result = $this->getFileName(); |
||
158 | } |
||
159 | |||
160 | return $result; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * @param string $path |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function setQueuePath($path = '') { |
||
173 | } |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return mixed |
||
178 | */ |
||
179 | protected function saveMessage() |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function getFileName() { |
||
200 | return $this->mail->getMessageID() . '.eml'; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param $file |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function fromQueue($file) |
||
208 | { |
||
209 | $result = false; |
||
210 | $dir = MODX_BASE_PATH . $this->queuePath; |
||
211 | $fs = FS::getInstance(); |
||
212 | if ($fs->checkFile($dir)) { |
||
213 | $message = unserialize(file_get_contents($dir . $file)); |
||
214 | $this->config = $message['config']; |
||
215 | $this->applyMailConfig(); |
||
216 | $this->mail->setMIMEHeader($message['header'])->setMIMEBody($message['body']); |
||
217 | unset($message); |
||
218 | $result = $this->mail->postSend(); |
||
219 | if ($result) { |
||
220 | $this->mail->setMIMEBody()->setMIMEHeader(); |
||
221 | FS::getInstance()->unlink($dir . $file); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | return $result; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return $this |
||
230 | */ |
||
231 | protected function applyMailConfig() |
||
232 | { |
||
233 | $this->mail->IsHTML($this->getCFGDef('isHtml', 1)); |
||
234 | $this->mail->From = $this->getCFGDef('from', $this->modx->config['emailsender']); |
||
235 | $this->mail->FromName = $this->getCFGDef('fromName', $this->modx->config['site_name']); |
||
236 | $this->mail->Subject = $this->getCFGDef('subject'); |
||
237 | $this->addAddressToMailer("replyTo", $this->getCFGDef('replyTo')); |
||
238 | $this->addAddressToMailer("to", $this->getCFGDef('to')); |
||
239 | $this->addAddressToMailer("cc", $this->getCFGDef('cc')); |
||
240 | $this->addAddressToMailer("bcc", $this->getCFGDef('bcc')); |
||
241 | |||
242 | return $this; |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @param string $param |
||
247 | * @param mixed $default |
||
248 | * @return mixed |
||
249 | */ |
||
250 | public function getCFGDef($param, $default = null) |
||
253 | } |
||
254 | } |
||
255 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths