ChunkListener   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 74
ccs 18
cts 18
cp 1
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A endDocument() 0 8 2
A processExtractedObject() 0 3 1
A endObject() 0 7 2
1
<?php
2
3
namespace Cerbero\JsonObjects\Listeners;
4
5
/**
6
 * The chunk listener.
7
 *
8
 */
9
class ChunkListener extends AbstractListener
10
{
11
    /**
12
     * The size of the chunk.
13
     *
14
     * @var int
15
     */
16
    protected $size;
17
18
    /**
19
     * The callback processing the chunk.
20
     *
21
     * @var callable
22
     */
23
    protected $callback;
24
25
    /**
26
     * The chunk of objects.
27
     *
28
     * @var array
29
     */
30
    protected $chunk = [];
31
32
    /**
33
     * Set the dependencies.
34
     *
35
     * @param int $size
36
     * @param callable $callback
37
     */
38 4
    public function __construct(int $size, callable $callback)
39
    {
40 4
        $this->size = $size;
41 4
        $this->callback = $callback;
42 4
    }
43
44
    /**
45
     * Listen to the end of the object.
46
     *
47
     * @return void
48
     */
49 4
    public function endObject(): void
50
    {
51 4
        parent::endObject();
52
53 4
        if (count($this->chunk) === $this->size) {
54 4
            call_user_func($this->callback, $this->chunk);
55 1
            $this->chunk = [];
56
        }
57 4
    }
58
59
    /**
60
     * Process the given extracted object.
61
     *
62
     * @param array $object
63
     * @return void
64
     */
65 4
    protected function processExtractedObject(array $object): void
66
    {
67 4
        $this->chunk[] = $object;
68 4
    }
69
70
    /**
71
     * Listen to the end of the document.
72
     *
73
     * @return void
74
     */
75 1
    public function endDocument(): void
76
    {
77 1
        if (!empty($this->chunk)) {
78 1
            call_user_func($this->callback, $this->chunk);
79 1
            $this->chunk = [];
80
        }
81
82 1
        parent::endDocument();
83 1
    }
84
}
85