Complex classes like SendgridTransport 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 SendgridTransport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class SendgridTransport extends Transport |
||
17 | { |
||
18 | use SendGrid { |
||
19 | sgDecode as decode; |
||
20 | } |
||
21 | |||
22 | const SMTP_API_NAME = 'sendgrid/x-smtpapi'; |
||
23 | const BASE_URL = 'https://api.sendgrid.com/v3/mail/send'; |
||
24 | |||
25 | /** |
||
26 | * @var Client |
||
27 | */ |
||
28 | private $client; |
||
29 | private $attachments; |
||
30 | private $numberOfRecipients; |
||
31 | private $apiKey; |
||
32 | private $endpoint; |
||
33 | |||
34 | public function __construct(ClientInterface $client, $api_key, $endpoint = null) |
||
35 | { |
||
36 | $this->client = $client; |
||
|
|||
37 | $this->apiKey = $api_key; |
||
38 | $this->endpoint = isset($endpoint) ? $endpoint : self::BASE_URL; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | public function send(Swift_Mime_SimpleMessage $message, &$failedRecipients = null) |
||
45 | { |
||
46 | $this->beforeSendPerformed($message); |
||
47 | |||
48 | $data = [ |
||
49 | 'personalizations' => $this->getPersonalizations($message), |
||
50 | 'from' => $this->getFrom($message), |
||
51 | 'subject' => $message->getSubject(), |
||
52 | ]; |
||
53 | |||
54 | if ($contents = $this->getContents($message)) { |
||
55 | $data['content'] = $contents; |
||
56 | } |
||
57 | |||
58 | if ($reply_to = $this->getReplyTo($message)) { |
||
59 | $data['reply_to'] = $reply_to; |
||
60 | } |
||
61 | |||
62 | $attachments = $this->getAttachments($message); |
||
63 | if (count($attachments) > 0) { |
||
64 | $data['attachments'] = $attachments; |
||
65 | } |
||
66 | |||
67 | $data = $this->setParameters($message, $data); |
||
68 | |||
69 | $payload = [ |
||
70 | 'headers' => [ |
||
71 | 'Authorization' => 'Bearer ' . $this->apiKey, |
||
72 | 'Content-Type' => 'application/json', |
||
73 | ], |
||
74 | 'json' => $data, |
||
75 | ]; |
||
76 | |||
77 | $response = $this->post($payload); |
||
78 | |||
79 | $message->getHeaders()->addTextHeader('X-Sendgrid-Message-Id', $response->getHeaderLine('X-Message-Id')); |
||
80 | |||
81 | $this->sendPerformed($message); |
||
82 | |||
83 | return $this->numberOfRecipients ?: $this->numberOfRecipients($message); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param Swift_Mime_SimpleMessage $message |
||
88 | * @return array |
||
89 | */ |
||
90 | private function getPersonalizations(Swift_Mime_SimpleMessage $message) |
||
91 | { |
||
92 | $setter = function (array $addresses) { |
||
93 | $recipients = []; |
||
94 | foreach ($addresses as $email => $name) { |
||
95 | $address = []; |
||
96 | $address['email'] = $email; |
||
97 | if ($name) { |
||
98 | $address['name'] = $name; |
||
99 | } |
||
100 | $recipients[] = $address; |
||
101 | } |
||
102 | return $recipients; |
||
103 | }; |
||
104 | |||
105 | $personalization['to'] = $setter($message->getTo()); |
||
106 | |||
107 | if ($cc = $message->getCc()) { |
||
108 | $personalization['cc'] = $setter($cc); |
||
109 | } |
||
110 | |||
111 | if ($bcc = $message->getBcc()) { |
||
112 | $personalization['bcc'] = $setter($bcc); |
||
113 | } |
||
114 | |||
115 | return [$personalization]; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Get From Addresses. |
||
120 | * |
||
121 | * @param Swift_Mime_SimpleMessage $message |
||
122 | * @return array |
||
123 | */ |
||
124 | private function getFrom(Swift_Mime_SimpleMessage $message) |
||
125 | { |
||
126 | if ($message->getFrom()) { |
||
127 | foreach ($message->getFrom() as $email => $name) { |
||
128 | return ['email' => $email, 'name' => $name]; |
||
129 | } |
||
130 | } |
||
131 | return []; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get ReplyTo Addresses. |
||
136 | * |
||
137 | * @param Swift_Mime_SimpleMessage $message |
||
138 | * @return array |
||
139 | */ |
||
140 | private function getReplyTo(Swift_Mime_SimpleMessage $message) |
||
141 | { |
||
142 | if ($message->getReplyTo()) { |
||
143 | foreach ($message->getReplyTo() as $email => $name) { |
||
144 | return ['email' => $email, 'name' => $name]; |
||
145 | } |
||
146 | } |
||
147 | return null; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Get contents. |
||
152 | * |
||
153 | * @param Swift_Mime_SimpleMessage $message |
||
154 | * @return array |
||
155 | */ |
||
156 | private function getContents(Swift_Mime_SimpleMessage $message) |
||
157 | { |
||
158 | $contentType = $message->getContentType(); |
||
159 | switch ($contentType) { |
||
160 | case 'text/plain': |
||
161 | return [ |
||
162 | [ |
||
163 | 'type' => 'text/plain', |
||
164 | 'value' => $message->getBody(), |
||
165 | |||
166 | ], |
||
167 | ]; |
||
168 | case 'text/html': |
||
169 | return [ |
||
170 | [ |
||
171 | 'type' => 'text/html', |
||
172 | 'value' => $message->getBody(), |
||
173 | ], |
||
174 | ]; |
||
175 | } |
||
176 | |||
177 | // Following RFC 1341, text/html after text/plain in multipart |
||
178 | $content = []; |
||
179 | foreach ($message->getChildren() as $child) { |
||
180 | if ($child instanceof Swift_MimePart && $child->getContentType() === 'text/plain') { |
||
181 | $content[] = [ |
||
182 | 'type' => 'text/plain', |
||
183 | 'value' => $child->getBody(), |
||
184 | ]; |
||
185 | } |
||
186 | } |
||
187 | |||
188 | if (is_null($message->getBody())) { |
||
189 | return null; |
||
190 | } |
||
191 | |||
192 | $content[] = [ |
||
193 | 'type' => 'text/html', |
||
194 | 'value' => $message->getBody(), |
||
195 | ]; |
||
196 | return $content; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param Swift_Mime_SimpleMessage $message |
||
201 | * @return array |
||
202 | */ |
||
203 | private function getAttachments(Swift_Mime_SimpleMessage $message) |
||
204 | { |
||
205 | $attachments = []; |
||
206 | foreach ($message->getChildren() as $attachment) { |
||
207 | if ((!$attachment instanceof Swift_Attachment && !$attachment instanceof Swift_Image) |
||
208 | || $attachment->getFilename() === self::SMTP_API_NAME |
||
209 | ) { |
||
210 | continue; |
||
211 | } |
||
212 | $attachments[] = [ |
||
213 | 'content' => base64_encode($attachment->getBody()), |
||
214 | 'filename' => $attachment->getFilename(), |
||
215 | 'type' => $attachment->getContentType(), |
||
216 | 'disposition' => $attachment->getDisposition(), |
||
217 | 'content_id' => $attachment->getId(), |
||
218 | ]; |
||
219 | } |
||
220 | return $this->attachments = $attachments; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Set Request Body Parameters |
||
225 | * |
||
226 | * @param Swift_Mime_SimpleMessage $message |
||
227 | * @param array $data |
||
228 | * @return array |
||
229 | * @throws \Exception |
||
230 | */ |
||
231 | protected function setParameters(Swift_Mime_SimpleMessage $message, $data) |
||
282 | |||
283 | private function setPersonalizations(&$data, $personalizations) |
||
296 | |||
297 | /** |
||
298 | * @param $payload |
||
299 | * @return ResponseInterface |
||
300 | * @throws GuzzleException |
||
301 | */ |
||
302 | private function post($payload) |
||
303 | { |
||
306 | } |
||
307 |
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 given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.