Complex classes like MailServiceAbstractFactory 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MailServiceAbstractFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class MailServiceAbstractFactory extends AbstractAcMailerFactory |
||
28 | { |
||
29 | const SPECIFIC_PART = 'mailservice'; |
||
30 | |||
31 | /** |
||
32 | * @var MailOptions |
||
33 | */ |
||
34 | protected $mailOptions; |
||
35 | |||
36 | /** |
||
37 | * Create service with name |
||
38 | * |
||
39 | * @param ServiceLocatorInterface $sm |
||
40 | * @param $name |
||
41 | * @param $requestedName |
||
42 | * @return mixed |
||
43 | */ |
||
44 | 13 | public function createServiceWithName(ServiceLocatorInterface $sm, $name, $requestedName) |
|
45 | { |
||
46 | 13 | $specificServiceName = explode('.', $name)[2]; |
|
47 | 13 | $this->mailOptions = $sm->get( |
|
|
|||
48 | 13 | sprintf('%s.%s.%s', self::ACMAILER_PART, MailOptionsAbstractFactory::SPECIFIC_PART, $specificServiceName) |
|
49 | ); |
||
50 | |||
51 | // Create the service |
||
52 | 13 | $message = $this->createMessage(); |
|
53 | 13 | $transport = $this->createTransport($sm); |
|
54 | 11 | $renderer = $this->createRenderer($sm); |
|
55 | 11 | $mailService = new MailService($message, $transport, $renderer); |
|
56 | |||
57 | // Set subject |
||
58 | 11 | $mailService->setSubject($this->mailOptions->getMessageOptions()->getSubject()); |
|
59 | |||
60 | // Set body, either by using a template or a raw body |
||
61 | 11 | $body = $this->mailOptions->getMessageOptions()->getBody(); |
|
62 | 11 | if ($body->getUseTemplate()) { |
|
63 | 2 | $defaultLayoutConfig = $body->getTemplate()->getDefaultLayout(); |
|
64 | 2 | if (isset($defaultLayoutConfig['path'])) { |
|
65 | 1 | $params = isset($defaultLayoutConfig['params']) ? $defaultLayoutConfig['params'] : []; |
|
66 | 1 | $captureTo = isset($defaultLayoutConfig['template_capture_to']) |
|
67 | ? $defaultLayoutConfig['template_capture_to'] |
||
68 | 1 | : 'content'; |
|
69 | 1 | $mailService->setDefaultLayout(new DefaultLayout($defaultLayoutConfig['path'], $params, $captureTo)); |
|
70 | } |
||
71 | 2 | $mailService->setTemplate($body->getTemplate()->toViewModel(), ['charset' => $body->getCharset()]); |
|
72 | } else { |
||
73 | 9 | $mailService->setBody($body->getContent(), $body->getCharset()); |
|
74 | } |
||
75 | |||
76 | // Attach files |
||
77 | 11 | $files = $this->mailOptions->getMessageOptions()->getAttachments()->getFiles(); |
|
78 | 11 | $mailService->addAttachments($files); |
|
79 | |||
80 | // Attach files from dir |
||
81 | 11 | $dir = $this->mailOptions->getMessageOptions()->getAttachments()->getDir(); |
|
82 | 11 | if ($dir['iterate'] === true && is_string($dir['path']) && is_dir($dir['path'])) { |
|
83 | 1 | $files = $dir['recursive'] === true ? |
|
84 | 1 | new \RecursiveIteratorIterator( |
|
85 | 1 | new \RecursiveDirectoryIterator($dir['path'], \RecursiveDirectoryIterator::SKIP_DOTS), |
|
86 | 1 | \RecursiveIteratorIterator::CHILD_FIRST |
|
87 | ): |
||
88 | 1 | new \DirectoryIterator($dir['path']); |
|
89 | |||
90 | /* @var \SplFileInfo $fileInfo */ |
||
91 | 1 | foreach ($files as $fileInfo) { |
|
92 | 1 | if ($fileInfo->isDir()) { |
|
93 | 1 | continue; |
|
94 | } |
||
95 | 1 | $mailService->addAttachment($fileInfo->getPathname()); |
|
96 | } |
||
97 | } |
||
98 | |||
99 | // Attach mail listeners |
||
100 | 11 | $this->attachMailListeners($mailService, $sm); |
|
101 | 10 | return $mailService; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @return Message |
||
106 | */ |
||
107 | 13 | protected function createMessage() |
|
108 | { |
||
109 | 13 | $options = $this->mailOptions->getMessageOptions(); |
|
110 | // Prepare Mail Message |
||
111 | 13 | $message = new Message(); |
|
112 | 13 | $from = $options->getFrom(); |
|
113 | 13 | if (! empty($from)) { |
|
114 | 1 | $message->setFrom($from, $options->getFromName()); |
|
115 | } |
||
116 | 13 | $replyTo = $options->getReplyTo(); |
|
117 | 13 | if (! empty($replyTo)) { |
|
118 | 1 | $message->setReplyTo($replyTo, $options->getReplyToName()); |
|
119 | } |
||
120 | 13 | $to = $options->getTo(); |
|
121 | 13 | if (! empty($to)) { |
|
122 | 1 | $message->setTo($to); |
|
123 | } |
||
124 | 13 | $cc = $options->getCc(); |
|
125 | 13 | if (! empty($cc)) { |
|
126 | 1 | $message->setCc($cc); |
|
127 | } |
||
128 | 13 | $bcc = $options->getBcc(); |
|
129 | 13 | if (! empty($bcc)) { |
|
130 | 1 | $message->setBcc($bcc); |
|
131 | } |
||
132 | |||
133 | 13 | return $message; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param ServiceLocatorInterface $sm |
||
138 | * @return TransportInterface |
||
139 | */ |
||
140 | 13 | protected function createTransport(ServiceLocatorInterface $sm) |
|
141 | { |
||
142 | 13 | $adapter = $this->mailOptions->getMailAdapter(); |
|
143 | // A transport instance can be returned as is |
||
144 | 13 | if ($adapter instanceof TransportInterface) { |
|
145 | 1 | return $this->setupTransportConfig($adapter); |
|
146 | } |
||
147 | |||
148 | // Check if the adapter is a service |
||
149 | 12 | if (is_string($adapter) && $sm->has($adapter)) { |
|
150 | /** @var TransportInterface $transport */ |
||
151 | 2 | $transport = $sm->get($adapter); |
|
152 | 2 | if ($transport instanceof TransportInterface) { |
|
153 | 1 | return $this->setupTransportConfig($transport); |
|
154 | } else { |
||
155 | 1 | throw new InvalidArgumentException( |
|
156 | 1 | 'Provided mail_adapter service does not return a "Zend\Mail\Transport\TransportInterface" instance' |
|
157 | ); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | // Check if the adapter is one of Zend's default adapters |
||
162 | 10 | if (is_string($adapter) && is_subclass_of($adapter, 'Zend\Mail\Transport\TransportInterface')) { |
|
163 | 9 | return $this->setupTransportConfig(new $adapter()); |
|
164 | } |
||
165 | |||
166 | // The adapter is not valid. Throw an exception |
||
167 | 1 | throw new InvalidArgumentException(sprintf( |
|
168 | 1 | 'mail_adapter must be an instance of "Zend\Mail\Transport\TransportInterface" or string, "%s" provided', |
|
169 | 1 | is_object($adapter) ? get_class($adapter) : gettype($adapter) |
|
170 | )); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param TransportInterface $transport |
||
175 | * @return TransportInterface |
||
176 | */ |
||
177 | 11 | protected function setupTransportConfig(TransportInterface $transport) |
|
178 | { |
||
179 | 11 | if ($transport instanceof Smtp) { |
|
180 | 1 | $transport->setOptions($this->mailOptions->getSmtpOptions()); |
|
181 | } elseif ($transport instanceof File) { |
||
182 | 1 | $transport->setOptions($this->mailOptions->getFileOptions()); |
|
183 | } |
||
184 | |||
185 | 11 | return $transport; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * @param ServiceLocatorInterface $sm |
||
190 | * @return RendererInterface |
||
191 | */ |
||
192 | 11 | protected function createRenderer(ServiceLocatorInterface $sm) |
|
193 | { |
||
194 | // Try to return the configured renderer. If it points to an undefined service, create a renderer on the fly |
||
195 | 11 | $serviceName = $this->mailOptions->getRenderer(); |
|
196 | |||
197 | try { |
||
198 | 11 | $renderer = $sm->get($serviceName); |
|
199 | 3 | return $renderer; |
|
200 | 9 | } catch (ServiceNotFoundException $e) { |
|
201 | // In case the renderer service is not defined, try to construct it |
||
202 | 9 | $vmConfig = $this->getSpecificConfig($sm, 'view_manager'); |
|
203 | 9 | $renderer = new PhpRenderer(); |
|
204 | |||
205 | // Check what kind of view_manager configuration has been defined |
||
206 | 9 | if (isset($vmConfig['template_map']) && isset($vmConfig['template_path_stack'])) { |
|
207 | // If both a template_map and a template_path_stack have been defined, create an AggregateResolver |
||
208 | 1 | $pathStackResolver = new TemplatePathStack(); |
|
209 | 1 | $pathStackResolver->setPaths($vmConfig['template_path_stack']); |
|
210 | 1 | $resolver = new AggregateResolver(); |
|
211 | 1 | $resolver->attach($pathStackResolver) |
|
212 | 1 | ->attach(new TemplateMapResolver($vmConfig['template_map'])); |
|
213 | 1 | $renderer->setResolver($resolver); |
|
214 | 9 | } elseif (isset($vmConfig['template_map'])) { |
|
215 | // Create a TemplateMapResolver in case only the template_map has been defined |
||
216 | 1 | $renderer->setResolver(new TemplateMapResolver($vmConfig['template_map'])); |
|
217 | 9 | } elseif (isset($vmConfig['template_path_stack'])) { |
|
218 | // Create a TemplatePathStack resolver in case only the template_path_stack has been defined |
||
219 | 9 | $pathStackResolver = new TemplatePathStack(); |
|
220 | 9 | $pathStackResolver->setPaths($vmConfig['template_path_stack']); |
|
221 | 9 | $renderer->setResolver($pathStackResolver); |
|
222 | } |
||
223 | |||
224 | // Create a HelperPluginManager with default view helpers and user defined view helpers |
||
225 | 9 | $renderer->setHelperPluginManager($this->createHelperPluginManager($sm)); |
|
226 | 9 | return $renderer; |
|
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Creates a view helper manager |
||
232 | * @param ServiceLocatorInterface $sm |
||
233 | * @return HelperPluginManager |
||
234 | */ |
||
235 | 9 | protected function createHelperPluginManager(ServiceLocatorInterface $sm) |
|
244 | |||
245 | /** |
||
246 | * Returns a specific configuration defined by provided key |
||
247 | * @param ServiceLocatorInterface $sm |
||
248 | * @param $configKey |
||
249 | * @return array |
||
250 | */ |
||
251 | 9 | protected function getSpecificConfig(ServiceLocatorInterface $sm, $configKey) |
|
256 | |||
257 | /** |
||
258 | * Attaches the preconfigured mail listeners to the mail service |
||
259 | * |
||
260 | * @param MailListenerAwareInterface $service |
||
261 | * @param ServiceLocatorInterface $sm |
||
262 | * @throws InvalidArgumentException |
||
263 | */ |
||
264 | 11 | protected function attachMailListeners(MailListenerAwareInterface $service, ServiceLocatorInterface $sm) |
|
286 | } |
||
287 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.