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

MiddlewareCommon   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A enabled() 0 10 3
A serialize() 0 4 1
A unserialize() 0 4 1
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