Completed
Push — master ( 1768c8...189bb6 )
by Matthew
03:41
created

AlertStatisticsLoader::getZoneStats()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 34
rs 8.8571
cc 2
eloc 21
nc 2
nop 3
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\AlertRepository;
8
use Ps2alerts\Api\Validator\AlertInputValidator;
9
10
class AlertStatisticsLoader extends AbstractStatisticsLoader
11
{
12
    /**
13
     * @var \Ps2alerts\Api\Repository\AlertRepository
14
     */
15
    protected $repository;
16
17
    /**
18
     * @var \Ps2alerts\Api\Validator\AlertInputValidator
19
     */
20
    protected $inputValidator;
21
22
    /**
23
     * Construct
24
     *
25
     * @param \Ps2alerts\Api\Repository\AlertRepository    $repository
26
     * @param \Ps2alerts\Api\Validator\AlertInputValidator $inputValidator
27
     */
28
    public function __construct(
29
        AlertRepository     $repository,
30
        AlertInputValidator $inputValidator
31
    ) {
32
        $this->repository     = $repository;
33
        $this->inputValidator = $inputValidator;
34
35
        $this->setCacheNamespace('Statistics');
36
        $this->setType('Alerts');
37
    }
38
39
    /**
40
     * Read total counts for alerts
41
     *
42
     * @param  array $post
43
     *
44
     * @return array
45
     */
46
    public function readTotals(array $post)
47
    {
48
        $redisKey = "{$this->getCacheNamespace()}:{$this->getType()}:Totals";
49
        $redisKey = $this->appendRedisKey($post, $redisKey);
50
        $post = $this->processPostVars($post);
51
52
        $this->getLogDriver()->addDebug($redisKey);
53
54
        if ($this->checkRedis($redisKey)) {
55
            return $this->getFromRedis($redisKey);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFromRedis($redisKey); (string) is incompatible with the return type documented by Ps2alerts\Api\Loader\Sta...sticsLoader::readTotals of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
56
        }
57
58
        $queryObject = new QueryObject;
59
60
        $queryObject = $this->setupQueryObject($queryObject, $post);
0 ignored issues
show
Documentation introduced by
$queryObject is of type object<Ps2alerts\Api\QueryObjects\QueryObject>, but the function expects a object<Ps2alerts\Api\Loa...eryObjects\QueryObject>.

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...
61
        $queryObject->addSelect('COUNT(ResultID) AS COUNT');
62
63
        if ($this->checkRedis($redisKey)) {
64
            return $this->getFromRedis($redisKey);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFromRedis($redisKey); (string) is incompatible with the return type documented by Ps2alerts\Api\Loader\Sta...sticsLoader::readTotals of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
        }
66
67
        $this->setCacheExpireTime(900); // 15 mins
68
69
        return $this->cacheAndReturn(
70
            $this->repository->read($queryObject),
71
            $redisKey
72
        );
73
    }
74
75
    /**
76
     * Retrieves all zone totals and caches as required
77
     *
78
     * @return array
79
     */
80
    public function readZoneTotals()
81
    {
82
        $masterRedisKey = "{$this->getCacheNamespace()}:{$this->getType()}:Totals:Zones";
83
84
        $this->getLogDriver()->addDebug($masterRedisKey);
85
86
        if ($this->checkRedis($masterRedisKey)) {
87
            $this->getLogDriver()->addDebug("Pulled the lot from Redis");
88
            return $this->getFromRedis($masterRedisKey);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFromRedis($masterRedisKey); (string) is incompatible with the return type documented by Ps2alerts\Api\Loader\Sta...sLoader::readZoneTotals of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
89
        }
90
91
        $servers  = [1,10,13,17,25,1000,2000];
92
        $zones    = [2,4,6,8];
93
        $factions = ['vs','nc','tr','draw'];
94
95
        $results = [];
96
        $this->setCacheExpireTime(3600); // 1 Hour
97
98
        // Dat loop yo
99
        foreach ($servers as $server) {
100
            foreach ($zones as $zone) {
101
                foreach ($factions as $faction) {
102
                    $results[$server][$zone][$faction] = $this->getZoneStats($server, $zone, $faction);
103
                }
104
            }
105
        }
106
107
        // Commit to Redis
108
        return $this->cacheAndReturn(
109
            $results,
110
            $masterRedisKey
111
        );
112
    }
113
114
    /**
115
     * Gets all information regarding zone victories out of the DB and caches as
116
     * required
117
     *
118
     * @see readZoneTotals()
119
     *
120
     * @param  integer $server
121
     * @param  integer $zone
122
     * @param  integer $faction
123
     *
124
     * @return array
125
     */
126
    public function getZoneStats($server, $zone, $faction)
127
    {
128
        $redisKey = "{$this->getCacheNamespace()}:{$this->getType()}:Totals:Zones";
129
        $redisKey .= ":{$server}:{$zone}:{$faction}";
130
131
        $this->getLogDriver()->addDebug($redisKey);
132
133
        if ($this->checkRedis($redisKey)) {
134
            $this->getLogDriver()->addDebug("CACHE PULL");
135
            return $this->getFromRedis($redisKey);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getFromRedis($redisKey); (string) is incompatible with the return type documented by Ps2alerts\Api\Loader\Sta...icsLoader::getZoneStats of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
136
        }
137
138
        // Fire a set of queries to build the object required
139
        $queryObject = new QueryObject;
140
        $queryObject->addSelect('COUNT(ResultID) AS COUNT');
141
        $queryObject->addWhere([
142
            'col'   => 'ResultServer',
143
            'value' => $server
144
        ]);
145
        $queryObject->addWhere([
146
            'col'   => 'ResultAlertCont',
147
            'value' => $zone
148
        ]);
149
        $queryObject->addWhere([
150
            'col'   => 'ResultWinner',
151
            'value' => $faction
152
        ]);
153
154
        // Commit to Redis
155
        return $this->cacheAndReturn(
156
            $this->repository->read($queryObject)[0]["COUNT"],
157
            $redisKey
158
        );
159
    }
160
}
161