Completed
Push — develop ( 46135c...90e0de )
by Neomerx
03:10
created

JsonApiErrorCollection::getMessageFormatter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Limoncello\Flute\Validation\JsonApi\Execution;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\L10n\FormatterInterface;
20
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
21
use Limoncello\Flute\Http\JsonApiResponse;
22
use Limoncello\Validation\Contracts\Errors\ErrorInterface;
23
use Neomerx\JsonApi\Exceptions\ErrorCollection;
24
25
/**
26
 * @package Limoncello\Flute
27
 */
28
class JsonApiErrorCollection extends ErrorCollection
29
{
30
    /**
31
     * @var int
32
     */
33
    private $errorStatus;
34
35
    /**
36
     * @var FormatterInterface
37
     */
38
    private $messageFormatter;
39
40
    /**
41
     * @param FormatterInterface $formatter
42
     * @param int                $errorStatus
43
     */
44
    public function __construct(
45
        FormatterInterface $formatter,
46
        int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY
47
    ) {
48
        $this->messageFormatter = $formatter;
49
        $this->errorStatus      = $errorStatus;
50
    }
51 54
52
    /**
53
     * @inheritdoc
54
     */
55 54
    public function addValidationIdError(ErrorInterface $error)
56 54
    {
57
        $title  = $this->getInvalidValueMessage();
58
        $detail = $this->getValidationMessage($error);
59
        $this->addDataIdError($title, $detail, $this->getErrorStatus());
60
    }
61
62 2
    /**
63
     * @inheritdoc
64 2
     */
65 2
    public function addValidationTypeError(ErrorInterface $error)
66 2
    {
67
        $title  = $this->getInvalidValueMessage();
68
        $detail = $this->getValidationMessage($error);
69
        $this->addDataTypeError($title, $detail, $this->getErrorStatus());
70
    }
71
72 3
    /**
73
     * @inheritdoc
74 3
     */
75 3
    public function addValidationAttributeError(ErrorInterface $error)
76 3
    {
77
        $title  = $this->getInvalidValueMessage();
78
        $detail = $this->getValidationMessage($error);
79
        $this->addDataAttributeError($error->getParameterName(), $title, $detail, $this->getErrorStatus());
80
    }
81
82 6
    /**
83
     * @inheritdoc
84 6
     */
85 6
    public function addValidationRelationshipError(ErrorInterface $error)
86 6
    {
87
        $title  = $this->getInvalidValueMessage();
88
        $detail = $this->getValidationMessage($error);
89
        $this->addRelationshipError($error->getParameterName(), $title, $detail, $this->getErrorStatus());
90
    }
91
92 9
    /**
93
     * @inheritdoc
94 9
     */
95
    public function addValidationQueryError(string $paramName, ErrorInterface $error)
96
    {
97
        $title  = $this->getInvalidValueMessage();
98
        $detail = $this->getValidationMessage($error);
99
        $this->addQueryParameterError($paramName, $title, $detail, $this->getErrorStatus());
100
    }
101
102
    /**
103 9
     * @return int
104
     */
105 9
    protected function getErrorStatus(): int
106
    {
107 9
        return $this->errorStatus;
108
    }
109
110
    /**
111
     *
112
     * @return string
113
     */
114
    private function getInvalidValueMessage(): string
115
    {
116
        $message = $this->getMessageFormatter()->formatMessage(ErrorCodes::INVALID_VALUE);
117
118 9
        return $message;
119
    }
120 9
121 9
    /**
122 9
     * @param ErrorInterface $error
123
     *
124 9
     * @return string
125
     */
126
    private function getValidationMessage(ErrorInterface $error): string
127
    {
128
        $context = $error->getMessageContext();
129
        $args    = $context === null ? [] : $context;
130
        $message = $this->getMessageFormatter()->formatMessage($error->getMessageCode(), $args);
131
132
        return $message;
133 9
    }
134
135 9
    /**
136 9
     * @return FormatterInterface
137
     */
138
    private function getMessageFormatter(): FormatterInterface
139 9
    {
140
        return $this->messageFormatter;
141
    }
142
}
143