Completed
Push — master ( 5e6003...ff6361 )
by Matthew
03:07
created

AlertCountsEndpointController::getDominations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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\Contract\ConfigAwareInterface;
8
use Ps2alerts\Api\Contract\ConfigAwareTrait;
9
use Ps2alerts\Api\Repository\AlertRepository;
10
use Ps2alerts\Api\Transformer\AlertTotalTransformer;
11
use Ps2alerts\Api\Transformer\AlertTransformer;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class AlertCountsEndpointController extends AbstractEndpointController implements
16
    ConfigAwareInterface
17
{
18
    use ConfigAwareTrait;
19
20
    /**
21
     * Construct
22
     *
23
     * @param Ps2alerts\Api\Repository\AlertRepository   $repository
24
     * @param Ps2alerts\Api\Transformer\AlertTransformer $transformer
25
     * @param League\Fractal\Manager                     $fractal
26
     */
27
    public function __construct(
28
        AlertRepository  $repository,
29
        AlertTransformer $transformer,
30
        Manager          $fractal
31
    ) {
32
        $this->repository  = $repository;
33
        $this->transformer = $transformer;
34
        $this->fractal     = $fractal;
35
    }
36
37
    /**
38
     * Returns the victories of each faction and the totals
39
     *
40
     * @param  Symfony\Component\HttpFoundation\Request  $request
41
     * @param  Symfony\Component\HttpFoundation\Response $response
42
     *
43
     * @return array
44
     */
45
    public function getVictories(Request $request, Response $response)
46
    {
47
        return $this->getCountData($request, $response, 'victories');
48
    }
49
50
    /**
51
     * Returns the dominations of each faction and the totals
52
     *
53
     * @param  \Symfony\Component\HttpFoundation\Request  $request
54
     * @param  \Symfony\Component\HttpFoundation\Response $response
55
     *
56
     * @return array
57
     */
58
    public function getDominations(Request $request, Response $response)
59
    {
60
        return $this->getCountData($request, $response, 'dominations');
61
    }
62
63
    /**
64
     * Gets the required count data and returns
65
     *
66
     * @param  \Symfony\Component\HttpFoundation\Request  $request
67
     * @param  \Symfony\Component\HttpFoundation\Response $response
68
     * @param  string                                     $mode     The type of data we're getting (victory / domination)
69
     *
70
     * @return \Symfony\Component\HttpFoundation\Response
71
     */
72
    public function getCountData(Request $request, Response $response, $mode)
73
    {
74
        $serversQuery = $request->get('servers');
75
        $zonesQuery   = $request->get('zones');
76
        $servers      = $this->getConfigItem('servers');
77
        $zones        = $this->getConfigItem('zones');
78
79 View Code Duplication
        if (! empty($serversQuery)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80
            $check = explode(',', $serversQuery);
81
82
            // Run a check on the IDs provided to make sure they're valid and no naughty things are being passed
83
            foreach ($check as $id) {
84
                if (! in_array($id, $servers)) {
85
                    return $this->errorWrongArgs($response, 'Invalid Server Arguments passed');
86
                }
87
            }
88
89
            $servers = $serversQuery;
90
        } else {
91
            $servers = implode(',', $servers);
92
        }
93
94 View Code Duplication
        if (! empty($zonesQuery)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
95
            $check = explode(',', $zonesQuery);
96
97
            // Run a check on the IDs provided to make sure they're valid and no naughty things are being passed
98
            foreach ($check as $id) {
99
                if (! in_array($id, $zones)) {
100
                    return $this->errorWrongArgs($response, 'Invalid Zone Arguments passed');
101
                }
102
            }
103
104
            $zones = $zonesQuery;
105
        } else {
106
            $zones = implode(',', $zones);
107
        }
108
109
        $counts = [];
110
        $serversExploded = explode(',', $servers);
111
112
        foreach ($serversExploded as $server) {
113
            $query = $this->repository->newQuery();
114
115
            $sql = '';
116
117
            foreach ($this->getConfigItem('factions') as $faction) {
0 ignored issues
show
Bug introduced by
The expression $this->getConfigItem('factions') of type string is not traversable.
Loading history...
118
                $factionAbv = strtoupper($faction);
119
                $sql .= "SUM(CASE WHEN `ResultWinner`='{$factionAbv}' ";
120
                $sql .= "AND `ResultServer` IN ({$server}) ";
121
                $sql .= "AND `ResultAlertCont` IN ({$zones}) ";
122
123
                if ($mode === 'dominations') {
124
                    $sql .= "AND `ResultDomination` = 1 ";
125
                }
126
127
                $sql .= "THEN 1 ELSE 0 END) {$faction}";
128
129
                if ($factionAbv !== 'DRAW') {
130
                    $sql .= ", ";
131
                }
132
            }
133
134
            $query->cols([$sql]);
135
            $data = $this->repository->readRaw($query->getStatement(), true);
136
            $data['total'] = array_sum($data);
137
138
            if ($mode === 'domination') {
139
                $data['draw'] = null; // For dominations, set it by default first
140
            }
141
142
            // Build each section of the final response using the transformer
143
            $counts['data'][$server] = $this->createItem($data, new AlertTotalTransformer);
144
        }
145
146
        // Return the now formatted array to the response
147
        return $this->respondWithArray($response, $counts);
148
    }
149
}
150