Completed
Push — master ( c36253...8f6ddc )
by Krzysztof
7s
created

ChainSearch   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 14
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 96
ccs 34
cts 34
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B search() 0 32 6
B validateCells() 0 19 5
A shouldSkipResults() 0 6 2
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
 * This class represents whole chain of searchers, but it behaves like regular searcher.
11
 * It will perform all (not skipped) sub-searches, transform theirs results into CriteriaCollection
12
 * and pass those criteria to next sub-search.
13
 * At the end it will return collection of all the results from all the steps.
14
 *
15
 * @author Krzysztof Gzocha <[email protected]>
16
 */
17
class ChainSearch implements SearcherInterface
18
{
19
    /**
20
     * @var CellInterface[]
21
     */
22
    private $cells;
23
24
    /**
25
     * @param CellInterface[] $cells
26
     */
27 5
    public function __construct(array $cells)
28
    {
29 5
        $this->validateCells($cells);
30 3
        $this->cells = $cells;
31 3
    }
32
33
    /**
34
     * Will perform multiple sub-searches.
35
     * Results from first search will be transformed and passed as CriteriaCollection
36
     * to another sub-search.
37
     * Whole process will return collection of results from each sub-search.
38
     *
39
     * @param CriteriaCollectionInterface $criteriaCollection
40
     *
41
     * @return ResultCollection
42
     */
43 3
    public function search(
44
        CriteriaCollectionInterface $criteriaCollection
45
    ) {
46 3
        $previousCriteria = null;
47 3
        $previousResults = null;
48 3
        $resultsArray = [];
49
50 3
        foreach ($this->cells as $cell) {
51 3
            if ($this->shouldSkipResults($cell->getTransformer(), $previousResults)) {
52 1
                continue;
53
            }
54
55
            // Assumed only for first iteration
56 3
            if (!$previousCriteria) {
57 3
                $previousCriteria = $criteriaCollection;
58
            }
59
60 3
            $previousResults = $cell->getSearcher()->search($previousCriteria);
61 3
            if ($cell->hasTransformer()) {
62 3
                $previousCriteria = $cell->getTransformer()->transform($previousResults);
63
            }
64
65 3
            if ($cell->getName()) {
66 2
                $resultsArray[$cell->getName()] = $previousResults;
67 2
                continue;
68
            }
69
70 1
            array_push($resultsArray, $previousResults);
71
        }
72
73 3
        return new ResultCollection($resultsArray);
74
    }
75
76
    /**
77
     * @param CellInterface[] $cells
78
     * @throws \InvalidArgumentException
79
     */
80 5
    private function validateCells(array $cells)
81
    {
82 5
        if (2 > count($cells)) {
83 1
            throw new \InvalidArgumentException(
84 1
                'At least two searchers are required to create a chain'
85
            );
86
        }
87
88 4
        foreach ($cells as $cell) {
89 4
            if (is_object($cell) && $cell instanceof CellInterface) {
90 4
                continue;
91
            }
92
93 1
            throw new \InvalidArgumentException(sprintf(
94 1
                'All cells passed to %s should be object and must implement CellInterface',
95 1
                __CLASS__
96
            ));
97
        }
98 3
    }
99
100
    /**
101
     * @param TransformerInterface $transformer
102
     * @param mixed                $results
103
     *
104
     * @return bool
105
     */
106 3
    private function shouldSkipResults(
107
        TransformerInterface $transformer,
108
        $results
109
    ) {
110 3
        return null !== $results && $transformer->skip($results);
111
    }
112
}
113