Completed
Pull Request — master (#22)
by Matthew
06:23 queued 04:03
created

LeaderboardWeaponEndpointController::weapons()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 25
nc 7
nop 0
dl 0
loc 46
rs 8.4751
c 0
b 0
f 0
1
<?php
2
3
// @todo Go over this ENTIRE file again as there's been major refactors since and it's likely broken!
4
5
namespace Ps2alerts\Api\Controller\Endpoint\Leaderboards;
6
7
use Ps2alerts\Api\Repository\Metrics\WeaponTotalRepository;
8
use Ps2alerts\Api\Transformer\Leaderboards\WeaponLeaderboardTransformer;
9
use Psr\Http\Message\ResponseInterface;
10
11
class LeaderboardWeaponEndpointController extends AbstractLeaderboardEndpointController
12
{
13
    /**
14
     * Construct
15
     *
16
     * @param WeaponTotalRepository $repository
17
     */
18
    public function __construct(
19
        WeaponTotalRepository  $repository
20
    ) {
21
        $this->repository = $repository;
22
    }
23
24
    /**
25
     * Get Weapon Leaderboard
26
     *
27
     * @return ResponseInterface
28
     */
29
    public function weapons()
0 ignored issues
show
Coding Style introduced by
weapons 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...
30
    {
31
        $valid = $this->validateRequestVars();
32
33
        // If validation didn't pass, chuck 'em out
34
        if ($valid !== true) {
35
            return $this->respondWithError($valid->getMessage(), self::CODE_WRONG_ARGS);
36
        }
37
38
        // Translate field into table specific columns
39
        if (isset($_GET['field'])) {
40
            $field = $this->getField($_GET['field']);
41
        }
42
43
        if (! isset($field)) {
44
            return $this->respondWithError('Field wasn\'t provided and is required.', self::CODE_WRONG_ARGS);
45
        }
46
47
        $weapons = $this->getRedisUtility()->checkRedis('api', 'leaderboards', "weapons:{$field}");
48
49
        // If we have this cached already
50
        if (empty($weapons)) {
51
            // Perform Query
52
            $query = $this->repository->newQuery();
53
            $query->cols([
54
                'weaponID',
55
                'SUM(killCount) as killCount',
56
                'SUM(teamkills) as teamkills',
57
                'SUM(headshots) as headshots'
58
            ]);
59
            $query->where('weaponID > 0');
60
            $query->orderBy(["{$field} desc"]);
61
            $query->groupBy(['weaponID']);
62
63
            $weapons = $this->repository->fireStatementAndReturn($query);
64
65
            // Cache results in redis
66
            $this->getRedisUtility()->storeInRedis('api', 'leaderboards', "weapons:{$field}", $weapons);
0 ignored issues
show
Documentation introduced by
$weapons is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
        }
68
69
        return $this->respond(
70
            'collection',
71
            $weapons,
72
            new WeaponLeaderboardTransformer
73
        );
74
    }
75
76
    /**
77
     * Gets the appropriate field for the table and handles some table naming oddities
78
     *
79
     * @param  string $input Field to look at
80
     * @return string
81
     */
82
    public function getField($input) {
83
        $field = null;
84
        switch ($input) {
85
            case 'kills':
86
                $field = 'killCount';
87
                break;
88
            case 'headshots':
89
                $field = 'headshots';
90
                break;
91
            case 'teamkills':
92
                $field = 'teamkills';
93
                break;
94
        }
95
96
        return $field;
97
    }
98
}
99