Completed
Push — master ( ff6361...375f0f )
by Matthew
04:31
created

AlertEndpointController::getHistoryByDate()   B

Complexity

Conditions 8
Paths 20

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 3
Metric Value
c 4
b 1
f 3
dl 0
loc 54
rs 7.4119
cc 8
eloc 35
nc 20
nop 2

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 Ps2alerts\Api\Controller\Endpoint\Alerts;
4
5
use League\Fractal\Manager;
6
use Ps2alerts\Api\Controller\Endpoint\AbstractEndpointController;
7
use Ps2alerts\Api\Repository\AlertRepository;
8
use Ps2alerts\Api\Transformer\AlertTotalTransformer;
9
use Ps2alerts\Api\Transformer\AlertTransformer;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class AlertEndpointController extends AbstractEndpointController
14
{
15
    /**
16
     * Construct
17
     *
18
     * @param Ps2alerts\Api\Repository\AlertRepository   $repository
19
     * @param Ps2alerts\Api\Transformer\AlertTransformer $transformer
20
     * @param League\Fractal\Manager                     $fractal
21
     */
22
    public function __construct(
23
        AlertRepository  $repository,
24
        AlertTransformer $transformer,
25
        Manager          $fractal
26
    ) {
27
        $this->repository  = $repository;
28
        $this->transformer = $transformer;
29
        $this->fractal     = $fractal;
30
    }
31
32
    /**
33
     * Returns a single alert's information
34
     *
35
     * @param  Symfony\Component\HttpFoundation\Request  $request
36
     * @param  Symfony\Component\HttpFoundation\Response $response
37
     * @param  array                                     $args
38
     *
39
     * @return \League\Fractal\TransformerAbstract
40
     */
41 View Code Duplication
    public function getSingle(Request $request, Response $response, array $args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
    {
43
        $alert = $this->repository->readSingleById($args['id']);
44
45
        if (empty($alert)) {
46
            return $this->errorEmpty($response);
47
        }
48
49
        return $this->respond('item', $alert, $this->transformer, $request, $response);
50
    }
51
52
    /**
53
     * Returns all currently running alerts
54
     *
55
     * @param  Symfony\Component\HttpFoundation\Request  $request
56
     * @param  Symfony\Component\HttpFoundation\Response $response
57
     *
58
     * @return \League\Fractal\TransformerAbstract
59
     */
60 View Code Duplication
    public function getActives(Request $request, Response $response)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        $actives = $this->repository->readAllByFields(['InProgress' => 1]);
63
64
        if (empty($actives)) {
65
            return $this->errorEmpty($response);
66
        }
67
68
        return $this->respond('collection', $actives, $this->transformer, $request, $response);
69
    }
70
71
    /**
72
     * Returns all alerts in historial order
73
     *
74
     * @param  Symfony\Component\HttpFoundation\Request  $request
75
     * @param  Symfony\Component\HttpFoundation\Response $response
76
     *
77
     * @return \League\Fractal\TransformerAbstract
78
     */
79
    public function getHistoryByDate(Request $request, Response $response)
80
    {
81
        try {
82
            $servers  = $this->getFiltersFromQueryString($request->get('servers'), 'servers', $response);
83
            $zones    = $this->getFiltersFromQueryString($request->get('zones'), 'zones', $response);
84
            $factions = $this->getFiltersFromQueryString($request->get('factions'), 'factions', $response);
85
            $brackets = $this->getFiltersFromQueryString($request->get('brackets'), 'brackets', $response);
86
        } catch (InvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Ps2alerts\Api\Controller...nvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
87
            return $this->errorWrongArgs($response, $e->getMessage());
88
        }
89
90
        $dateFrom = $request->get('dateFrom');
91
        $dateTo   = $request->get('dateTo');
92
        $offset   = (int) $request->get('offset');
93
        $limit    = (int) $request->get('limit');
94
95
        // Set defaults if not supplied
96
        if ($offset === null || ! is_numeric($offset)) {
97
            $offset = 0;
98
        }
99
100
        if ($limit === null || ! is_numeric($limit)) {
101
            $limit = 25;
102
        }
103
104
        if ($dateFrom === null) {
105
            $dateFrom = date('Y-m-d H:i:s', strtotime('-48 hours'));
106
        }
107
108
        if ($dateTo === null) {
109
            $dateTo = date('Y-m-d H:i:s', strtotime('now'));
110
        }
111
112
        // Format the dates into UNIX timestamp for use with the DB
113
        $dateFrom = date('Y-m-d H:i:s', strtotime($dateFrom));
114
        $dateTo   = date('Y-m-d H:i:s', strtotime($dateTo));
115
116
        $query = $this->repository->newQuery();
117
118
        $query->cols(['*']);
119
        $query->where("`ResultServer` IN ({$servers})");
120
        $query->where("`ResultAlertCont` IN ({$zones})");
121
        $query->where("`ResultDateTime` > '{$dateFrom}'");
122
        $query->where("`ResultDateTime` < '{$dateTo}'");
123
        $query->where("`ResultWinner` IN ({$factions})");
124
        $query->where("`ResultTimeType` IN ({$brackets})");
125
        $query->orderBy(["`ResultEndTime` DESC"]);
1 ignored issue
show
Bug introduced by
The method orderBy() does not exist on Aura\SqlQuery\AbstractQuery. Did you maybe mean addOrderBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
126
        $query->limit($limit);
127
        $query->offset($offset);
128
129
        $history = $this->repository->readRaw($query->getStatement());
130
131
        return $this->respond('collection', $history, $this->transformer, $request, $response);
132
    }
133
}
134