Passed
Push — master ( 2f8638...569786 )
by Dominik
25:17
created

TestHelper::addMailgunEvents()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 66
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 5 Features 2
Metric Value
cc 2
eloc 58
c 7
b 5
f 2
nc 2
nop 3
dl 0
loc 66
rs 8.9163

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Azine\MailgunWebhooksBundle\Tests;
4
5
use Azine\MailgunWebhooksBundle\Entity\MailgunAttachment;
6
use Azine\MailgunWebhooksBundle\Entity\MailgunCustomVariable;
7
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent;
8
use Doctrine\ORM\EntityManager;
9
use Symfony\Component\HttpFoundation\File\UploadedFile;
10
11
class TestHelper
12
{
13
    /**
14
     * @param int $count
15
     */
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());
0 ignored issues
show
Bug introduced by
'some custom variable for event' . $e->getId() of type string is incompatible with the type integer expected by parameter $variableName of Azine\MailgunWebhooksBun...able::setVariableName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $variable->setVariableName(/** @scrutinizer ignore-type */ 'some custom variable for event'.$e->getId());
Loading history...
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
    }
83
84
    public static function getPostDataWithoutSignature($newApi)
85
    {
86
        if($newApi){
87
            $timestamp = time();
88
            $data = array(
89
                'signature' => array (
90
                    'timestamp' => $timestamp,
91
                    'token' => '50dcec4a2d0ef27036c44ebd9ce9324736fc98ef9405428803',
92
                ),
93
                'event-data' =>
94
                    array(
95
                        'tags' =>
96
                            array(
97
                                0 => 'my_tag_1',
98
                                1 => 'my_tag_2'
99
                            ),
100
                        'timestamp' => $timestamp,
101
                        'storage' =>
102
                            array(
103
                                'url' => 'https://se.api.mailgun.net/v3/domains/acme.com/messages/message_key',
104
                                'key' => 'message_key',
105
                            ),
106
                        'envelope' =>
107
                            array(
108
                                'transport' => 'smtp',
109
                                'sender' => '[email protected]',
110
                                'sending-ip' => '209.61.154.250',
111
                                'targets' => '[email protected]',
112
                            ),
113
                        'recipient-domain' => 'example.com',
114
                        'event' => 'delivered',
115
                        'campaigns' =>
116
                            array(),
117
                        'user-variables' =>
118
                            array(
119
                                'my_var_1' => 'Mailgun Variable #1',
120
                                'my-var-2' => 'awesome',
121
                            ),
122
                        'flags' =>
123
                            array('is-routed' => false, 'is-authenticated' => true, 'is-system-test' => false, 'is-test-mode' => false,
124
                            ),
125
                        'log-level' => 'info',
126
                        'message' =>
127
                            array(
128
                                'headers' =>
129
                                    array(
130
                                        'to' => 'Alice <[email protected]>',
131
                                        'message-id' => '[email protected]',
132
                                        'from' => 'Bob <[email protected]>',
133
                                        'subject' => 'Test delivered webhook',
134
                                    ),
135
                                'attachments' => array(),
136
                                'size' => 111,
137
                            ),
138
                        'recipient' => '[email protected]',
139
                        'id' => 'CPgfbmQMTCKtHW6uIWtuVe',
140
                        'delivery-status' =>
141
                            array(
142
                                'tls' => true,
143
                                'mx-host' => 'smtp-in.example.com',
144
                                'attempt-no' => 1,
145
                                'description' => '',
146
                                'session-seconds' => 0.4331989288330078,
147
                                'utf8' => true,
148
                                'code' => 250,
149
                                'message' => 'OK',
150
                                'certificate-verified' => true,
151
                            ),
152
                    ),
153
            );
154
        } else {
155
            $data = array(
156
                'event' => 'delivered',
157
                'domain' => 'acme',
158
                'timestamp' => time(),
159
                'token' => 'c47468e81de0818af77f3e14a728602a29',
160
                'X-Mailgun-Sid' => 'irrelevant',
161
                'attachment-count' => 'irrelevant',
162
                'recipient' => '[email protected]',
163
                'message-headers' => json_encode(array(
164
                    array('X-Mailgun-Sending-Ip', '198.62.234.37'),
165
                    array('X-Mailgun-Sid', 'WyIwN2U4YyIsICJzdXBwb3J0QGF6aW5lLm1lIiwgIjA2MjkzIl0='),
166
                    array('Received', 'from acme.test (b4.cme.test [194.140.238.63])'),
167
                    array('Sender', '[email protected]'),
168
                    array('Message-Id', '<[email protected]>'),
169
                    array('Date', 'Mon, 07 Sep 2020 14:38:41 +0200'),
170
                    array('Subject', 'Some email message subject'),
171
                    array('From', '\'acme.test sender-name\' <[email protected]>'),
172
                    array('To', '\'acme.test recipient-name\' <[email protected]>'),
173
                    array('Mime-Version', '1.0'),
174
                    array('Content-Transfer-Encoding', '[\'quoted-printable\']')
175
                )),
176
                'Message-Id' => '<[email protected]>',
177
                'description' => 'some description',
178
                'notification' => 'some notification',
179
                'reason' => 'don\'t know the reason',
180
                'code' => 123,
181
                'ip' => '42.42.42.42',
182
                'error' => 'some error',
183
                'country' => 'CH',
184
                'city' => 'Zurich',
185
                'region' => '8000',
186
                'campaign-id' => '2014-01-01',
187
                'campaign-name' => 'newsletter',
188
                'client-name' => 'some client',
189
                'client-os' => 'some os',
190
                'client-type' => 'some type',
191
                'device-type' => 'some device',
192
                'mailing-list' => 'no list',
193
                'tag' => 'hmmm no tag',
194
                'user-agent' => 'Firefox 42',
195
                'url' => '',
196
                'duplicate-key' => 'data1',
197
                'Duplicate-key' => 'data2',
198
                'some-custom-var1' => 'some data1',
199
                'some-custom-var2' => 'some data2',
200
                'some-custom-var3' => 'some data3',
201
            );
202
        }
203
        return $data;
204
    }
205
}
206