MailgunController::eventIndexAction()   B
last analyzed

Complexity

Conditions 8
Paths 16

Size

Total Lines 97
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 67
nc 16
nop 3
dl 0
loc 97
ccs 0
cts 59
cp 0
crap 72
rs 7.4755
c 0
b 0
f 0

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\Controller;
4
5
use Azine\MailgunWebhooksBundle\DependencyInjection\AzineMailgunWebhooksExtension;
6
use Azine\MailgunWebhooksBundle\Entity\MailgunEvent;
7
use Azine\MailgunWebhooksBundle\Entity\MailgunMessageSummary;
8
use Azine\MailgunWebhooksBundle\Entity\Repositories\MailgunEventRepository;
9
use Azine\MailgunWebhooksBundle\Entity\Repositories\MailgunMessageSummaryRepository;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Mailgun controller.
16
 */
17
class MailgunController extends AbstractController
18
{
19
    /**
20
     * Show MailgunEvent-Overview.
21
     */
22
    public function eventOverviewAction()
23
    {
24
        $eventRepository = $this->getEventRepository();
25
        $params = array();
26
        $params['importantEvents'] = $eventRepository->getImportantEvents(10);
27
        $params['events'] = $eventRepository->getEventCount(array());
28
        $params['bounced'] = $eventRepository->getEventCount(array('eventType' => 'bounced'));
29
        $params['dropped'] = $eventRepository->getEventCount(array('eventType' => 'dropped'));
30
        $params['complained'] = $eventRepository->getEventCount(array('eventType' => 'complained'));
31
        $params['unsubscribed'] = $eventRepository->getEventCount(array('eventType' => 'unsubscribed'));
32
        $params['unopened'] = $eventRepository->getEventCount(array('eventType' => 'unopened'));
33
        $params['emailWebViewRoute'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_ROUTE);
0 ignored issues
show
Bug introduced by
The method getParameter() does not exist on Psr\Container\ContainerInterface. It seems like you code against a sub-type of Psr\Container\ContainerInterface such as Symfony\Component\Depend...tion\ContainerInterface. ( Ignorable by Annotation )

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

33
        /** @scrutinizer ignore-call */ 
34
        $params['emailWebViewRoute'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_ROUTE);
Loading history...
34
        $params['emailWebViewToken'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_TOKEN);
35
36
        return $this->render('AzineMailgunWebhooksBundle::overview.html.twig', $params);
37
    }
38
39
    /**
40
     * Lists all MailgunEvent entities.
41
     *
42
     * @param Request $request
43
     * @param int     $page
44
     * @param int     $pageSize
45
     *
46
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
47
     */
48
    public function eventIndexAction(Request $request, $page, $pageSize)
0 ignored issues
show
Unused Code introduced by
The parameter $pageSize is not used and could be removed. ( Ignorable by Annotation )

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

48
    public function eventIndexAction(Request $request, $page, /** @scrutinizer ignore-unused */ $pageSize)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $page is not used and could be removed. ( Ignorable by Annotation )

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

48
    public function eventIndexAction(Request $request, /** @scrutinizer ignore-unused */ $page, $pageSize)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
        $params = array();
51
        $eventRepository = $this->getEventRepository();
52
        // get general filter options
53
        $params['filterOptions'] = array(
54
            'orderBy' => $eventRepository->getFieldsToOrderBy(),
55
            'eventTypes' => $eventRepository->getEventTypes(),
56
            'domains' => $eventRepository->getDomains(),
57
            'recipients' => $eventRepository->getRecipients(),
58
        );
59
60
        if ($request->get('clear')) {
61
            $request->getSession()->remove('mailgunEventIndexParams');
62
        }
63
64
        // get filter criteria from session
65
        $sessionParams = $request->getSession()->get('mailgunEventIndexParams', array(
66
            'page' => 1,
67
            'pageSize' => 25,
68
            'domain' => '',
69
            'eventType' => 'all',
70
            'search' => '',
71
            'recipient' => '',
72
            'orderBy' => 'timestamp',
73
            'orderDirection' => 'desc',
74
        ));
75
76
        $page = $sessionParams['page'];
77
        $pageSize = $sessionParams['pageSize'];
78
        $domain = $sessionParams['domain'];
79
        $eventType = $sessionParams['eventType'];
80
        $search = $sessionParams['search'];
81
        $recipient = $sessionParams['recipient'];
82
        $orderBy = $sessionParams['orderBy'];
83
        $orderDirection = $sessionParams['orderDirection'];
84
85
        // update filter criteria from get-request
86
        if ($request->isMethod('GET')) {
87
            $page = $request->get('page', $sessionParams['page']);
88
            $pageSize = $request->get('pageSize', $sessionParams['pageSize']);
89
            if ($sessionParams['pageSize'] != $pageSize) {
90
                $page = 1;
91
            }
92
            $eventType = $request->get('eventType', 'all');
93
94
        // update filter criteria from post-request
95
        } elseif ($request->isMethod('POST') && is_array($request->get('filter'))) {
96
            $filter = $request->get('filter');
97
            $eventType = $filter['eventType'];
98
            $domain = $filter['domain'];
99
            $search = trim($filter['search']);
100
            $recipient = trim($filter['recipient']);
101
            $orderBy = $filter['orderBy'];
102
            $orderDirection = $filter['orderDirection'];
103
        }
104
105
        // set params for filter-form
106
        $currentFilter = array(
107
                'page' => $page,
108
                'pageSize' => $pageSize,
109
                'domain' => $domain,
110
                'eventType' => $eventType,
111
                'search' => $search,
112
                'recipient' => $recipient,
113
                'orderBy' => $orderBy,
114
                'orderDirection' => $orderDirection,
115
            );
116
        // store filter criteria back to session & to params
117
        $request->getSession()->set('mailgunEventIndexParams', $currentFilter);
118
        $params['currentFilters'] = $currentFilter;
119
120
        $eventCount = $eventRepository->getEventCount($currentFilter);
121
        // validate the page/pageSize and with the total number of result entries
122
        if ($eventCount > 0 && (($page - 1) * $pageSize >= $eventCount)) {
123
            $maxPage = max(1, ceil($eventCount / $pageSize));
124
125
            return $this->redirect($this->generateUrl('mailgunevent_list', array('page' => $maxPage, 'pageSize' => $pageSize)).'?'.$request->getQueryString());
126
        }
127
128
        // get the events
129
        $params['events'] = $eventRepository->getEvents($currentFilter, array($orderBy => $orderDirection), $pageSize, ($page - 1) * $pageSize);
130
        $params['emailWebViewRoute'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_ROUTE);
131
        $params['emailWebViewToken'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_TOKEN);
132
133
        // set the params for the pager
134
        $params['paginatorParams'] = array(
135
            'paginationPath' => 'mailgunevent_list',
136
            'pageSize' => $pageSize,
137
            'currentPage' => $page,
138
            'currentFilters' => $currentFilter,
139
            'totalItems' => $eventCount,
140
            'lastPage' => ceil($eventCount / $pageSize),
141
            'showAlwaysFirstAndLast' => true,
142
        );
143
144
        return $this->render('AzineMailgunWebhooksBundle:MailgunEvent:index.html.twig', $params);
145
    }
