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

AlertStatisticsLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.4286
cc 1
eloc 7
nc 1
nop 2
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
50
        $queryObject = new QueryObject;
51
        $queryObject->addSelect('COUNT(ResultID) AS COUNT');
52
        $queryObject->addWhere([
53
            'col'   => 'Valid',
54
            'value' => '1'
55
        ]);
56
57 View Code Duplication
        if (! empty($post['ResultServer'])) {
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...
58
            if ($this->inputValidator->validatePostVars(
59
                'ResultServer',
60
                $post['ResultServer']
61
            ) === true) {
62
                $queryObject->addWhere([
63
                    'col'   => 'ResultServer',
64
                    'value' => $post['ResultServer']
65
                ]);
66
                $redisKey .= ":Server-{$post['ResultServer']}";
67
            }
68
        }
69
70 View Code Duplication
        if (! empty($post['ResultWinner'])) {
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...
71
            if ($this->inputValidator->validatePostVars(
72
                'ResultWinner',
73
                $post['ResultWinner']
74
            ) === true) {
75
                $queryObject->addWhere([
76
                    'col'   => 'ResultWinner',
77
                    'value' => $post['ResultWinner']
78
                ]);
79
                $redisKey .= ":Winner-{$post['ResultWinner']}";
80
            }
81
        }
82
83 View Code Duplication
        if (! empty($post['ResultAlertCont'])) {
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...
84
            if ($this->inputValidator->validatePostVars(
85
                'ResultAlertCont',
86
                $post['ResultAlertCont']
87
            ) === true) {
88
                $queryObject->addWhere([
89
                    'col'   => 'ResultAlertCont',
90
                    'value' => $post['ResultAlertCont']
91
                ]);
92
                $redisKey .= ":Cont-{$post['ResultAlertCont']}";
93
            }
94
        }
95
96 View Code Duplication
        if (! empty($post['ResultDomination'])) {
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...
97
            if ($this->inputValidator->validatePostVars(
98
                'ResultDomination',
99
                $post['ResultDomination']
100
            ) === true) {
101
                $queryObject->addWhere([
102
                    'col'   => 'ResultDomination',
103
                    'value' => $post['ResultDomination']
104
                ]);
105
                $redisKey .= ":Domination-{$post['ResultDomination']}";
106
            }
107
        }
108
109
        if ($this->checkRedis($redisKey)) {
110
            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...
111
        }
112
113
        $this->setCacheExpireTime(900); // 15 mins
114
115
        return $this->cacheAndReturn(
116
            $this->repository->read($queryObject),
117
            $redisKey
118
        );
119
    }
120
}
121