Operation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromDecodedJSON() 0 8 1
A getPath() 0 4 1
A assertValidOperationContent() 0 12 3
A assertKnownOperation() 0 15 2
1
<?php
2
declare(strict_types=1);
3
4
namespace gamringer\JSONPatch;
5
6
abstract class Operation
7
{
8
9
    const OP_TEST = 'test';
10
    const OP_ADD = 'add';
11
    const OP_REMOVE = 'remove';
12
    const OP_REPLACE = 'replace';
13
    const OP_MOVE = 'move';
14
    const OP_COPY = 'copy';
15
16
    protected $path = '';
17
18
    public static function fromDecodedJSON($operationContent)
19
    {
20
        self::assertValidOperationContent($operationContent);
21
22
        $operationClass = __NAMESPACE__.'\\Operation\\'.ucfirst($operationContent->op);
23
24
        return $operationClass::fromDecodedJSON($operationContent);
25
    }
26
27
    public function getPath(): string
28
    {
29
        return $this->path;
30
    }
31
32
    private static function assertValidOperationContent($operationContent)
33
    {
34
        if (!($operationContent instanceof \stdClass)) {
35
            throw new Operation\Exception('Operation Content is not an object');
36
        }
37
38
        if (!isset($operationContent->op)) {
39
            throw new Operation\Exception('All Operations must contain exactly one "op" member');
40
        }
41
42
        self::assertKnownOperation($operationContent->op);
43
    }
44
45
    private static function assertKnownOperation($operation)
46
    {
47
        $knownOperations = [
48
            self::OP_TEST,
49
            self::OP_ADD,
50
            self::OP_REMOVE,
51
            self::OP_REPLACE,
52
            self::OP_MOVE,
53
            self::OP_COPY
54
        ];
55
        
56
        if (!in_array($operation, $knownOperations)) {
57
            throw new Operation\Exception('Operation must be one of "'.implode('", "', $knownOperations).'"');
58
        }
59
    }
60
}
61