Passed
Branch master (6b6e1c)
by Menno
02:03
created

Condition::assertIsModel()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Codefocus\ManagedCache;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
8
class Condition
9
{
10
    const CACHE_TAG_PREFIX = 'ManagedCache:';
11
    const CACHE_TAG_SEPARATOR = '-';
12
    const CACHE_TAG_ID_OPEN = '(';
13
    const CACHE_TAG_ID_CLOSE = ')';
14
    const CACHE_TAG_RELATED = '->';
15
16
    protected $eventName = null;
17
18
    protected $modelName = null;
19
20
    protected $modelId = null;
21
22
    protected $relatedModelName = null;
23
24
    protected $relatedModelId = null;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param string $eventName
30
     * @param ?string $modelName (default: null)
31
     * @param ?integer $modelId (default: null)
32
     * @param ?string $relatedModelName (default: null)
33
     * @param ?integer $relatedModelId (default: null)
34
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment ?string at position 0 could not be parsed: Unknown type name '?string' at position 0 in ?string.
Loading history...
35
    public function __construct(
36
        string $eventName,
37
        ?string $modelName = null,
38
        ?int $modelId = null,
39
        ?string $relatedModelName = null,
40
        ?int $relatedModelId = null
41
    ) {
42
        $this->eventName = $eventName;
43
        $this->modelName = $modelName;
44
        $this->modelId = $modelId;
45
        $this->relatedModelName = $relatedModelName;
46
        $this->relatedModelId = $relatedModelId;
47
48
        if (null !== $this->modelName) {
49
            $this->assertIsModel($this->modelName);
50
        }
51
    }
52
53
    /**
54
     * Return a string representation of this Condition.
55
     *
56
     * @return string
57
     */
58
    public function __toString(): string
59
    {
60
        if (null === $this->modelName) {
61
            //  All events of this type
62
            //  trigger a flush.
63
            return
64
                self::CACHE_TAG_PREFIX .
65
                $this->eventName;
66
        }
67
        $modelTagPart = $this->getModelTagPart();
68
        $relationTagPart = $this->getRelationTagPart();
69
        if ($relationTagPart) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relationTagPart of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
70
            return
71
                self::CACHE_TAG_PREFIX .
72
                $this->eventName .
73
                self::CACHE_TAG_SEPARATOR .
74
                $modelTagPart .
75
                self::CACHE_TAG_SEPARATOR .
76
                $relationTagPart;
77
        }
78
79
        return
80
            self::CACHE_TAG_PREFIX .
81
            $this->eventName .
82
            self::CACHE_TAG_SEPARATOR .
83
            $modelTagPart;
84
    }
85
86
    protected function getModelTagPart()
87
    {
88
        if (null === $this->modelId) {
89
            //  Any instance of this model.
90
            return $this->modelName;
91
        }
92
93
        //  Only the instance of this model with this id.
94
        return $this->modelName .
95
            self::CACHE_TAG_ID_OPEN .
96
            $this->modelId .
97
            self::CACHE_TAG_ID_CLOSE;
98
    }
99
100
    protected function getRelationTagPart()
101
    {
102
        if (null === $this->relatedModelId) {
103
            if (null === $this->relatedModelName) {
104
                //  No relation specified.
105
                return null;
106
            }
107
            //  Any instance of this related model.
108
            return $this->relatedModelName;
109
        }
110
111
        //  Only the instance of this related model with this id.
112
        return $this->relatedModelName .
113
            self::CACHE_TAG_ID_OPEN .
114
            $this->relatedModelId .
115
            self::CACHE_TAG_ID_CLOSE;
116
    }
117
118
    /**
119
     * Throws an Exception if the specified class name is not an Eloquent Model.
120
     *
121
     * @param string $modelClassName
122
     */
123
    protected function assertIsModel(string $modelClassName): void
124
    {
125
        if ( ! is_subclass_of($modelClassName, Model::class)) {
126
            throw new Exception($modelClassName . ' is not an Eloquent Model class name.');
127
        }
128
    }
129
}
130