146
147
    /**
148
     * Finds and displays a MailgunEvent entity.
149
     */
150
    public function eventShowAction($id)
151
    {
152
        $em = $this->getDoctrine()->getManager();
153
154
        $entity = $em->getRepository('AzineMailgunWebhooksBundle:MailgunEvent')->find($id);
155
156
        if (!$entity) {
157
            throw $this->createNotFoundException('Unable to find MailgunEvent entity.');
158
        }
159
160
        return $this->render('AzineMailgunWebhooksBundle:MailgunEvent:show.html.twig', array(
161
            'emailWebViewRoute' => $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_ROUTE),
162
            'emailWebViewToken' => $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_TOKEN),
163
            'entity' => $entity,
164
        ));
165
    }
166
167
    /**
168
     * Deletes a MailgunEvent entity.
169
     */
170
    public function eventDeleteAction(Request $request)
171
    {
172
        $entity = $this->getEventRepository()->find($request->get('eventId'));
173
174
        if (!$entity) {
175
            throw $this->createNotFoundException('Unable to find MailgunEvent entity.');
176
        }
177
178
        $em = $this->getDoctrine()->getManager();
179
        $em->remove($entity);
180
        $em->flush();
181
182
        if ($request->isXmlHttpRequest()) {
183
            return new JsonResponse(array('success' => true));
184
        }
185
186
        $session = $request->getSession();
187
        $page = $session->get('page', 1);
188
        $pageSize = $session->get('pageSize', 25);
189
190
        return $this->redirect($this->generateUrl('mailgunevent_list', array('page' => $page, 'pageSize' => $pageSize)));
191
    }
