Passed
Push — master ( 7106e3...456ee0 )
by Menno
02:12
created

ConditionBuilder::modelSaved()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 2
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Codefocus\ManagedCache;
4
5
use Codefocus\ManagedCache\Events\Event;
6
7
use Illuminate\Database\Eloquent\Model;
8
use Iterator;
9
10
/**
11
 * ConditionBuilder.
12
 */
13
class ConditionBuilder implements Iterator
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $conditions = [];
19
20
    /**
21
     * Adds a Condition that tags a cache to get invalidated
22
     * when a new Model of the specified class is created.
23
     *
24
     * @param string $modelClassName model class name
25
     *
26
     * @return self
27
     */
28
    public function modelCreated(string $modelClassName): self
29
    {
30
        $this->conditions[] = new Condition(
31
            Event::EVENT_ELOQUENT_CREATED,
32
            $modelClassName
33
        );
34
35
        return $this;
36
    }
37
38
    /**
39
     * Adds a Condition that tags a cache to get invalidated
40
     * when the specified Model instance, or any Model of the specified class
41
     * is updated.
42
     *
43
     * @param mixed $model model instance or class name
44
     * @param int|null $modelId (default: null) The Model id
45
     *
46
     * @return self
47
     */
48
    public function modelUpdated($model, ?int $modelId = null): self
49
    {
50
        if ($this->isModel($model)) {
0 ignored issues
show
Bug introduced by
The method isModel() does not exist on Codefocus\ManagedCache\ConditionBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        if ($this->/** @scrutinizer ignore-call */ isModel($model)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
            $modelClassName = get_class($model);
52
            $modelId = $model->getKey();
53
        } else {
54
            $modelClassName = $model;
55
        }
56
        $this->conditions[] = new Condition(
57
            Event::EVENT_ELOQUENT_UPDATED,
58
            $modelClassName,
59
            $modelId
60
        );
61
62
        return $this;
63
    }
64
65
    /**
66
     * Adds a Condition that tags a cache to get invalidated
67
     * when the specified Model instance, or any Model of the specified class
68
     * is saved.
69
     *
70
     * @param mixed $model model instance or class name
71
     * @param int|null $modelId (default: null) The Model id
72
     *
73
     * @return self
74
     */
75
    public function modelSaved($model, ?int $modelId = null): self
76
    {
77
        if ($this->isModel($model)) {
78
            $modelClassName = get_class($model);
79
            $modelId = $model->getKey();
80
        } else {
81
            $modelClassName = $model;
82
        }
83
        $this->confitions[] = new Condition(
0 ignored issues
show
Bug Best Practice introduced by
The property confitions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
84
            Event::EVENT_ELOQUENT_SAVED,
85
            $modelClassName,
86
            $modelId
87
        );
88
89
        return $this;
90
    }
91
92
    /**
93
     * Adds a Condition that tags a cache to get invalidated
94
     * when the specified Model instance, or any Model of the specified class
95
     * is deleted.
96
     *
97
     * @param mixed $model model instance or class name
98
     * @param int|null $modelId (default: null) The Model id
99
     *
100
     * @return self
101
     */
102
    public function modelDeleted($model, ?int $modelId = null): self
103
    {
104
        if ($this->isModel($model)) {
105
            $modelClassName = get_class($model);
106
            $modelId = $model->getKey();
107
        } else {
108
            $modelClassName = $model;
109
        }
110
        $this->conditions[] = new Condition(
111
            Event::EVENT_ELOQUENT_DELETED,
112
            $modelClassName,
113
            $modelId
114
        );
115
116
        return $this;
117
    }
118
119
    /**
120
     * Adds a Condition that tags a cache to get invalidated
121
     * when the specified Model instance, or any Model of the specified class
122
     * is restored.
123
     *
124
     * @param mixed $model model instance or class name
125
     * @param int|null $modelId (default: null) The Model id
126
     *
127
     * @return self
128
     */
129
    public function modelRestored($model, ?int $modelId = null): self
130
    {
131
        if ($this->isModel($model)) {
132
            $modelClassName = get_class($model);
133
            $modelId = $model->getKey();
134
        } else {
135
            $modelClassName = $model;
136
        }
137
        $this->conditions[] = new Condition(
138
            Event::EVENT_ELOQUENT_RESTORED,
139
            $modelClassName,
140
            $modelId
141
        );
142
143
        return $this;
144
    }
145
146
    public function current(): mixed
0 ignored issues
show
Bug introduced by
The type Codefocus\ManagedCache\mixed was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
147
    {
148
        return current($this->conditions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return current($this->conditions) returns the type mixed which includes types incompatible with the type-hinted return Codefocus\ManagedCache\mixed.
Loading history...
149
    }
150
151
    public function key(): scalar
0 ignored issues
show
Bug introduced by
The type Codefocus\ManagedCache\scalar was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
152
    {
153
        return key($this->conditions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return key($this->conditions) returns the type mixed which includes types incompatible with the type-hinted return Codefocus\ManagedCache\scalar.
Loading history...
154
    }
155
156
    public function next(): void
157
    {
158
        next($this->conditions);
159
    }
160
161
    public function rewind(): void
162
    {
163
        rewind($this->conditions);
0 ignored issues
show
Bug introduced by
$this->conditions of type array is incompatible with the type resource expected by parameter $handle of rewind(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
        rewind(/** @scrutinizer ignore-type */ $this->conditions);
Loading history...
164
    }
165
166
    public function valid(): boolean
0 ignored issues
show
Bug introduced by
The type Codefocus\ManagedCache\boolean was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
167
    {
168
        return valid($this->conditions);
0 ignored issues
show
Bug introduced by
The function valid was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        return /** @scrutinizer ignore-call */ valid($this->conditions);
Loading history...
169
    }
170
}
171