Completed
Push — master ( 02e991...6c3ee0 )
by Matthew
15:05 queued 12:47
created

AlertEndpointController::getHistoryByDate()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 63
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 40
nc 20
nop 2
dl 0
loc 63
rs 6.8825
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 Ps2alerts\Api\Controller\Endpoint\Alerts;
4
5
use Ps2alerts\Api\Controller\Endpoint\AbstractEndpointController;
6
use Ps2alerts\Api\Exception\InvalidArgumentException;
7
use Ps2alerts\Api\Repository\AlertRepository;
8
use Ps2alerts\Api\Transformer\AlertTransformer;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
class AlertEndpointController extends AbstractEndpointController
13
{
14
    /**
15
     * Construct
16
     *
17
     * @param AlertRepository  $repository
18
     * @param AlertTransformer $transformer
19
     */
20
    public function __construct(
21
        AlertRepository  $repository,
22
        AlertTransformer $transformer
23
    ) {
24
25
        $this->repository  = $repository;
26
        $this->transformer = $transformer;
27
    }
28
29
    /**
30
     * Returns a single alert's information
31
     *
32
     * @param  ServerRequestInterface  $request
33
     * @param  ResponseInterface       $response
34
     * @param  array                   $args
35
     *
36
     * @return ResponseInterface
37
     */
38
    public function getSingle(ServerRequestInterface $request, ResponseInterface $response, array $args)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from 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 $response is not used and could be removed.

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

Loading history...
39
    {
40
        $alert = $this->repository->readSingleById($args['id']);
41
42
        if (empty($alert)) {
43
            return $this->respondWithError('Alert not found', self::CODE_NOT_FOUND);
44
        }
45
46
        return $this->respond(
47
            'item',
48
            $alert,
49
            $this->transformer
50
        );
51
    }
52
53
    /**
54
     * Returns all currently running alerts
55
     *
56
     * @param  ServerRequestInterface  $request
57
     * @param  ResponseInterface       $response
58
     *
59
     * @return ResponseInterface
60
     */
61
    public function getActives(ServerRequestInterface $request, ResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from 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 $response is not used and could be removed.

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

Loading history...
62
    {
63
        $actives = $this->repository->readAllByFields(['InProgress' => 1]);
64
65
        return $this->respond(
66
            'collection',
67
            $actives,
68
            $this->transformer
69
        );
70
    }
71
72
    /**
73
     * Returns all alerts in historical order
74
     *
75
     * @param  ServerRequestInterface  $request
76
     * @param  ResponseInterface $response
77
     *
78
     * @return ResponseInterface
79
     */
80
    public function getByDate(ServerRequestInterface $request, ResponseInterface $response)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from 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 $response is not used and could be removed.

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

Loading history...
Coding Style introduced by
getByDate uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
81
    {
82
        try {
83
            $servers  = $this->validateQueryStringArguments($_GET['servers'], 'servers');
84
            $zones    = $this->validateQueryStringArguments($_GET['zones'], 'zones');
85
            $factions = $this->validateQueryStringArguments($_GET['factions'], 'factions');
86
            $brackets = $this->validateQueryStringArguments($_GET['brackets'], 'brackets');
87
            $dates    = $this->validateQueryStringArguments($_GET['dates'], 'dates');
88
        } catch (InvalidArgumentException $e) {
89
            return $this->respondWithError($e->getMessage(), self::CODE_WRONG_ARGS);
90
        }
91
92
        $offset = (int) $_GET['offset'];
93
        $limit  = (int) $_GET['limit'];
94
95
        // Set defaults if not supplied
96
        if (empty($offset) || ! is_numeric($offset)) {
97
            $offset = 0;
98
        }
99
100
        if (empty($limit) || ! is_numeric($limit)) {
101
            $limit = 50;
102
        }
103
104
        // Check the date difference between two dates. we don't want to run queries for ALL OF ZE ALERTS NOW do we?!
105
        if (! empty($dates)) {
106
            try {
107
                $this->getDateValidationUtility()->validateTimeDifference($dates, 180); // Allow half a year
108
            } catch (InvalidArgumentException $e) {
109
                return $this->respondWithError($e->getMessage(), self::CODE_WRONG_ARGS);
110
            }
111
112
            $limit = null;
113
        }
114
115
        $query = $this->repository->newQuery();
116
117
        $brackets = $this->convertStringToArrayForAuraBinds($brackets);
118
        $brackets[] = 'UNK';
119
120
        $query->cols(['*']);
121
        $query->where('ResultServer IN (?)', $this->convertStringToArrayForAuraBinds($servers));
122
        $query->where('ResultAlertCont IN (?)', $this->convertStringToArrayForAuraBinds($zones));
123
        $query->where('ResultWinner IN (?)', $this->convertStringToArrayForAuraBinds($factions));
124
        $query->where('ResultTimeType IN (?)', $brackets);
125
126
        if (! empty($dates)) {
127
            $this->addDateRangeWhereClause($dates, $query);
128
        }
129
130
        $query->orderBy(["ResultEndTime DESC"]);
131
        if ($limit) {
132
            $query->limit($limit);
133
        }
134
        $query->offset($offset);
135
136
        $history = $this->repository->fireStatementAndReturn($query);
137
138
        return $this->respond(
139
            'collection',
140
            $history,
141
            $this->transformer
142
        );
143
    }
144
}
145