192
193
    /**
194
     * @param Request $request
195
     * @param $page
196
     * @param $pageSize
197
     *
198
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|null
199
     */
200
    public function messageSummaryIndexAction(Request $request, $page, $pageSize)
0 ignored issues
show
Unused Code introduced by
The parameter $page is not used and could be removed. ( Ignorable by Annotation )

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

200
    public function messageSummaryIndexAction(Request $request, /** @scrutinizer ignore-unused */ $page, $pageSize)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pageSize is not used and could be removed. ( Ignorable by Annotation )

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

200
    public function messageSummaryIndexAction(Request $request, $page, /** @scrutinizer ignore-unused */ $pageSize)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
201
    {
202
        $params = array();
203
204
        $messageSummaryRepository = $this->getMessageSummaryRepository();
205
206
        // get general filter options
207
        $params['filterOptions'] = array(
208
            'orderBy' => $messageSummaryRepository->getFieldsToOrderBy(),
209
            'toAddress' => $messageSummaryRepository->getRecipients(),
210
            'fromAddress' => $messageSummaryRepository->getSenders(),
211
        );
212
213
        if ($request->get('clear')) {
214
            $request->getSession()->remove('mailgunMessageIndexParams');
215
        }
216
217
        // get filter criteria from session
218
        $sessionParams = $request->getSession()->get('mailgunMessageIndexParams', array(
219
            'page' => 1,
220
            'pageSize' => 25,
221
            'search' => '',
222
            'toAddress' => '',
223
            'fromAddress' => '',
224
            'orderBy' => 'sendDate',
225
            'orderDirection' => 'desc',
226
        ));
227
228
        $page = $sessionParams['page'];
229
        $pageSize = $sessionParams['pageSize'];
230
        $search = $sessionParams['search'];
231
        $toAddress = $sessionParams['toAddress'];
232
        $fromAddress = $sessionParams['fromAddress'];
233
        $orderBy = $sessionParams['orderBy'];
234
        $orderDirection = $sessionParams['orderDirection'];
235
236
        // update filter criteria from get-request
237
        if ($request->isMethod('GET')) {
238
            $page = $request->get('page', $sessionParams['page']);
239
            $pageSize = $request->get('pageSize', $sessionParams['pageSize']);
240
            if ($sessionParams['pageSize'] != $pageSize) {
241
                $page = 1;
242
            }
243
244
            // update filter criteria from post-request
245
        } elseif ($request->isMethod('POST') && is_array($request->get('filter'))) {
246
            $filter = $request->get('filter');
247
            $search = trim($filter['search']);
248
            $toAddress = trim($filter['toAddress']);
249
            $fromAddress = trim($filter['fromAddress']);
250
            $orderBy = $filter['orderBy'];
251
            $orderDirection = $filter['orderDirection'];
252
        }
253
254
        // set params for filter-form
255
        $currentFilter = array(
256
            'page' => $page,
257
            'pageSize' => $pageSize,
258
            'search' => $search,
259
            'toAddress' => $toAddress,
260
            'fromAddress' => $fromAddress,
261
            'orderBy' => $orderBy,
262
            'orderDirection' => $orderDirection,
263
        );
264
        // store filter criteria back to session & to params
265
        $request->getSession()->set('mailgunMessageIndexParams', $currentFilter);
266
        $params['currentFilters'] = $currentFilter;
267
268
        $messageSummaryCount = $messageSummaryRepository->getMessageSummaryCount($currentFilter);
269
        // validate the page/pageSize and with the total number of result entries
270
        if ($messageSummaryCount > 0 && (($page - 1) * $pageSize >= $messageSummaryCount)) {
271
            $maxPage = max(1, ceil($messageSummaryCount / $pageSize));
272
273
            return $this->redirect($this->generateUrl('mailgun_message_summary_list', array('page' => $maxPage, 'pageSize' => $pageSize)).'?'.$request->getQueryString());
274
        }
275
276
        // get the events
277
        $params['messageSummaries'] = $messageSummaryRepository->getMessageSummaries($currentFilter, array($orderBy => $orderDirection), $pageSize, ($page - 1) * $pageSize);
278
        $params['emailWebViewRoute'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_ROUTE);
279
        $params['emailWebViewToken'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_TOKEN);
280
281
        // set the params for the pager
282
        $params['paginatorParams'] = array(
283
            'paginationPath' => 'mailgun_message_summary_list',
284
            'pageSize' => $pageSize,
285
            'currentPage' => $page,
286
            'currentFilters' => $currentFilter,
287
            'totalItems' => $messageSummaryCount,
288
            'lastPage' => ceil($messageSummaryCount / $pageSize),
289
            'showAlwaysFirstAndLast' => true,
290
        );
291
292
        return $this->render('AzineMailgunWebhooksBundle::messageSummaryIndex.html.twig', $params);
293
    }
