| Conditions | 2 |
| Paths | 2 |
| Total Lines | 66 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 5 | Features | 2 |
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 |
||
| 16 | public static function addMailgunEvents(EntityManager $manager, $count, $mailgunApiKey) |
||
| 17 | { |
||
| 18 | $eventTypes = array('delivered', 'bounced', 'opened', 'dropped'); |
||
| 19 | while ($count > 0) { |
||
| 20 | $e = new MailgunEvent(); |
||
| 21 | $e->setEvent($eventTypes[rand(0, sizeof($eventTypes) - 1)]); |
||
| 22 | $e->setDomain('acme'); |
||
| 23 | $d = new \DateTime(rand(1, 200).' days '.rand(1, 86400).' seconds ago'); |
||
| 24 | $e->setTimestamp($d->getTimestamp()); |
||
| 25 | $e->setToken(md5(time().$count)); |
||
| 26 | $e->setRecipient('recipient-'.$count.'@email.com'); |
||
| 27 | $e->setSender('some-sender-'.$count.'@email.com'); |
||
| 28 | $e->setMessageHeaders(json_encode(array('some_json' => 'data', 'Subject' => "this mail was sent because it's important."))); |
||
| 29 | $e->setMessageId('<'.md5(time()).$count.'@acme.com>'); |
||
| 30 | $e->setSignature(hash_hmac('SHA256', $e->getTimestamp().$e->getToken(), $mailgunApiKey)); |
||
| 31 | $e->setDescription('some description'); |
||
| 32 | $e->setReason('don\'t know the reason'); |
||
| 33 | $e->setIp('42.42.42.42'); |
||
| 34 | $e->setErrorCode('123'); |
||
| 35 | $e->setCountry('CH'); |
||
| 36 | $e->setCity('Zurich'); |
||
| 37 | $e->setRegion('8000'); |
||
| 38 | $e->setCampaignId('2014-01-01'); |
||
| 39 | $e->setCampaignName('newsletter'); |
||
| 40 | $e->setClientName('some client'); |
||
| 41 | $e->setClientOs('some os'); |
||
| 42 | $e->setClientType('some type'); |
||
| 43 | $e->setDeviceType('some device'); |
||
| 44 | $e->setMailingList('no list'); |
||
| 45 | $e->setTag('hmmm no tag'); |
||
| 46 | $e->setUserAgent('Firefox 42'); |
||
| 47 | $e->setUrl(''); |
||
| 48 | $manager->persist($e); |
||
| 49 | |||
| 50 | $file = new UploadedFile(realpath(__DIR__.'/testAttachment.small.png'), 'some.real.file.name1.png'); |
||
| 51 | $attachment = new MailgunAttachment($e); |
||
| 52 | $attachment->setContent(file_get_contents($file->getRealPath())); |
||
| 53 | $attachment->setName(md5(time() + rand(0, 100)).'.'.$file->getClientOriginalExtension()); |
||
| 54 | $attachment->setSize($file->getSize()); |
||
| 55 | $attachment->setType($file->getType()); |
||
| 56 | $attachment->setCounter(1); |
||
| 57 | $manager->persist($attachment); |
||
| 58 | |||
| 59 | $attachment = new MailgunAttachment($e); |
||
| 60 | $attachment->setContent(file_get_contents($file->getRealPath())); |
||
| 61 | $attachment->setName(md5(time() + rand(0, 100)).'.'.$file->getClientOriginalExtension()); |
||
| 62 | $attachment->setSize($file->getSize()); |
||
| 63 | $attachment->setType($file->getType()); |
||
| 64 | $attachment->setCounter(2); |
||
| 65 | $manager->persist($attachment); |
||
| 66 | |||
| 67 | $variable = new MailgunCustomVariable($e); |
||
| 68 | $variable->setEventId($e->getId()); |
||
| 69 | $variable->setContent(array('some data1')); |
||
| 70 | $variable->setVariableName('some custom variable for event'.$e->getId()); |
||
|
|
|||
| 71 | $manager->persist($variable); |
||
| 72 | |||
| 73 | $variable = new MailgunCustomVariable($e); |
||
| 74 | $variable->setEventId($e->getId()); |
||
| 75 | $variable->setContent(array('some data2')); |
||
| 76 | $variable->setVariableName('some custom variable for event'.$e->getId()); |
||
| 77 | $manager->persist($variable); |
||
| 78 | |||
| 79 | --$count; |
||
| 80 | } |
||
| 81 | $manager->flush(); |
||
| 82 | } |
||
| 206 |