EventDecoder::exportEvents()   C
last analyzed

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