AbstractSet   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 65
wmc 10
lcom 1
cbo 2
ccs 29
cts 29
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toString() 0 11 2
A add() 0 6 1
A isEmpty() 0 12 3
A buildCompositeCondition() 0 13 2
A getNotEmptyConditions() 0 6 1
joinConditions() 0 1 ?
1
<?php
2
3
namespace Muffin\Conditions\Sets;
4
5
use Muffin\Conditions\AbstractCondition;
6
use Muffin\Conditions\CompositeCondition;
7
use Muffin\Escaper;
8
use Muffin\Condition;
9
10
abstract class AbstractSet extends AbstractCondition implements CompositeCondition
11
{
12
    private
13
    $conditions;
14
15 36
    public function __construct()
16
    {
17 36
        $this->conditions = array();
18 36
    }
19
20 20
    public function toString(Escaper $escaper)
21
    {
22 20
        if($this->isEmpty())
23 20
        {
24 6
            return '';
25
        }
26
27 14
        $condition = $this->buildCompositeCondition();
28
29 14
        return $condition->toString($escaper);
30
    }
31
32 32
    public function add(Condition $condition)
33
    {
34 32
        $this->conditions[] = $condition;
35
36 32
        return $this;
37
    }
38
39 36
    public function isEmpty()
40
    {
41 36
        foreach($this->conditions as $condition)
42
        {
43 32
            if(! $condition->isEmpty())
44 32
            {
45 20
                return false;
46
            }
47 20
        }
48
49 20
        return true;
50
    }
51
52 14
    private function buildCompositeCondition()
53
    {
54 14
        $conditions = $this->getNotEmptyConditions();
55
56 14
        $compositeCondition = array_shift($conditions);
57
58 14
        foreach($conditions as $condition)
59
        {
60 12
            $compositeCondition = $this->joinConditions($compositeCondition, $condition);
61 14
        }
62
63 14
        return $compositeCondition;
64
    }
65
66
    private function getNotEmptyConditions()
67
    {
68 14
        return array_filter($this->conditions, function (Condition $item) {
69 14
            return $item->isEmpty() === false;
70 14
        });
71
    }
72
73
    abstract protected function joinConditions(Condition $leftCondition, Condition $rightCondition);
74
}
75