Passed
Push — master ( bb0e2b...eaa23b )
by Stephen
01:06 queued 10s
created

NextOrPreviousModelQuery::builder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\Queries;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Sfneal\Caching\Traits\Cacheable;
8
use Sfneal\Models\AbstractModel;
9
10
class NextOrPreviousModelQuery implements Query
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 a Query builder.
51
     *
52
     * @return Builder
53
     */
54
    public function builder(): Builder
55
    {
56
        return $this->model::query();
57
    }
58
59
    /**
60
     * Retrieve the 'next' or 'previous' Model.
61
     *
62
     * @return Model|null
63
     */
64
    public function execute(): ?Model
65
    {
66
        $query = $this->model::query();
67
68
        // Next Model
69
        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...
70
            return $query->getNextModel($this->model_id);
71
        }
72
73
        // Previous Model
74
        else {
75
            return $query->getPreviousModel($this->model_id);
76
        }
77
    }
78
79
    /**
80
     * Set the returned model to 'next'.
81
     *
82
     * @return $this
83
     */
84
    public function next(): self
85
    {
86
        $this->next = true;
87
        $this->cache_key_string = 'next';
88
89
        return $this;
90
    }
91
92
    /**
93
     * Set the returned model to 'previous'.
94
     *
95
     * @return $this
96
     */
97
    public function previous(): self
98
    {
99
        $this->next = false;
100
        $this->cache_key_string = 'previous';
101
102
        return $this;
103
    }
104
105
    /**
106
     * Retrieve the Queries Cache Key.
107
     *
108
     * @return string
109
     */
110
    public function cacheKey(): string
111
    {
112
        return $this->model::getTableName().":{$this->cache_key_string}:#{$this->model_id}";
113
    }
114
}
115