Completed
Push — master ( 45aada...26ef87 )
by Matthew
02:32
created

ResultLoader::readRecent()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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