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

AlertLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 123
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B readRecent() 0 37 4
A readActive() 0 21 2
A readSingle() 0 23 3
1
<?php
2
3
namespace Ps2alerts\Api\Loader;
4
5
use Ps2alerts\Api\QueryObjects\QueryObject;
6
use Ps2alerts\Api\Loader\AbstractLoader;
7
use Ps2alerts\Api\Repository\AlertRepository;
8
9
class AlertLoader extends AbstractLoader
10
{
11
    /**
12
     * @var \Ps2alerts\Api\Repository\AlertRepository
13
     */
14
    protected $repository;
15
16
    /**
17
     * Construct
18
     *
19
     * @param \Ps2alerts\Api\Repository\AlertRepository $repository
20
     */
21
    public function __construct(AlertRepository $repository)
22
    {
23
        $this->repository = $repository;
24
        $this->setCacheNamespace('Alerts');
25
    }
26
27
    /**
28
     * Returns recent alerts
29
     *
30
     * @param  array $args Path Arguments
31
     *
32
     * @return array
33
     */
34
    public function readRecent(array $args)
35
    {
36
        $redisKey = "{$this->getCacheNamespace()}:Recent";
37
38
        $this->setCacheExpireTime(3600); // 1 hour
39
40
        $queryObject = new QueryObject;
41
        $queryObject->addWhere([
42
            'col'   => 'ResultStartTime',
43
            'op'    => '>',
44
            'value' => date('U', strtotime('-48 hours'))
45
        ]);
46
47
        if (! empty($args['serverID'])) {
48
            $redisKey .= ":{$args['serverID']}";
49
            $queryObject->addWhere([
50
                'col'   => 'ResultServer',
51
                'value' => $args['serverID']
52
            ]);
53
        }
54
55
        if (! empty($args['limit'])) {
56
            if ($args['limit'] > 50) {
57
                $args['limit'] = 50;
58
            }
59
            $redisKey .= "/{$args['limit']}";
60
            $queryObject->setLimit($args['limit']);
61
        }
62
63
        $queryObject->setOrderBy('ResultStartTime');
64
        $queryObject->setOrderByDirection('desc');
65
66
        return $this->cacheAndReturn(
67
            $this->repository->read($queryObject),
68
            $redisKey
69
        );
70
    }
71
72
    /**
73
     * Reads all currently active alerts
74
     *
75
     * @param  array $args
76
     *
77
     * @return array
78
     */
79
    public function readActive($args)
80
    {
81
        $queryObject = new QueryObject;
82
        $queryObject->addWhere([
83
            'col'   => 'InProgress',
84
            'value' => 1
85
        ]);
86
87
        if (! empty($args['serverID'])) {
88
            $queryObject->addWhere([
89
                'col'   => 'ResultServer',
90
                'value' => $args['serverID']
91
            ]);
92
        }
93
94
        $this->setCacheable(false);
95
96
        return $this->cacheAndReturn(
97
            $this->repository->read($queryObject)
98
        );
99
    }
100
101
    /**
102
     * Reads a single Alert
103
     *
104
     * @param  integer|string $id
105
     *
106
     * @return array
107
     */
108
    public function readSingle($id)
109
    {
110
        $redisKey = "{$this->getCacheNamespace()}:{$id}";
111
112
        if ($this->checkRedis($redisKey)) {
113
            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\AlertLoader::readSingle 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...
114
        }
115
116
        $queryObject = new QueryObject;
117
        $queryObject->addWhere([
118
            'col'   => 'primary',
119
            'value' => $id
120
        ]);
121
        $queryObject->setDimension('single');
122
123
        $result = $this->repository->read($queryObject);
124
125
        if ($result['InProgress'] === '1') {
126
            $this->setCacheable(false);
127
        }
128
129
        return $this->cacheAndReturn($result, $redisKey);
130
    }
131
}
132