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

AlertEndpointController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 121
Duplicated Lines 16.53 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 1 Features 3
Metric Value
wmc 13
c 5
b 1
f 3
lcom 1
cbo 4
dl 20
loc 121
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSingle() 10 10 2
A getActives() 10 10 2
B getHistoryByDate() 0 54 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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