Conditions | 38 |
Total Lines | 226 |
Code Lines | 130 |
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 |
||
285 | private function createEventOldApi($paramsPre) |
||
286 | { |
||
287 | $params = array_change_key_case($paramsPre, CASE_LOWER); |
||
288 | |||
289 | if (sizeof($params) != sizeof($paramsPre)) { |
||
290 | $params['params_contained_duplicate_keys'] = $paramsPre; |
||
291 | } |
||
292 | |||
293 | // validate post-data |
||
294 | $key = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::API_KEY); |
||
295 | $timestamp = $params['timestamp']; |
||
296 | |||
297 | // check if the timestamp is fresh |
||
298 | $now = time(); |
||
299 | $tsAge = abs($now - $timestamp); |
||
300 | if ($tsAge > 15) { |
||
301 | return new Response("Signature verification failed. Timestamp too old abs($now - $timestamp)=$tsAge", 401); |
||
302 | } |
||
303 | |||
304 | $token = $params['token']; |
||
305 | $expectedSignature = hash_hmac('SHA256', $timestamp.$token, $key); |
||
306 | if ($expectedSignature != $params['signature']) { |
||
307 | return new Response('Signature verification failed.', 401); |
||
308 | } |
||
309 | |||
310 | // drop unused variables |
||
311 | if (array_key_exists('x-mailgun-sid', $params)) { |
||
312 | unset($params['x-mailgun-sid']); |
||
313 | } |
||
314 | if (array_key_exists('attachment-count', $params)) { |
||
315 | unset($params['attachment-count']); |
||
316 | } |
||
317 | |||
318 | try { |
||
319 | // create event & populate with supplied data |
||
320 | $event = new MailgunEvent(); |
||
321 | |||
322 | // event |
||
323 | if (array_key_exists('event', $params)) { |
||
324 | $event->setEvent($params['event']); |
||
325 | unset($params['event']); |
||
326 | } |
||
327 | |||
328 | // domain |
||
329 | if (array_key_exists('domain', $params)) { |
||
330 | $event->setDomain($params['domain']); |
||
331 | unset($params['domain']); |
||
332 | } |
||
333 | // description |
||
334 | if (array_key_exists('description', $params)) { |
||
335 | $event->setDescription($params['description']); |
||
336 | unset($params['description']); |
||
337 | } |
||
338 | // reason |
||
339 | if (array_key_exists('reason', $params)) { |
||
340 | $event->setReason($params['reason']); |
||
341 | unset($params['reason']); |
||
342 | } |
||
343 | // recipient |
||
344 | if (array_key_exists('recipient', $params)) { |
||
345 | $event->setRecipient($params['recipient']); |
||
346 | unset($params['recipient']); |
||
347 | } |
||
348 | // errorCode |
||
349 | if (array_key_exists('code', $params)) { |
||
350 | $event->setErrorCode($params['code']); |
||
351 | unset($params['code']); |
||
352 | } |
||
353 | // ip |
||
354 | if (array_key_exists('ip', $params)) { |
||
355 | $event->setIp($params['ip']); |
||
356 | unset($params['ip']); |
||
357 | } |
||
358 | // error |
||
359 | if (array_key_exists('error', $params)) { |
||
360 | $event->setDescription($params['error']); |
||
361 | unset($params['error']); |
||
362 | } |
||
363 | // country |
||
364 | if (array_key_exists('country', $params)) { |
||
365 | $event->setCountry($params['country']); |
||
366 | unset($params['country']); |
||
367 | } |
||
368 | // city |
||
369 | if (array_key_exists('city', $params)) { |
||
370 | $event->setCity($params['city']); |
||
371 | unset($params['city']); |
||
372 | } |
||
373 | // region |
||
374 | if (array_key_exists('region', $params)) { |
||
375 | $event->setRegion($params['region']); |
||
376 | unset($params['region']); |
||
377 | } |
||
378 | // campaignId |
||
379 | if (array_key_exists('campaign-id', $params)) { |
||
380 | $event->setCampaignId($params['campaign-id']); |
||
381 | unset($params['campaign-id']); |
||
382 | } |
||
383 | // campaignName { |
||
384 | if (array_key_exists('campaign-name', $params)) { |
||
385 | $event->setCampaignName($params['campaign-name']); |
||
386 | unset($params['campaign-name']); |
||
387 | } |
||
388 | // clientName |
||
389 | if (array_key_exists('client-name', $params)) { |
||
390 | $event->setClientName($params['client-name']); |
||
391 | unset($params['client-name']); |
||
392 | } |
||
393 | // clientOs |
||
394 | if (array_key_exists('client-os', $params)) { |
||
395 | $event->setClientOs($params['client-os']); |
||
396 | unset($params['client-os']); |
||
397 | } |
||
398 | // clientType |
||
399 | if (array_key_exists('client-type', $params)) { |
||
400 | $event->setClientType($params['client-type']); |
||
401 | unset($params['client-type']); |
||
402 | } |
||
403 | // deviceType |
||
404 | if (array_key_exists('device-type', $params)) { |
||
405 | $event->setDeviceType($params['device-type']); |
||
406 | unset($params['device-type']); |
||
407 | } |
||
408 | // mailingList |
||
409 | if (array_key_exists('mailing-list', $params)) { |
||
410 | $event->setmailingList($params['mailing-list']); |
||
411 | unset($params['mailing-list']); |
||
412 | } |
||
413 | // messageHeaders |
||
414 | if (array_key_exists('message-headers', $params)) { |
||
415 | $headers = json_decode($params['message-headers']); |
||
416 | foreach ($headers as $header) { |
||
417 | // sender |
||
418 | if ($header[0] == 'Sender') { |
||
419 | $event->setSender($header[1]); |
||
420 | } |
||
421 | } |
||
422 | $event->setMessageHeaders($params['message-headers']); |
||
423 | unset($params['message-headers']); |
||
424 | } |
||
425 | // messageId |
||
426 | if (array_key_exists('message-id', $params)) { |
||
427 | $trimmedMessageId = trim(trim($params['message-id']), '<>'); |
||
428 | $event->setMessageId($trimmedMessageId); |
||
429 | unset($params['message-id']); |
||
430 | } |
||
431 | // tag |
||
432 | if (array_key_exists('tag', $params)) { |
||
433 | $event->setTag($params['tag']); |
||
434 | unset($params['tag']); |
||
435 | } |
||
436 | // x-mailgun-tag |
||
437 | if (array_key_exists('x-mailgun-tag', $params)) { |
||
438 | $event->setTag($params['x-mailgun-tag']); |
||
439 | unset($params['x-mailgun-tag']); |
||
440 | } |
||
441 | // userAgent |
||
442 | if (array_key_exists('user-agent', $params)) { |
||
443 | $event->setUserAgent($params['user-agent']); |
||
444 | unset($params['user-agent']); |
||
445 | } |
||
446 | // url |
||
447 | if (array_key_exists('url', $params)) { |
||
448 | $event->setUrl($params['url']); |
||
449 | unset($params['url']); |
||
450 | } |
||
451 | // token |
||
452 | if (array_key_exists('token', $params)) { |
||
453 | $event->setToken($params['token']); |
||
454 | unset($params['token']); |
||
455 | } |
||
456 | // timestamp |
||
457 | if (array_key_exists('timestamp', $params)) { |
||
458 | $event->setTimestamp($params['timestamp']); |
||
459 | unset($params['timestamp']); |
||
460 | } |
||
461 | // signature |
||
462 | if (array_key_exists('signature', $params)) { |
||
463 | $event->setSignature($params['signature']); |
||
464 | unset($params['signature']); |
||
465 | } |
||
466 | |||
467 | $manager = $this->container->get('doctrine.orm.entity_manager'); |
||
468 | $manager->persist($event); |
||
469 | |||
470 | $this->getDoctrine()->getRepository(MailgunMessageSummary::class)->createOrUpdateMessageSummary($event); |
||
471 | |||
472 | $params = $this->removeEmptyArrayElements($params); |
||
473 | |||
474 | // process the remaining posted values |
||
475 | foreach ($params as $key => $value) { |
||
476 | if (0 === strpos($key, 'attachment-')) { |
||
477 | // create event attachments |
||
478 | $attachment = new MailgunAttachment($event); |
||
479 | $attachment->setCounter(substr($key, 11)); |
||
480 | |||
481 | // get the file |
||
482 | /* @var UploadedFile $value */ |
||
483 | $attachment->setContent(file_get_contents($value->getPathname())); |
||
484 | $attachment->setSize($value->getSize()); |
||
485 | $attachment->setType($value->getMimeType()); |
||
486 | $attachment->setName($value->getFilename()); |
||
487 | $manager->persist($attachment); |
||
488 | } else { |
||
489 | // create custom-variables for event |
||
490 | $customVar = new MailgunCustomVariable($event); |
||
491 | $customVar->setVariableName($key); |
||
492 | $customVar->setContent($value); |
||
493 | $manager->persist($customVar); |
||
494 | } |
||
495 | } |
||
496 | |||
497 | // save all entities |
||
498 | $manager->flush(); |
||
499 | |||
500 | // Dispatch an event about the logging of a Webhook-call |
||
501 | $this->get('event_dispatcher')->dispatch(MailgunEvent::CREATE_EVENT, new MailgunWebhookEvent($event)); |
||
502 | } catch (\Exception $e) { |
||
503 | $this->container->get('logger')->warning('AzineMailgunWebhooksBundle: creating entities failed: '.$e->getMessage()); |
||
504 | $this->container->get('logger')->warning($e->getTraceAsString()); |
||
505 | |||
506 | return new Response('AzineMailgunWebhooksBundle: creating entities failed: '.$e->getMessage(), 500); |
||
507 | } |
||
508 | |||
509 | // send response |
||
510 | return new Response('Thanx, for the info.', 200); |
||
511 | } |
||
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.