Completed
Pull Request — master (#52)
by Daniel
09:53
created

QueryCriteriaBuilderCollection::checkType()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 7
nc 2
nop 1
crap 4
1
<?php
2
3
namespace KGzocha\Searcher\QueryCriteriaBuilder\Collection;
4
5
use KGzocha\Searcher\Context\SearchingContextInterface;
6
use KGzocha\Searcher\QueryCriteriaBuilder\QueryCriteriaBuilderInterface;
7
8
/**
9
 * @author Krzysztof Gzocha <[email protected]>
10
 */
11 View Code Duplication
class QueryCriteriaBuilderCollection implements QueryCriteriaBuilderCollectionInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
{
13
    /**
14
     * @var QueryCriteriaBuilderInterface[]
15
     */
16
    private $queryCriteriaBuilders;
17
18
    /**
19
     * @param QueryCriteriaBuilderInterface[] $builders array or \Traversable object
20
     */
21 8
    public function __construct($builders = [])
22
    {
23 8
        $this->queryCriteriaBuilders = [];
24 8
        $this->checkType($builders);
25 3
        foreach ($builders as $builder) {
26
            // In this way we will ensure that
27
            // every element in array has correct type
28 3
            $this->addQueryCriteriaBuilder($builder);
29
        }
30 3
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 3
    public function addQueryCriteriaBuilder(QueryCriteriaBuilderInterface $queryCriteriaBuilder)
36
    {
37 3
        $this->queryCriteriaBuilders[] = $queryCriteriaBuilder;
38
39 3
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 3
    public function getQueryCriteriaBuilders()
46
    {
47 3
        return $this->queryCriteriaBuilders;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function getQueryCriteriaBuildersForContext(
54
        SearchingContextInterface $searchingContext
55
    ) {
56 2
        return array_filter(
57 2
            $this->getQueryCriteriaBuilders(),
58 2
            function (QueryCriteriaBuilderInterface $queryCriteriaBuilder) use ($searchingContext) {
59 2
                return $queryCriteriaBuilder->supportsSearchingContext($searchingContext);
60 2
            }
61
        );
62
    }
63
64
    /**
65
     * Will ensure that provided criteria are array or traversable object.
66
     *
67
     * @param mixed $criteriaBuilders
68
     */
69 8
    private function checkType($criteriaBuilders)
70
    {
71 8
        if (is_array($criteriaBuilders)
72 8
            || $criteriaBuilders instanceof \Traversable) {
73 3
            return;
74
        }
75
76 5
        throw new \InvalidArgumentException(sprintf(
77 5
            'Provided criteria builders should be an array or \Traversable object. Given: %s',
78 5
            is_object($criteriaBuilders) ? get_class($criteriaBuilders) : gettype($criteriaBuilders)
79
        ));
80
    }
81
}
82