FreezableTrait::setFrozen()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Clippings\Freezable;
4
5
/**
6
 * Freezable trait:
7
 * - Store frozen state
8
 * - Call abstract methods for actual freezing and unfreezing.
9
 *
10
 * @author    Haralan Dobrev <[email protected]>
11
 * @copyright 2014, Clippings Ltd.
12
 * @license   http://spdx.org/licenses/BSD-3-Clause
13
 */
14
trait FreezableTrait
15
{
16
    /**
17
     * Set `frozen` and execute `performFreeze()`
18
     *
19
     * @return self
20
     */
21 2
    public function freeze()
22
    {
23 2
        if (! $this->isFrozen()) {
24 2
            $this->performFreeze();
25 2
            $this->setFrozen(true);
26
        }
27
28 2
        return $this;
29
    }
30
31
    /**
32
     * Unset `frozen` and execute `performUnfreeze()`
33
     *
34
     * @return self
35
     */
36 2
    public function unfreeze()
37
    {
38 2
        if ($this->isFrozen()) {
39 2
            $this->performUnfreeze();
40 2
            $this->setFrozen(false);
41
        }
42
43 2
        return $this;
44
    }
45
46
    /**
47
     * Is the object frozen
48
     *
49
     * @return boolean
50
     */
51
    abstract public function isFrozen();
52
53
    /**
54
     * Set the frozen state
55
     *
56
     * @param boolean $frozen
57
     * @return self
58
     */
59
    abstract protected function setFrozen($frozen);
60
61
    /**
62
     * Perform actual freezing.
63
     * It could be achieved by simply storing dynamic value returned
64
     * by a function in a property.
65
     *
66
     * @return void
67
     */
68
    abstract public function performFreeze();
69
70
    /**
71
     * Perform actual unfreezing.
72
     * It should be the opposite of the freezeing.
73
     * E.g. setting the property value to null.
74
     *
75
     * @return void
76
     */
77
    abstract public function performUnfreeze();
78
}
79