Test Failed
Push — master ( 918fe6...6f63e6 )
by Julien
09:55 queued 05:39
created

SkippableTrait::staticDisable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model\Behavior;
13
14
/**
15
 * Allow to enable or disable trait
16
 * on the current model instance ($enabled)
17
 * or globally for every model instance ($staticEnabled)
18
 */
19
trait SkippableTrait
20
{
21
    public bool $enabled = true;
22
    public static bool $staticEnabled = true;
23
    
24
    /**
25
     * Return true if the behavior is enabled
26
     * on the current model instance
27
     */
28
    public function getEnabled(): bool
29
    {
30
        return $this->enabled;
31
    }
32
    
33
    /**
34
     * Set true to enable the behavior
35
     * on the current model instance
36
     */
37
    public function setEnabled(bool $enabled): void
38
    {
39
        $this->enabled = $enabled;
40
    }
41
    
42
    /**
43
     * Return true if the behavior is enabled
44
     * globally for every model instance
45
     */
46
    public static function getStaticEnabled(): bool
47
    {
48
        return self::$staticEnabled;
49
    }
50
    
51
    /**
52
     * Set true to enable the behavior
53
     * globally for every model instance
54
     */
55
    public static function setStaticEnabled(bool $staticEnabled): void
56
    {
57
        self::$staticEnabled = $staticEnabled;
58
    }
59
    
60
    /**
61
     * Enable the behavior
62
     * on the current model instance
63
     */
64
    public function enable(): void
65
    {
66
        $this->setEnabled(true);
67
    }
68
    
69
    /**
70
     * Disable the behavior
71
     * on the current model instance
72
     */
73
    public function disable(): void
74
    {
75
        $this->setEnabled(false);
76
    }
77
    
78
    /**
79
     * Enable the behavior
80
     * globally for every model instance
81
     */
82
    public static function staticEnable(): void
83
    {
84
        self::setStaticEnabled(true);
85
    }
86
    
87
    /**
88
     * Disable the behavior
89
     * globally for every model instance
90
     */
91
    public static function staticDisable(): void
92
    {
93
        self::setStaticEnabled(false);
94
    }
95
    
96
    /**
97
     * Return true if the behavior is enabled
98
     * on the current model instance and globally
99
     */
100
    public function isEnabled(): bool
101
    {
102
        return $this->getEnabled() && self::getStaticEnabled();
103
    }
104
    
105
    /**
106
     * Return true if the behavior is enabled
107
     * on the current model instance and globally
108
     */
109
    public function isDisabled(): bool
110
    {
111
        return !$this->isEnabled();
112
    }
113
}
114