AbstractNormalizableCommandResolverException   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 59.38%

Importance

Changes 0
Metric Value
wmc 14
lcom 2
cbo 0
dl 0
loc 138
ccs 19
cts 32
cp 0.5938
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A fromRaw() 0 8 1
A fromParameter() 0 8 1
A fromAttribute() 0 7 1
A getPropertyPath() 0 4 1
A getSource() 0 4 1
A normalize() 0 12 1
A createSourceNode() 0 12 2
A createTitleNode() 0 12 3
A cleanPropertyPath() 0 8 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Imedia\Ammit\UI\Resolver\Exception;
5
6
use Imedia\Ammit\UI\Resolver\NormalizableInterface;
7
8
/**
9
 * @author Guillaume MOREL <[email protected]>
10
 */
11
abstract class AbstractNormalizableCommandResolverException extends \InvalidArgumentException implements NormalizableInterface
12
{
13
    const SOURCE_RAW = null;
14
    const SOURCE_ATTRIBUTE = 'pointer';
15
    const SOURCE_PARAMETER = 'parameter';
16
17
    /** @var string */
18
    private $propertyPath;
19
20
    /** @var string|null */
21
    private $source;
22
23
    /**
24
     * @inheritDoc
25
     */
26
    public function __construct(string $message, string $propertyPath = null, string $source = null)
27
    {
28 1
        parent::__construct($message, 0);
29
30 1
        $this->propertyPath = self::cleanPropertyPath($propertyPath);
31 1
        $this->source = $source;
32 1
    }
33
34
    /**
35
     * From raw value directly injected
36
     */
37
    public static function fromRaw(string $message, string $propertyPath = null): AbstractNormalizableCommandResolverException
38
    {
39
        return new static(
40
            $message,
41
            self::cleanPropertyPath($propertyPath),
42
            self::SOURCE_RAW
43
        );
44
    }
45
46
    /**
47
     * From query string parameter ($_GET)
48
     */
49
    public static function fromParameter(string $message, string $propertyPath = null): AbstractNormalizableCommandResolverException
50
    {
51
        return new static(
52
            $message,
53
            self::cleanPropertyPath($propertyPath),
54
            self::SOURCE_PARAMETER
55
        );
56
    }
57
58
    /**
59
     * From attribute parameter ($_POST)
60
     */
61
    public static function fromAttribute(string $message, string $propertyPath = null): AbstractNormalizableCommandResolverException
62
    {
63
        return new static($message,
64
            self::cleanPropertyPath($propertyPath),
65
            self::SOURCE_ATTRIBUTE
66
        );
67
    }
68
69
    public function getPropertyPath(): string
70
    {
71 1
        return $this->propertyPath;
72
    }
73
74
    /**
75
     * @return null|string
76
     */
77
    public function getSource()
78
    {
79
        return $this->source;
80
    }
81
82
    /**
83
     * @inheritdoc
84
     * Normalize the Exception into a ready to be JSON encoded array
85
     * Example for Attribute $_POST:
86
     * {
87
     *     "status": "406",
88
     *     "source": { "pointer": "/data/attributes/firstName" },
89
     *     "title": "Invalid Attribute",
90
     *     "detail": "Array does not contain an element with key firstName"
91
     * }
92
     *
93
     * Example for parameter $_GET
94
     * {
95
     *     "status": "406",
96
     *     "source": { "parameter": "firstName" },
97
     *     "title":  "Invalid Query Parameter",
98
     *     "detail": "Array does not contain an element with key firstName"
99
     *     }
100
     */
101
    public function normalize(): array
102
    {
103
        return [
104 1
            'status' => 406,
105 1
            'source' => $this->createSourceNode(
106 1
                $this->propertyPath,
107 1
                $this->source
108
            ),
109 1
            'title' => $this->createTitleNode($this->source),
110 1
            'detail' => $this->getMessage(),
111
        ];
112
    }
113
114
    private function createSourceNode(string $propertyPath, string $source = null): array
115
    {
116 1
        if (self::SOURCE_ATTRIBUTE === $source) {
117
            return [
118 1
                'pointer' => '/data/attributes/' . $propertyPath
119
            ];
120
        }
121
122
        return [
123
            'parameter' => $propertyPath
124
        ];
125
    }
126
127
    private function createTitleNode(string $source = null): string
128
    {
129 1
        if (self::SOURCE_PARAMETER === $source) {
130
            return 'Invalid Query Parameter';
131
        }
132
133 1
        if (self::SOURCE_RAW === $source) {
134
            return 'Invalid Parameter';
135
        }
136
137 1
        return 'Invalid Attribute';
138
    }
139
140
    private static function cleanPropertyPath(string $propertyPath = null): string
141
    {
142 1
        if (null === $propertyPath) {
143 1
            return $propertyPath = '';
0 ignored issues
show
Unused Code introduced by
$propertyPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
144
        }
145
146 1
        return $propertyPath;
147
    }
148
}
149