AppendPropertyMutation::reset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Pointer\Processor\Mutator;
6
7
use Generator;
8
use Remorhaz\JSON\Data\Event\AfterObjectEventInterface;
9
use Remorhaz\JSON\Data\Event\AfterPropertyEvent;
10
use Remorhaz\JSON\Data\Event\BeforePropertyEvent;
11
use Remorhaz\JSON\Data\Event\EventInterface;
12
use Remorhaz\JSON\Data\Path\PathInterface;
13
use Remorhaz\JSON\Data\Value\NodeValueInterface;
14
use Remorhaz\JSON\Data\Walker\MutationInterface;
15
use Remorhaz\JSON\Data\Walker\ValueWalkerInterface;
16
17
final class AppendPropertyMutation implements MutationInterface
18
{
19
20
    private $value;
21
22
    private $path;
23
24
    private $propertyName;
25
26
    public function __construct(NodeValueInterface $value, PathInterface $path, string $propertyName)
27
    {
28
        $this->value = $value;
29
        $this->path = $path;
30
        $this->propertyName = $propertyName;
31
    }
32
33
    public function __invoke(EventInterface $event, ValueWalkerInterface $valueWalker)
34
    {
35
        return $this->createEventGenerator($event, $valueWalker);
36
    }
37
38
    public function reset(): void
39
    {
40
    }
41
42
    private function createEventGenerator(EventInterface $event, ValueWalkerInterface $valueWalker): Generator
43
    {
44
        if (!$this->path->equals($event->getPath())) {
45
            yield $event;
46
47
            return;
48
        }
49
50
        if ($event instanceof AfterObjectEventInterface) {
51
            $propertyPath = $this->path->copyWithProperty($this->propertyName);
52
            yield new BeforePropertyEvent($this->propertyName, $propertyPath);
53
            yield from $valueWalker->createEventIterator($this->value, $propertyPath);
54
            yield new AfterPropertyEvent($this->propertyName, $propertyPath);
55
        }
56
57
        yield $event;
58
    }
59
}
60