Batching   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 43
ccs 19
cts 20
cp 0.95
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A init() 0 4 1
A step() 0 12 2
A complete() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Arnaud LEMAIRE  <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fp\Reducer;
13
14
class Batching implements Reducer
15
{
16
    protected $next_reducer;
17
    protected $batch_size;
18
    protected $current_batch = [];
19
20 4
    public function __construct(Reducer $next_reducer, $batch_size)
21
    {
22 4
        if (!is_int($batch_size)) {
23
            throw \InvalidArgumentException('argument #2 of Batching::__construct must be an integer');
24
        }
25
26 4
        $this->batch_size = $batch_size;
27 4
        $this->next_reducer = $next_reducer;
28 4
    }
29
30 3
    public function init()
31
    {
32 3
        return $this->next_reducer->init();
33
    }
34
35 3
    public function step($result, $current)
36
    {
37 3
        $this->current_batch[] = $current;
38 3
        if (count($this->current_batch) >= $this->batch_size) {
39 3
            $batch = $this->current_batch;
40 3
            $this->current_batch = [];
41
42 3
            return $this->next_reducer->step($result, $batch);
43
        }
44
45 3
        return $result;
46
    }
47
48 3
    public function complete($result)
49
    {
50 3
        if (count($this->current_batch) > 0) {
51 1
            $result = $this->next_reducer->step($result, $this->current_batch);
52 1
        }
53
54 3
        return $this->next_reducer->complete($result);
55
    }
56
}
57