Passed
Push — master ( 74cbab...62dc6b )
by Stephen
02:33
created

NextOrPreviousModelQuery   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 89
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A previous() 0 4 1
A next() 0 4 1
A cacheKey() 0 3 1
A execute() 0 12 2
1
<?php
2
3
4
namespace Sfneal\Queries;
5
6
use Illuminate\Database\Eloquent\Model;
7
use Sfneal\Models\AbstractModel;
8
use Sfneal\Caching\Traits\Cacheable;
9
10
class NextOrPreviousModelQuery extends AbstractQuery
11
{
12
    use Cacheable;
13
14
    /**
15
     * @var bool Determine if the return model should be the 'next' or 'previous'
16
     */
17
    private $next;
18
19
    /**
20
     * @var string Cache Key String ('next' or 'previous')
21
     */
22
    private $cache_key_string;
23
24
    /**
25
     * @var AbstractModel|string
26
     */
27
    private $model;
28
29
    /**
30
     * @var int Model ID
31
     */
32
    private $model_id;
33
34
    /**
35
     * NextOrPreviousModelQuery constructor.
36
     *
37
     * @param AbstractModel|string $model
38
     * @param int $model_id
39
     * @param bool $next
40
     */
41
    public function __construct($model, int $model_id, bool $next = true)
42
    {
43
        $this->model = $model;
44
        $this->model_id = $model_id;
45
        $this->next = $next;
46
        $this->cache_key_string = $next ? 'next' : 'previous';
47
    }
48
49
    /**
50
     * Retrieve the 'next' or 'previous' Model
51
     *
52
     * @return Model|null
53
     */
54
    public function execute()
55
    {
56
        $query = $this->model::query();
57
58
        // Next Model
59
        if ($this->next == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
60
            return $query->getNextModel($this->model_id);
61
        }
62
63
        // Previous Model
64
        else {
65
            return $query->getPreviousModel($this->model_id);
66
        }
67
    }
68
69
    /**
70
     * Set the returned model to 'next'
71
     *
72
     * @return $this
73
     */
74
    public function next(): self {
75
        $this->next = true;
76
        $this->cache_key_string = 'next';
77
        return $this;
78
    }
79
80
    /**
81
     * Set the returned model to 'previous'
82
     *
83
     * @return $this
84
     */
85
    public function previous(): self {
86
        $this->next = false;
87
        $this->cache_key_string = 'previous';
88
        return $this;
89
    }
90
91
    /**
92
     * Retrieve the Queries Cache Key
93
     *
94
     * @return string
95
     */
96
    public function cacheKey(): string
97
    {
98
        return $this->model::getTableName() . ":{$this->cache_key_string}:#{$this->model_id}";
99
    }
100
}
101