Completed
Push — master ( 0cb788...5f9089 )
by Matthew
03:43
created

PlayerStatisticsLoader::validatePostVars()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 7.2766
cc 10
eloc 12
nc 9
nop 2

How to fix   Complexity   

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\Loader\Statistics;
4
5
use Ps2alerts\Api\Loader\Statistics\AbstractStatisticsLoader;
6
use Ps2alerts\Api\QueryObjects\QueryObject;
7
use Ps2alerts\Api\Repository\Statistics\PlayerTotalsRepository;
8
9
class PlayerStatisticsLoader extends AbstractStatisticsLoader
10
{
11
    /**
12
     * @var \Ps2alerts\Api\Repository\Statistics\PlayerTotalsRepository
13
     */
14
    protected $repository;
15
16
    /**
17
     * Construct
18
     *
19
     * @param \Ps2alerts\Api\Repository\Statistics\PlayerTotalsRepository $repository
20
     */
21
    public function __construct(PlayerTotalsRepository $repository)
22
    {
23
        $this->repository = $repository;
24
        $this->setCacheNamespace('Statistics');
25
        $this->setType('Players');
26
    }
27
28
    public function readLeaderboard(array $post)
29
    {
30
        $redisKey = "{$this->getCacheNamespace()}:{$this->getType()}:Leaderboards";
31
32
        $queryObject = new QueryObject;
33
34
        // Build based on metric alone (order by playerKills for example)
35
        if (! empty($post['metric'] && empty($post['value']))
36
            && $this->validatePostVars('metric', $post['metric']) === true
37
        ) {
38
            $direction = (! empty($post['direction']) ? $post['direction'] : 'desc');
39
40
            $queryObject->setOrderBy($post['metric']);
41
            $queryObject->setOrderByDirection($direction);
42
43
            $redisKey .= ":{$post['metric']}-{$direction}";
44
        }
45
46
        // Build based on metric and a value (such as order by playerKills by server)
47
        if (! empty($post['value'])
48
            && ! empty($post['metric'])
49
            && $this->validatePostVars('metric', $post['metric']) === true
50
        ) {
51
            $op = (! empty($post['operator']) ? $post['operator'] : '=');
52
53
            $queryObject->addWhere([
54
                'col' => $post['metric'],
55
                'op'  => $op,
56
                'value' => $post['value']
57
            ]);
58
59
            $direction = (! empty($post['direction']) ? $post['direction'] : 'desc');
60
61
            $queryObject->setOrderBy($post['metric']);
62
            $queryObject->setOrderByDirection($direction);
63
64
            $redisKey .= ":{$post['metric']}{$op}{$post['value']}-{$direction}";
65
        }
66
67
        if (! empty($post['limit'])) {
68
            $post['limit'] = intval($post['limit']);
69
70
            // Hard encode a 50 limit
71
            if ($post['limit'] > 50) {
72
                $post['limit'] = 50;
73
            }
74
75
            $queryObject->setLimit($post['limit']);
76
        } else {
77
            $post['limit'] = 50;
78
        }
79
80
        $redisKey .= ":limit-{$post['limit']}";
81
82
        /*if ($this->checkRedis($redisKey)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
            return $this->getFromRedis($redisKey);
84
        }*/
85
86
        $this->setCacheExpireTime(3600); // 1 hour
87
88
        return $this->cacheAndReturn(
89
            $this->repository->read($queryObject),
90
            $redisKey
91
        );
92
    }
93
94
    public function validatePostVars($field, $value)
95
    {
96
        if ($field === 'metric' && ! empty($value)) {
97
            switch ($value) {
98
                case 'playerKills':
99
                case 'playerDeaths':
100
                case 'playerTeamKills':
101
                case 'playerSuicides':
102
                case 'playerFaction':
103
                case 'headshots':
104
                case 'playerServer':
105
                    return true;
106
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
107
            }
108
        }
109
    }
110
}
111