Completed
Pull Request — master (#67)
by Krzysztof
12:49
created

ChainSearch::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace KGzocha\Searcher\Chain;
4
5
use KGzocha\Searcher\Criteria\Collection\CriteriaCollectionInterface;
6
use KGzocha\Searcher\Result\ResultCollection;
7
use KGzocha\Searcher\SearcherInterface;
8
9
/**
10
 * @author Krzysztof Gzocha <[email protected]>
11
 */
12
class ChainSearch implements SearcherInterface
13
{
14
    /**
15
     * @var Cell[]
16
     */
17
    private $cells;
18
19
    /**
20
     * @param Cell[] $cells
21
     */
22 1
    public function __construct(array $cells)
23
    {
24 1
        $this->validateCells($cells);
25 1
        $this->cells = $cells;
26 1
    }
27
28
    /**
29
     * Will perform multiple sub-searches.
30
     * Results from first search will be transformed and passed as CriteriaCollection
31
     * to another sub-search.
32
     * Whole process will return collection of results from each sub-search.
33
     *
34
     * @param CriteriaCollectionInterface $criteriaCollection
35
     *
36
     * @return ResultCollection
37
     */
38 1
    public function search(
39
        CriteriaCollectionInterface $criteriaCollection
40
    ) {
41 1
        $previousCriteria = null;
42 1
        $previousResults = null;
43 1
        $resultsArray = [];
44
45 1
        foreach ($this->cells as $cell) {
46 1
            if ($this->shouldSkipResults($cell->getTransformer(), $previousResults)) {
47
                continue;
48
            }
49
50
            // Assumed only for first iteration
51 1
            if (!$previousCriteria) {
52 1
                $previousCriteria = $criteriaCollection;
53
            }
54
55 1
            $previousResults = $cell->getSearcher()->search($previousCriteria);
56 1
            if ($cell->hasTransformer()) {
57 1
                $previousCriteria = $cell->getTransformer()->transform($previousResults);
58
            }
59
60 1
            if ($cell->getName()) {
61 1
                $resultsArray[$cell->getName()] = $previousResults;
62 1
                continue;
63
            }
64
65
            array_push($resultsArray, $previousResults);
66
        }
67
68 1
        return new ResultCollection($resultsArray);
69
    }
70
71
    /**
72
     * @param array $cells
73
     * @throws \InvalidArgumentException
74
     */
75 1
    private function validateCells(array $cells)
76
    {
77 1
        if (2 > count($cells)) {
78
            throw new \InvalidArgumentException(
79
                'At least two searchers are required to create a chain'
80
            );
81
        }
82 1
    }
83
84
    /**
85
     * @param TransformerInterface $transformer
86
     * @param mixed                $results
87
     *
88
     * @return bool
89
     */
90 1
    private function shouldSkipResults(
91
        TransformerInterface $transformer,
92
        $results
93
    ) {
94 1
        return null !== $results && $transformer->skip($results);
95
    }
96
}
97