AppendPropertyMutation   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 41
ccs 0
cts 19
cp 0
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createEventGenerator() 0 16 3
A __invoke() 0 3 1
A reset() 0 2 1
A __construct() 0 5 1
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