| Conditions | 1 |
| Paths | 1 |
| Total Lines | 61 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
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 |
||
| 49 | private static function addMailgunEvent(EntityManager $manager, $mailgunApiKey, $eventType, $messageId, $count){ |
||
| 50 | $mailgunEvent = new MailgunEvent(); |
||
| 51 | $mailgunEvent->setEvent($eventType); |
||
| 52 | $mailgunEvent->setDomain('acme'); |
||
| 53 | $d = new \DateTime(rand(1, 200).' days '.rand(1, 86400).' seconds ago'); |
||
| 54 | $mailgunEvent->setTimestamp($d->getTimestamp()); |
||
| 55 | $mailgunEvent->setToken(md5(time().$count)); |
||
| 56 | $mailgunEvent->setRecipient('recipient-'.$count.'@email.com'); |
||
| 57 | $mailgunEvent->setSender('some-sender-'.$count.'@email.com'); |
||
| 58 | $mailgunEvent->setMessageHeaders(json_encode(array('some_json' => 'data', 'Subject' => "this mail was sent because it's important."))); |
||
| 59 | $mailgunEvent->setMessageId($messageId); |
||
| 60 | $mailgunEvent->setSignature(hash_hmac('SHA256', $mailgunEvent->getTimestamp().$mailgunEvent->getToken(), $mailgunApiKey)); |
||
| 61 | $mailgunEvent->setDescription('some description'); |
||
| 62 | $mailgunEvent->setReason('don\'t know the reason'); |
||
| 63 | $mailgunEvent->setIp('42.42.42.42'); |
||
| 64 | $mailgunEvent->setErrorCode('123'); |
||
| 65 | $mailgunEvent->setCountry('CH'); |
||
| 66 | $mailgunEvent->setCity('Zurich'); |
||
| 67 | $mailgunEvent->setRegion('8000'); |
||
| 68 | $mailgunEvent->setCampaignId('2014-01-01'); |
||
| 69 | $mailgunEvent->setCampaignName('newsletter'); |
||
| 70 | $mailgunEvent->setClientName('some client'); |
||
| 71 | $mailgunEvent->setClientOs('some os'); |
||
| 72 | $mailgunEvent->setClientType('some type'); |
||
| 73 | $mailgunEvent->setDeviceType('some device'); |
||
| 74 | $mailgunEvent->setMailingList('no list'); |
||
| 75 | $mailgunEvent->setTag('hmmm no tag'); |
||
| 76 | $mailgunEvent->setUserAgent('Firefox 42'); |
||
| 77 | $mailgunEvent->setUrl(''); |
||
| 78 | $manager->persist($mailgunEvent); |
||
| 79 | |||
| 80 | $file = new UploadedFile(realpath(__DIR__.'/testAttachment.small.png'), 'some.real.file.name1.png'); |
||
| 81 | $attachment = new MailgunAttachment($mailgunEvent); |
||
| 82 | $attachment->setContent(file_get_contents($file->getRealPath())); |
||
| 83 | $attachment->setName(md5(time() + rand(0, 100)).'.'.$file->getClientOriginalExtension()); |
||
| 84 | $attachment->setSize($file->getSize()); |
||
| 85 | $attachment->setType($file->getType()); |
||
| 86 | $attachment->setCounter(1); |
||
| 87 | $manager->persist($attachment); |
||
| 88 | |||
| 89 | $attachment = new MailgunAttachment($mailgunEvent); |
||
| 90 | $attachment->setContent(file_get_contents($file->getRealPath())); |
||
| 91 | $attachment->setName(md5(time() + rand(0, 100)).'.'.$file->getClientOriginalExtension()); |
||
| 92 | $attachment->setSize($file->getSize()); |
||
| 93 | $attachment->setType($file->getType()); |
||
| 94 | $attachment->setCounter(2); |
||
| 95 | $manager->persist($attachment); |
||
| 96 | |||
| 97 | $variable = new MailgunCustomVariable($mailgunEvent); |
||
| 98 | $variable->setEventId($mailgunEvent->getId()); |
||
| 99 | $variable->setContent(array('some data1')); |
||
| 100 | $variable->setVariableName('some custom variable for event'.$mailgunEvent->getId()); |
||
| 101 | $manager->persist($variable); |
||
| 102 | |||
| 103 | $variable = new MailgunCustomVariable($mailgunEvent); |
||
| 104 | $variable->setEventId($mailgunEvent->getId()); |
||
| 105 | $variable->setContent(array('some data2')); |
||
| 106 | $variable->setVariableName('some custom variable for event'.$mailgunEvent->getId()); |
||
| 107 | $manager->persist($variable); |
||
| 108 | |||
| 109 | return $mailgunEvent; |
||
| 110 | } |
||
| 234 |