Remove   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 7
Bugs 2 Features 1
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 45
c 7
b 2
f 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A apply() 0 8 2
A revert() 0 4 1
A fromDecodedJSON() 0 6 1
A assertValidOperationContent() 0 6 2
A __toString() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace gamringer\JSONPatch\Operation;
5
6
use gamringer\JSONPatch\Operation;
7
use gamringer\JSONPointer\Pointer;
8
use gamringer\JSONPointer;
9
10
class Remove extends Operation implements Atomic
11
{
12
    private $previousValue;
13
    
14
    public function __construct(string $path)
15
    {
16
        $this->path = $path;
17
    }
18
19
    public function apply(Pointer $target)
20
    {
21
        try {
22
            $this->previousValue = $target->remove($this->path);
23
        } catch (JSONPointer\Exception $e) {
24
            throw new Exception($e->getMessage(), 0, $e);
25
        }
26
    }
27
28
    public function revert(Pointer $target)
29
    {
30
        $target->insert($this->path, $this->previousValue);
31
    }
32
33
    public static function fromDecodedJSON($operationContent): self
34
    {
35
        self::assertValidOperationContent($operationContent);
36
37
        return new self($operationContent->path);
38
    }
39
40
    private static function assertValidOperationContent($operationContent)
41
    {
42
        if (!property_exists($operationContent, 'path')) {
43
            throw new Operation\Exception('"Remove" Operations must contain a "path" member');
44
        }
45
    }
46
47
    public function __toString(): string
48
    {
49
        return json_encode([
50
            'op' => Operation::OP_REMOVE,
51
            'path' => $this->path,
52
        ]);
53
    }
54
}
55