Passed
Pull Request — master (#29)
by Sébastien
12:35 queued 04:51
created

Versionable   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Test Coverage

Coverage 97.87%

Importance

Changes 0
Metric Value
wmc 15
eloc 34
dl 0
loc 168
ccs 46
cts 47
cp 0.9787
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A afterUpdate() 0 4 2
A afterInsert() 0 4 2
A insertVersion() 0 3 1
A incrementVersion() 0 8 1
A __construct() 0 4 1
A subscribe() 0 10 2
A beforeUpdate() 0 7 2
A changeSchema() 0 3 1
A deleteAllVersions() 0 9 2
A beforeInsert() 0 3 1
1
<?php
2
3
namespace Bdf\Prime\Behaviors;
4
5
use Bdf\Prime\Mapper\Builder\FieldBuilder;
6
use Bdf\Prime\Repository\RepositoryEventsSubscriberInterface;
7
use Bdf\Prime\Repository\RepositoryInterface;
8
9
/**
10
 * Versionable
11
 *
12
 * The versionable behavior allows you to keep an history of your model objects.
13
 *
14
 * @template E as object
15
 * @extends Behavior<E>
16
 */
17
class Versionable extends Behavior
18
{
19
    const COLUMN_NAME = 'version';
20
21
    /**
22
     * The version repository className
23
     *
24
     * @var string
25
     */
26
    protected $versionClass;
27
28
    /**
29
     * Allow version deletion
30
     *
31
     * @var boolean
0 ignored issues
show
introduced by
Expected "bool" but found "boolean" for @var tag in member variable comment
Loading history...
32
     */
33
    protected $allowDeletion;
34
35
    /**
36
     * Version table name
37
     *
38
     * @var string
39
     */
40
    protected $tableName;
41
42
    /**
43
     * Versionable constructor.
44
     *
45
     * @param string $versionClass
46
     * @param bool   $allowDeletion
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for parameter type
Loading history...
47
     */
48 1
    public function __construct($versionClass, $allowDeletion = false)
49
    {
50 1
        $this->versionClass = $versionClass;
51 1
        $this->allowDeletion = $allowDeletion;
52 1
    }
53
54
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $builder should have a doc-comment as per coding-style.
Loading history...
55
     * {@inheritdoc}
56
     */
57 1
    public function changeSchema(FieldBuilder $builder): void
58
    {
59 1
        $builder->integer(self::COLUMN_NAME, 0);
60 1
    }
61
62
    /**
63
     * Before insert
64
     *
65
     * we increment version number on entity
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
Coding Style introduced by
Doc comment long description must start with a capital letter
Loading history...
66
     *
67
     * @param E $entity
0 ignored issues
show
Bug introduced by
The type Bdf\Prime\Behaviors\E 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...
68
     * @param RepositoryInterface<E> $repository
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceE" but found "RepositoryInterface<E>" for parameter type
Loading history...
69
     */
70 6
    public function beforeInsert($entity, $repository)
0 ignored issues
show
introduced by
Type hint "E" missing for $entity
Loading history...
introduced by
Type hint "RepositoryInterface<E>" missing for $repository
Loading history...
71
    {
72 6
        $this->incrementVersion($entity, $repository);
73 6
    }
74
75
    /**
76
     * After insert
77
     *
78
     * we historicize entity
0 ignored issues
show
Coding Style introduced by
Doc comment long description must start with a capital letter
Loading history...
introduced by
Doc comment long description must end with a full stop
Loading history...
79
     *
80
     * @param E $entity
81
     * @param RepositoryInterface<E> $repository
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceE" but found "RepositoryInterface<E>" for parameter type
Loading history...
82
     * @param integer $count
0 ignored issues
show
introduced by
Expected "int" but found "integer" for parameter type
Loading history...
83
     */
84 6
    public function afterInsert($entity, $repository, $count)
0 ignored issues
show
introduced by
Type hint "E" missing for $entity
Loading history...
introduced by
Type hint "RepositoryInterface<E>" missing for $repository
Loading history...
85
    {
86 6
        if ($count != 0) {
87 6
            $this->insertVersion($entity, $repository);
88
        }
89 6
    }
90
91
    /**
92
     * Before update
93
     *
94
     * we increment version number on entity
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
Coding Style introduced by
Doc comment long description must start with a capital letter
Loading history...
95
     *
96
     * @param E $entity
97
     * @param RepositoryInterface<E> $repository
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceE" but found "RepositoryInterface<E>" for parameter type
Loading history...
98
     * @param null|\ArrayObject $attributes
99
     */
100 5
    public function beforeUpdate($entity, $repository, $attributes)
0 ignored issues
show
introduced by
Type hint "E" missing for $entity
Loading history...
introduced by
Type hint "RepositoryInterface<E>" missing for $repository
Loading history...
101
    {
102 5
        if ($attributes !== null) {
103 1
            $attributes[] = self::COLUMN_NAME;
104
        }
105
106 5
        $this->incrementVersion($entity, $repository);
107 5
    }
108
109
    /**
110
     * After update
111
     *
112
     * we historicize entity
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
Coding Style introduced by
Doc comment long description must start with a capital letter
Loading history...
113
     *
114
     * @param E $entity
115
     * @param RepositoryInterface<E> $repository
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceE" but found "RepositoryInterface<E>" for parameter type
Loading history...
116
     * @param integer $count
0 ignored issues
show
introduced by
Expected "int" but found "integer" for parameter type
Loading history...
117
     */
118 5
    public function afterUpdate($entity, $repository, $count)
0 ignored issues
show
introduced by
Type hint "E" missing for $entity
Loading history...
introduced by
Type hint "RepositoryInterface<E>" missing for $repository
Loading history...
119
    {
120 5
        if ($count != 0) {
121 5
            $this->insertVersion($entity, $repository);
122
        }
123 5
    }
124
125
    /**
126
     * Remove entity versions
127
     *
128
     * @param E $entity
129
     * @param RepositoryInterface<E> $repository
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceE" but found "RepositoryInterface<E>" for parameter type
Loading history...
130
     */
131 1
    public function deleteAllVersions($entity, $repository)
0 ignored issues
show
introduced by
Type hint "E" missing for $entity
Loading history...
introduced by
Type hint "RepositoryInterface<E>" missing for $repository
Loading history...
132
    {
133 1
        $queries = $repository->repository($this->versionClass)->queries();
134 1
        $criteria = $repository->mapper()->primaryCriteria($entity);
135
136 1
        if ($query = $queries->keyValue($criteria)) {
0 ignored issues
show
Coding Style introduced by
Variable assignment found within a condition. Did you mean to do a comparison ?
Loading history...
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
137 1
            $query->delete();
138
        } else {
139
            $queries->builder()->where($criteria)->delete();
140
        }
141 1
    }
142
143
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $notifier should have a doc-comment as per coding-style.
Loading history...
144
     * {@inheritdoc}
145
     */
146 1
    public function subscribe(RepositoryEventsSubscriberInterface $notifier): void
147
    {
148 1
        $notifier->inserting([$this, 'beforeInsert']);
149 1
        $notifier->inserted([$this, 'afterInsert']);
150
151 1
        $notifier->updating([$this, 'beforeUpdate']);
152 1
        $notifier->updated([$this, 'afterUpdate']);
153
154 1
        if ($this->allowDeletion) {
155 1
            $notifier->deleted([$this, 'deleteAllVersions']);
156
        }
157 1
    }
158
159
    /**
160
     * Increment version number on entity
161
     *
162
     * @param E $entity
163
     * @param RepositoryInterface<E> $repository
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceE" but found "RepositoryInterface<E>" for parameter type
Loading history...
164
     */
165 6
    protected function incrementVersion($entity, $repository)
0 ignored issues
show
introduced by
Type hint "E" missing for $entity
Loading history...
introduced by
Type hint "RepositoryInterface<E>" missing for $repository
Loading history...
166
    {
167 6
        $mapper = $repository->mapper();
168
169 6
        $mapper->hydrateOne(
170 6
            $entity,
171 6
            self::COLUMN_NAME,
172 6
            $mapper->extractOne($entity, self::COLUMN_NAME) + 1
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
173
        );
174 6
    }
175
176
    /**
177
     * Historicize entity
178
     *
179
     * @param E $entity
180
     * @param RepositoryInterface<E> $repository
0 ignored issues
show
introduced by
Expected "RepositoryInterfaceE" but found "RepositoryInterface<E>" for parameter type
Loading history...
181
     */
182 6
    protected function insertVersion($entity, $repository)
0 ignored issues
show
introduced by
Type hint "E" missing for $entity
Loading history...
introduced by
Type hint "RepositoryInterface<E>" missing for $repository
Loading history...
183
    {
184 6
        $repository->repository($this->versionClass)->insert($entity);
185 6
    }
186
}
187