Completed
Push — master ( deca00...5388ff )
by Sam
24s
created

MiddlewareCommon::unserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Core\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
    public function __construct($disableFlag = 0)
18
    {
19
        $this->disableFlag = $disableFlag;
20
    }
21
22
    /**
23
     * Check if this middlware is enabled
24
     *
25
     * @param int|true $excludeMiddleware
26
     * @return bool
27
     */
28
    protected function enabled($excludeMiddleware)
29
    {
30
        if ($excludeMiddleware === true) {
31
            return false;
32
        }
33
        if (!$this->disableFlag) {
34
            return true;
35
        }
36
        return ($excludeMiddleware & $this->disableFlag) !== $this->disableFlag;
37
    }
38
39
    public function serialize()
40
    {
41
        return json_encode([$this->disableFlag]);
42
    }
43
44
    public function unserialize($serialized)
45
    {
46
        list($this->disableFlag) = json_decode($serialized, true);
47
    }
48
}
49