Completed
Push — master ( 29d040...599906 )
by Neomerx
04:36
created

JsonApiErrorCollection::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
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\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
    public function __construct(
50
        ContainerInterface $container,
51
        int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY
52 18
    ) {
53
        $this->setContainer($container);
54
        $this->errorStatus = $errorStatus;
55
    }
56 18
57 18
    /**
58
     * @inheritdoc
59
     */
60
    public function addValidationAttributeError(ErrorInterface $error)
61
    {
62
        $title  = $this->getInvalidValueMessage();
63 2
        $detail = $this->getValidationMessage($error);
64
        $this->addDataAttributeError($error->getParameterName(), $title, $detail, $this->getErrorStatus());
65 2
    }
66 2
67 2
    /**
68
     * @inheritdoc
69
     */
70
    public function addValidationRelationshipError(ErrorInterface $error)
71
    {
72
        $title  = $this->getInvalidValueMessage();
73 3
        $detail = $this->getValidationMessage($error);
74
        $this->addRelationshipError($error->getParameterName(), $title, $detail, $this->getErrorStatus());
75 3
    }
76 3
77 3
    /**
78
     * @return int
79
     */
80
    protected function getErrorStatus(): int
81
    {
82
        return $this->errorStatus;
83 3
    }
84
85 3
    /**
86
     * @return string
87
     */
88
    private function getInvalidValueMessage(): string
89
    {
90
        $message = $this->getMessageFormatter()->formatMessage(ErrorCodes::INVALID_VALUE);
91 3
92
        return $message;
93 3
    }
94
95 3
    /**
96
     * @param ErrorInterface $error
97
     *
98
     * @return string
99
     */
100 View Code Duplication
    private function getValidationMessage(ErrorInterface $error): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $context = $error->getMessageContext();
103 3
        $args    = $context === null ? [] : $context;
104
        $message = $this->getMessageFormatter()->formatMessage($error->getMessageCode(), $args);
105 3
106 3
        return $message;
107 3
    }
108
109 3
    /**
110
     * @return FormatterInterface
111
     */
112
    private function getMessageFormatter(): FormatterInterface
113
    {
114
        if ($this->messageFormatter === null) {
115 3
            $this->messageFormatter = $this->createValidationFormatter();
116
        }
117 3
118
        return $this->messageFormatter;
119 3
    }
120
}
121