Conditions | 42 |
Paths | > 20000 |
Total Lines | 243 |
Code Lines | 138 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
40 | private function createEventNewApi($paramsPre) |
||
41 | { |
||
42 | $params = array_change_key_case($paramsPre, CASE_LOWER); |
||
43 | |||
44 | if (sizeof($params) != sizeof($paramsPre)) { |
||
45 | $params['params_contained_duplicate_keys'] = $paramsPre; |
||
46 | } |
||
47 | |||
48 | ///////////////////////////////////////////////////// |
||
49 | // signature validation |
||
50 | $signatureData = $params['signature']; |
||
51 | $eventData = $params['event-data']; |
||
52 | |||
53 | // check if the timestamp is fresh |
||
54 | $timestamp = $signatureData['timestamp']; |
||
55 | $tsAge = abs(time() - $timestamp); |
||
56 | if ($tsAge > 15) { |
||
57 | return new Response('Signature verification failed. Timestamp too old abs('.time()." - $timestamp) = $tsAge", 401); |
||
58 | } |
||
59 | |||
60 | // validate post-data |
||
61 | $key = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::API_KEY); |
||
62 | $token = $signatureData['token']; |
||
63 | $expectedSignature = hash_hmac('SHA256', $timestamp.$token, $key); |
||
64 | if ($expectedSignature != $signatureData['signature']) { |
||
65 | return new Response('Signature verification failed.', 401); |
||
66 | } |
||
67 | |||
68 | ///////////////////////////////////////////////////// |
||
69 | // create event-entity |
||
70 | try { |
||
71 | // create event & populate with supplied data |
||
72 | $event = new MailgunEvent(); |
||
73 | |||
74 | // token |
||
75 | if (array_key_exists('token', $signatureData)) { |
||
76 | $event->setToken($signatureData['token']); |
||
77 | unset($signatureData['token']); |
||
78 | } |
||
79 | // timestamp |
||
80 | if (array_key_exists('timestamp', $signatureData)) { |
||
81 | $event->setTimestamp($signatureData['timestamp']); |
||
82 | unset($signatureData['timestamp']); |
||
83 | } |
||
84 | // signature |
||
85 | if (array_key_exists('signature', $signatureData)) { |
||
86 | $event->setSignature($signatureData['signature']); |
||
87 | unset($signatureData['signature']); |
||
88 | } |
||
89 | |||
90 | // event |
||
91 | if (array_key_exists('event', $eventData)) { |
||
92 | $event->setEvent($eventData['event']); |
||
93 | unset($eventData['event']); |
||
94 | } |
||
95 | // domain |
||
96 | if (array_key_exists('envelope', $eventData)) { |
||
97 | $envelope = $eventData['envelope']; |
||
98 | $sender = $envelope['sender']; |
||
99 | $event->setDomain(substr($sender, strrpos($sender, '@') + 1)); |
||
100 | |||
101 | // ip |
||
102 | if (array_key_exists('sending-ip', $envelope)) { |
||
103 | $event->setIp($envelope['sending-ip']); |
||
104 | unset($eventData['envelope']['sending-ip']); |
||
105 | } |
||
106 | |||
107 | unset($eventData['envelope']['sender']); |
||
108 | } |
||
109 | // description & reason |
||
110 | if (array_key_exists('delivery-status', $eventData)) { |
||
111 | $description = array_key_exists('message', $eventData['delivery-status']) ? $eventData['delivery-status']['message'].' ' : ''; |
||
112 | $description .= array_key_exists('description', $eventData['delivery-status']) ? $eventData['delivery-status']['description'] : ''; |
||
113 | |||
114 | $event->setDescription($description); |
||
115 | unset($eventData['delivery-status']['message'], $eventData['delivery-status']['description']); |
||
116 | |||
117 | // delivery status code |
||
118 | if (array_key_exists('code', $eventData['delivery-status'])) { |
||
119 | $event->setErrorCode($eventData['delivery-status']['code']); |
||
120 | unset($eventData['delivery-status']['code']); |
||
121 | } |
||
122 | } elseif (array_key_exists('reject', $eventData)) { |
||
123 | $description = array_key_exists('description', $eventData['reject']) ? $eventData['reject']['description'] : ''; |
||
124 | $reason = array_key_exists('reason', $eventData['reject']) ? $eventData['reject']['reason'] : ''; |
||
125 | $event->setDescription($description); |
||
126 | $event->setReason($reason); |
||
127 | unset($eventData['delivery-status']['description'], $eventData['delivery-status']['reason']); |
||
128 | } |
||
129 | // reason |
||
130 | if (array_key_exists('reason', $eventData)) { |
||
131 | $event->setReason($eventData['reason']); |
||
132 | unset($eventData['reason']); |
||
133 | } |
||
134 | // recipient |
||
135 | if (array_key_exists('recipient', $eventData)) { |
||
136 | $event->setRecipient($eventData['recipient']); |
||
137 | unset($eventData['recipient']); |
||
138 | } |
||
139 | if (array_key_exists('geolocation', $eventData)) { |
||
140 | $geolocation = $eventData['geolocation']; |
||
141 | // country |
||
142 | if (array_key_exists('country', $geolocation)) { |
||
143 | $event->setCountry($geolocation['country']); |
||
144 | unset($eventData['geolocation']['country']); |
||
145 | } |
||
146 | // city |
||
147 | if (array_key_exists('city', $geolocation)) { |
||
148 | $event->setCity($geolocation['city']); |
||
149 | unset($eventData['geolocation']['city']); |
||
150 | } |
||
151 | // region |
||
152 | if (array_key_exists('region', $geolocation)) { |
||
153 | $event->setRegion($geolocation['region']); |
||
154 | unset($eventData['geolocation']['region']); |
||
155 | } |
||
156 | } |
||
157 | if (array_key_exists('client-info', $eventData)) { |
||
158 | $clientInfo = $eventData['client-info']; |
||
159 | // clientName |
||
160 | if (array_key_exists('client-name', $clientInfo)) { |
||
161 | $event->setClientName($clientInfo['client-name']); |
||
162 | unset($eventData['client-info']['client-name']); |
||
163 | } |
||
164 | // clientOs |
||
165 | if (array_key_exists('client-os', $clientInfo)) { |
||
166 | $event->setClientOs($clientInfo['client-os']); |
||
167 | unset($eventData['client-info']['client-os']); |
||
168 | } |
||
169 | // clientType |
||
170 | if (array_key_exists('client-type', $clientInfo)) { |
||
171 | $event->setClientType($clientInfo['client-type']); |
||
172 | unset($eventData['client-info']['client-type']); |
||
173 | } |
||
174 | // deviceType |
||
175 | if (array_key_exists('device-type', $clientInfo)) { |
||
176 | $event->setDeviceType($clientInfo['device-type']); |
||
177 | unset($eventData['client-info']['device-type']); |
||
178 | } |
||
179 | // userAgent |
||
180 | if (array_key_exists('user-agent', $clientInfo)) { |
||
181 | $event->setUserAgent($clientInfo['user-agent']); |
||
182 | unset($eventData['client-info']['user-agent']); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | if (array_key_exists('message', $eventData)) { |
||
187 | $message = $eventData['message']; |
||
188 | |||
189 | // messageHeaders |
||
190 | if (array_key_exists('headers', $message)) { |
||
191 | $headers = $message['headers']; |
||
192 | // messageId |
||
193 | if (array_key_exists('message-id', $headers)) { |
||
194 | $trimmedMessageId = trim(trim($headers['message-id']), '<>'); |
||
195 | $event->setMessageId($trimmedMessageId); |
||
196 | |||
197 | // set message domain from message id |
||
198 | if (null == $event->getDomain()) { |
||
199 | $event->setDomain(substr($trimmedMessageId, strrpos($trimmedMessageId, '@') + 1)); |
||
200 | } |
||
201 | } |
||
202 | |||
203 | // sender |
||
204 | if(array_key_exists('from', $headers)){ |
||
205 | $event->setSender($headers['from']); |
||
206 | } |
||
207 | |||
208 | $event->setMessageHeaders(json_encode($headers)); |
||
209 | unset($eventData['message']['headers']); |
||
210 | } |
||
211 | } |
||
212 | // campaignName && campaignId |
||
213 | if (array_key_exists('campaigns', $eventData)) { |
||
214 | $event->setCampaignName(print_r($eventData['campaigns'], true)); |
||
215 | $event->setCampaignId(print_r($eventData['campaigns'], true)); |
||
216 | unset($eventData['campaigns']); |
||
217 | } |
||
218 | // tag |
||
219 | if (array_key_exists('tags', $eventData)) { |
||
220 | $event->setTag(print_r($eventData['tags'], true)); |
||
221 | unset($eventData['tags']); |
||
222 | } |
||
223 | // url |
||
224 | if (array_key_exists('url', $eventData)) { |
||
225 | $event->setUrl($eventData['url']); |
||
226 | unset($eventData['url']); |
||
227 | } elseif (array_key_exists('storage', $eventData)) { |
||
228 | $event->setUrl($eventData['storage']['url']); |
||
229 | unset($eventData['storage']['url']); |
||
230 | } |
||
231 | |||
232 | // mailingList |
||
233 | if (array_key_exists('recipients', $eventData)) { |
||
234 | $event->setmailingList(print_r($eventData['recipients'], true)); |
||
235 | unset($eventData['recipients']); |
||
236 | } |
||
237 | |||
238 | $manager = $this->container->get('doctrine.orm.entity_manager'); |
||
239 | $manager->persist($event); |
||
240 | $this->getDoctrine()->getRepository(MailgunMessageSummary::class)->createOrUpdateMessageSummary($event); |
||
241 | |||
242 | $eventData = $this->removeEmptyArrayElements($eventData); |
||
243 | |||
244 | // process the remaining posted values |
||
245 | foreach ($eventData as $key => $value) { |
||
246 | if (0 === strpos($key, 'attachment-')) { |
||
247 | // create event attachments |
||
248 | $attachment = new MailgunAttachment($event); |
||
249 | $attachment->setCounter(substr($key, 11)); |
||
250 | |||
251 | // get the file |
||
252 | /* @var $value UploadedFile */ |
||
253 | $attachment->setContent(file_get_contents($value->getPathname())); |
||
254 | $attachment->setSize($value->getSize()); |
||
255 | $attachment->setType($value->getMimeType()); |
||
256 | $attachment->setName($value->getFilename()); |
||
257 | |||
258 | $manager->persist($attachment); |
||
259 | } else { |
||
260 | // create custom-variables for event |
||
261 | $customVar = new MailgunCustomVariable($event); |
||
262 | $customVar->setVariableName($key); |
||
263 | $customVar->setContent($value); |
||
264 | |||
265 | $manager->persist($customVar); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | // save all entities |
||
270 | $manager->flush(); |
||
271 | |||
272 | // Dispatch an event about the logging of a Webhook-call |
||
273 | $this->get('event_dispatcher')->dispatch(MailgunEvent::CREATE_EVENT, new MailgunWebhookEvent($event)); |
||
274 | } catch (\Exception $e) { |
||
275 | $this->container->get('logger')->warning('AzineMailgunWebhooksBundle: creating entities failed: '.$e->getMessage()); |
||
276 | $this->container->get('logger')->warning($e->getTraceAsString()); |
||
277 | |||
278 | return new Response(print_r($params, true).'AzineMailgunWebhooksBundle: creating entities failed: '.$e->getMessage(), 500); |
||
279 | } |
||
280 | |||
281 | // send response |
||
282 | return new Response('Thanx, for the info.', 200); |
||
283 | } |
||
533 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.