Passed
Push — master ( 2bfb13...b8de7b )
by De Cramer
02:14
created

ChainProcessor::processItems()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 3
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oliverde8\Component\PhpEtl;
4
5
use Oliverde8\Component\PhpEtl\ChainOperation\ChainOperationInterface;
6
use Oliverde8\Component\PhpEtl\Item\ChainBreakItem;
7
use Oliverde8\Component\PhpEtl\Item\DataItem;
8
use Oliverde8\Component\PhpEtl\Item\GroupedItemInterface;
9
use Oliverde8\Component\PhpEtl\Item\ItemInterface;
10
use Oliverde8\Component\PhpEtl\Item\StopItem;
11
12
/**
13
 * Class ChainProcessor
14
 *
15
 * @author    de Cramer Oliver<[email protected]>
16
 * @copyright 2018 Oliverde8
17
 * @package Oliverde8\Component\PhpEtl
18
 */
19
class ChainProcessor
20
{
21
    /** @var ChainOperationInterface[] */
22
    protected $chainLinks = [];
23
24
    /** @var string[] */
25
    protected $chainLinkNames = [];
26
27
    /**
28
     * ChainProcessor constructor.
29
     *
30
     * @param ChainOperationInterface[] $chainLinks
31
     */
32 4
    public function __construct(array $chainLinks)
33
    {
34 4
        $this->chainLinkNames = array_keys($chainLinks);
35 4
        $this->chainLinks = array_values($chainLinks);
36 4
    }
37
38
    /**
39
     * Process items.
40
     *
41
     * @param \Iterator $items
42
     * @param $context
43
     */
44 4
    public function process(\Iterator $items, $context)
45
    {
46 4
        $this->processItems($items, 0, $context);
47 4
    }
48
49
    /**
50
     * Process list of items with chain starting at $startAt.
51
     *
52
     * @param \Iterator $items
53
     * @param int $startAt
54
     * @param array $context
55
     *
56
     * @return ItemInterface
57
     */
58 4
    protected function processItems(\Iterator $items, $startAt, &$context)
59
    {
60 4
        foreach ($items as $item) {
61 4
            $dataItem = new DataItem($item);
62 4
            $this->processItem($dataItem, $startAt, $context);
63
        }
64
65 4
        $stopItem = new StopItem();
66 4
        while ($this->processItem($stopItem, $startAt, $context) !== $stopItem);
67
68 4
        return $stopItem;
69
    }
70
71
    /**
72
     * Process an item, with chains starting at.
73
     *
74
     * @param ItemInterface $item
75
     * @param int $startAt
76
     * @param array $context
77
     *
78
     * @return ItemInterface
79
     */
80 4
    protected function processItem(ItemInterface $item, $startAt, &$context)
81
    {
82 4
        for ($chainNumber = $startAt; $chainNumber < count($this->chainLinks); $chainNumber++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
83 4
            $item = $this->chainLinks[$chainNumber]->process($item, $context);
84
85 4
            if ($item instanceof GroupedItemInterface) {
86 2
                $this->processItems($item->getIterator(), $chainNumber + 1, $context);
87
88 2
                return new StopItem();
89 4
            } else if ($item instanceof ChainBreakItem) {
90 3
                return $item;
91
            }
92
        }
93
94 4
        return $item;
95
    }
96
}
97