StatesTrait   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 212
rs 10
c 2
b 0
f 0
ccs 51
cts 51
cp 1
wmc 21
lcom 1
cbo 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A hasBeenMutated() 0 4 1
A hasBeenDeleted() 0 4 1
A hasBeenUndeleted() 0 4 1
A hasBeenModified() 0 4 1
A isDraft() 0 4 1
A hasBeenPublished() 0 4 1
A setModifiedState() 0 13 2
A isDeleted() 0 4 1
A isPublished() 0 4 1
A setMutatedState() 0 14 3
setSystemProperties() 0 1 ?
A setDeletedState() 0 15 2
A setUndeletedState() 0 15 2
A setPublishedState() 0 14 2
A resetState() 0 9 2
convertToDraft() 0 1 ?
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Object\Traits;
38
39
use Apparat\Object\Domain\Model\Properties\SystemProperties;
40
41
/**
42
 * Object states trait
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Domain\Model\Object\Traits
46
 * @property SystemProperties $systemProperties
47
 */
48
trait StatesTrait
49
{
50
    /**
51
     * Object state
52
     *
53
     * @var int
54
     */
55
    protected $state = self::STATE_CLEAN;
56
    /**
57
     * Property collection states
58
     *
59
     * @var array
60
     */
61
    protected $collectionStates = [];
62
63
    /**
64
     * Return whether the object is in mutated state
65
     *
66
     * @return boolean Mutated state
67
     */
68 3
    public function hasBeenMutated()
69
    {
70 3
        return !!($this->state & self::STATE_MUTATED);
71
    }
72
73
    /**
74
     * Return whether the object is in published state
75
     *
76
     * @return boolean Published state
77
     */
78 1
    public function isPublished()
79
    {
80 1
        return $this->systemProperties->isPublished();
81
    }
82
83
    /**
84
     * Return whether the object has been deleted
85
     *
86
     * @return boolean Object is deleted
87
     */
88 45
    public function isDeleted()
89
    {
90 45
        return $this->systemProperties->isDeleted();
91
    }
92
93
    /**
94
     * Return whether the object has just been deleted
95
     *
96
     * @return boolean Object has just been deleted
97
     */
98 6
    public function hasBeenDeleted()
99
    {
100 6
        return !!($this->state & self::STATE_DELETED);
101
    }
102
103
    /**
104
     * Return whether the object has just been undeleted
105
     *
106
     * @return boolean Object has just been undeleted
107
     */
108 6
    public function hasBeenUndeleted()
109
    {
110 6
        return !!($this->state & self::STATE_UNDELETED);
111
    }
112
113
    /**
114
     * Return whether the object is in modified state
115
     *
116
     * @return boolean Modified state
117
     */
118 3
    public function hasBeenModified()
119
    {
120 3
        return !!($this->state & self::STATE_MODIFIED);
121
    }
122
123
    /**
124
     * Set the object state to mutated
125
     */
126 11
    protected function setMutatedState()
127
    {
128
        // Make this object a draft if not already the case and not just
129 11
        if (!$this->isDraft() && !$this->hasBeenPublished()) {
130
            // TODO: Send signal
131 5
            $this->convertToDraft();
132
        }
133
134
        // Enable the mutated state
135 11
        $this->state |= self::STATE_MUTATED;
136
137
        // Enable the modified state
138 11
        $this->setModifiedState();
139 11
    }
140
141
    /**
142
     * Return the object draft mode
143
     *
144
     * @return boolean Object draft mode
145
     */
146 12
    public function isDraft()
147
    {
148 12
        return $this->systemProperties->isDraft();
149
    }
150
151
    /**
152
     * Return whether the object has just been published
153
     *
154
     * @return boolean Object has just been published
155
     */
156 9
    public function hasBeenPublished()
157
    {
158 9
        return !!($this->state & self::STATE_PUBLISHED);
159
    }
160
161
    /**
162
     * Set the object state to modified
163
     */
164 14
    protected function setModifiedState()
165
    {
166
        // If this object is not in modified state yet
167 14
        if (!($this->state & self::STATE_MODIFIED)) {
168
            // TODO: Send signal
169
        }
170
171
        // Enable the modified state
172 14
        $this->state |= self::STATE_MODIFIED;
173
174
        // Update the modification timestamp
175 14
        $this->setSystemProperties($this->systemProperties->touch(), true);
176 14
    }
177
178
    /**
179
     * Set the system properties collection
180
     *
181
     * @param SystemProperties $systemProperties System property collection
182
     * @param bool $overwrite Overwrite the existing collection (if present)
183
     */
184
    abstract protected function setSystemProperties(SystemProperties $systemProperties, $overwrite = false);
185
186
    /**
187
     * Set the object state to deleted
188
     */
189 3
    protected function setDeletedState()
190
    {
191
        // If this object is not in deleted state yet
192 3
        if (!($this->state & self::STATE_DELETED)) {
193
            // TODO: Send signal
194
        }
195
196
        // Enable the deleted state
197 3
        $this->state |= self::STATE_DELETED;
198 3
        $this->state &= ~self::STATE_UNDELETED;
199
200
        // Update system properties
201 3
        $this->setSystemProperties($this->systemProperties->delete(), true);
202 3
        $this->setModifiedState();
203 3
    }
204
205
    /**
206
     * Set the object state to undeleted
207
     */
208 2
    protected function setUndeletedState()
209
    {
210
        // If this object is in deleted state
211 2
        if ($this->state & self::STATE_DELETED) {
212
            // TODO: Send signal
213
        }
214
215
        // Disable the deleted state
216 2
        $this->state |= self::STATE_UNDELETED;
217 2
        $this->state &= ~self::STATE_DELETED;
218
219
        // Update system properties
220 2
        $this->setSystemProperties($this->systemProperties->undelete(), true);
221 2
        $this->setModifiedState();
222 2
    }
223
224
    /**
225
     * Set the object state to published
226
     */
227 2
    protected function setPublishedState()
228
    {
229
        // If this object is not in published state yet
230 2
        if (!($this->state & self::STATE_PUBLISHED)) {
231
            // TODO: Send signal
232
        }
233
234
        // Set the published flag
235 2
        $this->state |= self::STATE_PUBLISHED;
236
237
        // Update system properties
238 2
        $this->setSystemProperties($this->systemProperties->publish(), true);
239 2
        $this->setModifiedState();
240 2
    }
241
242
    /**
243
     * Reset the object state
244
     */
245 45
    protected function resetState()
246
    {
247
        // If this object is not clean
248 45
        if ($this->state != self::STATE_CLEAN) {
249
            // TODO: Send signal
250
        }
251
252 45
        $this->state = self::STATE_CLEAN;
253 45
    }
254
255
    /**
256
     * Convert this object revision into a draft
257
     */
258
    abstract protected function convertToDraft();
259
}
260