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

LeaderboardOutfitEndpointController::outfits()   C

Complexity

Conditions 7
Paths 19

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 19
nop 0
dl 0
loc 49
rs 6.7272
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\OutfitTotalRepository;
8
use Ps2alerts\Api\Transformer\Leaderboards\OutfitLeaderboardTransformer;
9
use Psr\Http\Message\ResponseInterface;
10
11
class LeaderboardOutfitEndpointController extends AbstractLeaderboardEndpointController
12
{
13
    protected $repository;
14
15
    /**
16
     * Construct
17
     *
18
     * @param League\Fractal\Manager $fractal
0 ignored issues
show
Bug introduced by
There is no parameter named $fractal. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
19
     */
20
    public function __construct(
21
        OutfitTotalRepository  $repository
22
    ) {
23
        $this->repository = $repository;
24
    }
25
26
    /**
27
     * Get Outfit Leaderboard
28
     *
29
     * @return ResponseInterface
30
     */
31
    public function outfits()
0 ignored issues
show
Coding Style introduced by
outfits 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...
32
    {
33
        $valid = $this->validateRequestVars();
34
35
        // If validation didn't pass, chuck 'em out
36
        if ($valid !== true) {
37
            return $this->respondWithError($valid->getMessage(), self::CODE_WRONG_ARGS);
38
        }
39
40
        $server = $_GET['server'];
41
        $limit  = $_GET['limit'];
42
        $offset = $_GET['offset'];
43
44
        // Translate field into table specific columns
45
46
        if (isset($_GET['field'])) {
47
            $field = $this->getField($_GET['field']);
48
        }
49
50
        if (! isset($field)) {
51
            return $this->respondWithError('Field wasn\'t provided and is required.', self::CODE_WRONG_ARGS);
52
        }
53
54
        // Perform Query
55
        $query = $this->repository->newQuery();
56
        $query->cols(['*']);
57
        $query->orderBy(["{$field} desc"]);
58
        $query->where('outfitID > 0');
59
60
        if (isset($server)) {
61
            $query->where('outfitServer = ?', $server);
62
        }
63
64
        if (isset($limit)) {
65
            $query->limit($limit);
66
        } else {
67
            $query->limit(10); // Set default limit
68
        }
69
70
        if (isset($offset)) {
71
            $query->offset($offset);
72
        }
73
74
        return $this->respond(
75
            'collection',
76
            $this->repository->fireStatementAndReturn($query),
77
            new OutfitLeaderboardTransformer
78
        );
79
    }
80
81
    /**
82
     * Gets the appropiate field for the table and handles some table naming oddities
83
     * @param  string $input Field to look at
84
     * @return string
85
     */
86 View Code Duplication
    public function getField($input) {
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...
87
        $field = null;
88
        switch ($input) {
89
            case 'kills':
90
                $field = 'outfitKills';
91
                break;
92
            case 'deaths':
93
                $field = 'outfitDeaths';
94
                break;
95
            case 'teamkills':
96
                $field = 'outfitTKs';
97
                break;
98
            case 'suicides':
99
                $field = 'outfitSuicides';
100
                break;
101
            case 'captures':
102
                $field = 'outfitCaptures';
103
                break;
104
        }
105
106
        return $field;
107
    }
108
}
109