Passed
Push — master ( a0b4b8...4849e7 )
by Damian
03:09
created

MiddlewareCommon::setDisableFlag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SilverStripe\Config\Middleware;
4
5
/**
6
 * Abstract flag-aware middleware
7
 */
8
trait MiddlewareCommon
9
{
10
    /**
11
     * Disable flag
12
     *
13
     * @var int
14
     */
15
    protected $disableFlag = 0;
16
17
    /**
18
     * Set flag to use to disable this middleware
19
     *
20
     * @param int $disableFlag
21
     * @return $this
22
     */
23 4
    public function setDisableFlag($disableFlag)
24
    {
25 4
        $this->disableFlag = $disableFlag;
26 4
        return $this;
27
    }
28
29
    /**
30
     * Get flag to use to disable this middleware
31
     *
32
     * @return int
33
     */
34
    public function getDisableFlag()
35
    {
36
        return $this->disableFlag;
37
    }
38
39
    /**
40
     * Check if this middlware is enabled
41
     *
42
     * @param int|true $excludeMiddleware
43
     * @return bool
44
     */
45 4
    protected function enabled($excludeMiddleware)
46
    {
47 4
        if ($excludeMiddleware === true) {
48
            return false;
49
        }
50 4
        if (!$this->disableFlag) {
51 4
            return true;
52
        }
53
        return ($excludeMiddleware & $this->disableFlag) !== $this->disableFlag;
54
    }
55
56
    public function serialize()
57
    {
58
        return json_encode([$this->disableFlag]);
59
    }
60
61
    public function unserialize($serialized)
62
    {
63
        list($this->disableFlag) = json_decode($serialized, true);
64
    }
65
}
66