Passed
Push — master ( 76b76b...ca78d8 )
by Edward
02:11
created

EventDecoder::exportEvents()   C

Complexity

Conditions 12
Paths 21

Size

Total Lines 49
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 36
c 1
b 0
f 0
nc 21
nop 1
dl 0
loc 49
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Data\Export;
5
6
use Iterator;
7
use Remorhaz\JSON\Data\Event\AfterArrayEventInterface;
8
use Remorhaz\JSON\Data\Event\AfterElementEventInterface;
9
use Remorhaz\JSON\Data\Event\AfterObjectEventInterface;
10
use Remorhaz\JSON\Data\Event\AfterPropertyEventInterface;
11
use Remorhaz\JSON\Data\Event\BeforeArrayEventInterface;
12
use Remorhaz\JSON\Data\Event\BeforeElementEventInterface;
13
use Remorhaz\JSON\Data\Event\BeforeObjectEventInterface;
14
use Remorhaz\JSON\Data\Event\BeforePropertyEventInterface;
15
use Remorhaz\JSON\Data\Event\EventInterface;
16
use Remorhaz\JSON\Data\Event\ScalarEventInterface;
17
use Remorhaz\JSON\Data\Value\DecodedJson\NodeValueFactory;
18
use Remorhaz\JSON\Data\Value\NodeValueInterface;
19
20
final class EventDecoder implements EventDecoderInterface
21
{
22
23
    /**
24
     * @param Iterator|EventInterface[] $events
25
     * @return NodeValueInterface|null
26
     */
27
    public function exportEvents(Iterator $events): ?NodeValueInterface
28
    {
29
        $buffer = [];
30
        $structures = [];
31
        $structure = null;
32
        foreach ($events as $event) {
33
            switch (true) {
34
                case $event instanceof ScalarEventInterface:
35
                    $buffer[] = $event->getData();
36
                    break;
37
38
                case $event instanceof BeforeArrayEventInterface:
39
                case $event instanceof BeforeObjectEventInterface:
40
                    $structures[] = $structure;
41
                    $structure = [];
42
                    break;
43
44
                case $event instanceof BeforeElementEventInterface:
45
                case $event instanceof BeforePropertyEventInterface:
46
                    break;
47
48
                case $event instanceof AfterElementEventInterface:
49
                    $structure[$event->getIndex()] = array_pop($buffer);
50
                    break;
51
52
                case $event instanceof AfterPropertyEventInterface:
53
                    $structure[$event->getName()] = array_pop($buffer);
54
                    break;
55
56
                case $event instanceof AfterArrayEventInterface:
57
                    $buffer[] = $structure;
58
                    $structure = array_pop($structures);
59
                    break;
60
61
                case $event instanceof AfterObjectEventInterface:
62
                    $buffer[] = (object) $structure;
63
                    $structure = array_pop($structures);
64
                    break;
65
66
                default:
67
                    throw new Exception\UnknownEventException($event);
68
            }
69
        }
70
        if (empty($buffer)) {
71
            return null;
72
        }
73
        $data = array_pop($buffer);
74
75
        return (new NodeValueFactory)->createValue($data);
76
    }
77
78
    public function exportExistingEvents(Iterator $events): NodeValueInterface
79
    {
80
        $value = $this->exportEvents($events);
81
        if (isset($value)) {
82
            return $value;
83
        }
84
85
        throw new Exception\NoValueToExportException;
86
    }
87
}
88