Completed
Push — master ( 48c4f2...a2b4be )
by Neomerx
01:38
created

addValidationAttributeError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php namespace Limoncello\Flute\Validation\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\Container\Traits\HasContainerTrait;
20
use Limoncello\Contracts\L10n\FormatterInterface;
21
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
22
use Limoncello\Flute\Http\JsonApiResponse;
23
use Limoncello\Flute\Validation\Traits\HasValidationFormatterTrait;
24
use Limoncello\Validation\Contracts\Errors\ErrorInterface;
25
use Neomerx\JsonApi\Exceptions\ErrorCollection;
26
use Psr\Container\ContainerInterface;
27
28
/**
29
 * @package Limoncello\Flute
30
 */
31
class JsonApiErrorCollection extends ErrorCollection
32
{
33
    use HasContainerTrait, HasValidationFormatterTrait;
34
35
    /**
36
     * @var int
37
     */
38
    private $errorStatus;
39
40
    /**
41
     * @var FormatterInterface|null
42
     */
43
    private $messageFormatter;
44
45
    /**
46
     * @param ContainerInterface $container
47
     * @param int                $errorStatus
48
     */
49 20
    public function __construct(
50
        ContainerInterface $container,
51
        int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY
52
    ) {
53 20
        $this->setContainer($container);
54 20
        $this->errorStatus = $errorStatus;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60 2
    public function addValidationAttributeError(ErrorInterface $error)
61
    {
62 2
        $title  = $this->getInvalidValueMessage();
63 2
        $detail = $this->getValidationMessage($error);
64 2
        $this->addDataAttributeError($error->getParameterName(), $title, $detail, $this->getErrorStatus());
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 3
    public function addValidationRelationshipError(ErrorInterface $error)
71
    {
72 3
        $title  = $this->getInvalidValueMessage();
73 3
        $detail = $this->getValidationMessage($error);
74 3
        $this->addRelationshipError($error->getParameterName(), $title, $detail, $this->getErrorStatus());
75
    }
76
77
    /**
78
     * @return int
79
     */
80 3
    protected function getErrorStatus(): int
81
    {
82 3
        return $this->errorStatus;
83
    }
84
85
    /**
86
     * @return string
87
     */
88 3
    private function getInvalidValueMessage(): string
89
    {
90 3
        $message = $this->getMessageFormatter()->formatMessage(ErrorCodes::INVALID_VALUE);
91
92 3
        return $message;
93
    }
94
95
    /**
96
     * @param ErrorInterface $error
97
     *
98
     * @return string
99
     */
100 3
    private function getValidationMessage(ErrorInterface $error): string
101
    {
102 3
        $context = $error->getMessageContext();
103 3
        $args    = $context === null ? [] : $context;
104 3
        $message = $this->getMessageFormatter()->formatMessage($error->getMessageCode(), $args);
105
106 3
        return $message;
107
    }
108
109
    /**
110
     * @return FormatterInterface
111
     */
112 3
    private function getMessageFormatter(): FormatterInterface
113
    {
114 3
        if ($this->messageFormatter === null) {
115 3
            $this->messageFormatter = $this->createValidationFormatter();
116
        }
117
118 3
        return $this->messageFormatter;
119
    }
120
}
121