Passed
Pull Request — master (#185)
by
unknown
02:58
created

ScopeAggregate::add()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Select\Scope;
6
7
use Cycle\ORM\Select\QueryBuilder;
8
use Cycle\ORM\Select\ScopeInterface;
9
10
/**
11
 * Class ScopeAggregate.
12
 *
13
 * @implements \IteratorAggregate<ScopeInterface>
14
 */
15
final class ScopeAggregate implements ScopeInterface, \IteratorAggregate
16
{
17
    /**
18
     * @var ScopeInterface[]
19
     */
20
    private $scopes = [];
21
22
    public function __construct(ScopeInterface ...$scopes)
23
    {
24
        foreach ($scopes as $scope) {
25
            $this->add($scope);
26
        }
27
    }
28
29
    public function add(ScopeInterface $scope): void
30
    {
31
        if ($scope instanceof self) {
32
            foreach ($scope as $s) {
33
                $this->add($s);
34
            }
35
        }
36
37
        $this->scopes[] = $scope;
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function apply(QueryBuilder $query): void
44
    {
45
        foreach ($this->scopes as $scope) {
46
            $scope->apply($query);
47
        }
48
    }
49
50
    /**
51
     * @return \Traversable<ScopeInterface>
52
     */
53
    public function getIterator(): \Traversable
54
    {
55
        return new \ArrayIterator($this->scopes);
56
    }
57
}
58