ChunkOperation::apply()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 17
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * File was created 09.06.2015 06:50
4
 *
5
 * @author Karsten J. Gerber <[email protected]>
6
 */
7
8
namespace PeekAndPoke\Component\Psi\Operation\FullSet;
9
10
use PeekAndPoke\Component\Psi\FullSetOperation;
11
12
/**
13
 * @author Karsten J. Gerber <[email protected]>
14
 */
15
class ChunkOperation implements FullSetOperation
16
{
17
    /** @var int */
18
    private $chunkSize;
19
20
    /**
21
     * @param int $chunkSize
22
     */
23 1
    public function __construct($chunkSize)
24
    {
25 1
        $this->chunkSize = max(1, (int) $chunkSize);
26 1
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public function apply(\Iterator $set)
32
    {
33 1
        $group   = 0;
34 1
        $counter = $this->chunkSize;
35 1
        $ret     = [];
36
37 1
        foreach ($set as $item) {
38
39 1
            $ret[$group][] = $item;
40
41 1
            if (--$counter === 0) {
42 1
                $counter = $this->chunkSize;
43 1
                ++$group;
44
            }
45
        }
46
47 1
        return new \ArrayIterator($ret);
48
    }
49
}
50