294
295
    public function messageSummaryShowAction(Request $request, MailgunMessageSummary $messageSummary)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

295
    public function messageSummaryShowAction(/** @scrutinizer ignore-unused */ Request $request, MailgunMessageSummary $messageSummary)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
296
    {
297
        $params = array('messageSummary' => $messageSummary);
298
        $params['emailWebViewRoute'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_ROUTE);
299
        $params['emailWebViewToken'] = $this->container->getParameter(AzineMailgunWebhooksExtension::PREFIX.'_'.AzineMailgunWebhooksExtension::WEB_VIEW_TOKEN);
300
301
        return $this->render('AzineMailgunWebhooksBundle::messageSummaryShow.html.twig', $params);
302
    }
303
304
    public function cockpitAction()
305
    {
306
        return $this->render('@AzineMailgunWebhooks/cockpit.html.twig', $this->get('azine_mailgun.cockpit_service')->getCockpitDataAsArray());
307
    }
308
309
    /**
310
     * Get the MailgunMessageSummary Repository.
311
     *
312
     * @return MailgunMessageSummaryRepository
313
     */
314
    private function getMessageSummaryRepository()
315
    {
316
        return $this->getDoctrine()->getManager()->getRepository(MailgunMessageSummary::class);
317
    }
318
319
    /**
320
     * Get the MailgunEvent Repository.
321
     *
322
     * @return MailgunEventRepository
323
     */
324
    private function getEventRepository()
325
    {
326
        return $this->getDoctrine()->getManager()->getRepository(MailgunEvent::class);
327
    }
328
329
    public function getMessageSummaryAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

329
    public function getMessageSummaryAction(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
330
    {
331
        $fromAddress = '';
332
        $toAddress = '';
333
        $sendTime = '';
334
        $subject = '';
335
        $summary = $this->getMessageSummaryRepository()->findSummary($fromAddress, $toAddress, $sendTime, $subject);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $summary is correct as $this->getMessageSummary...s, $sendTime, $subject) targeting Azine\MailgunWebhooksBun...pository::findSummary() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
336
337
        return new JsonResponse($summary);
338
    }
339
}
340