Completed
Push — master ( 0ca994...bcdd07 )
by Neomerx
05:23
created

addValidationRelationshipError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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\Contracts\L10n\FormatterFactoryInterface;
20
use Limoncello\Contracts\L10n\FormatterInterface;
21
use Limoncello\Flute\Contracts\Validation\ErrorCodes;
22
use Limoncello\Flute\Http\JsonApiResponse;
23
use Limoncello\Flute\Validation\Validator;
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
    /**
34
     * @var ContainerInterface
35
     */
36
    private $container;
37
38
    /**
39
     * @var int
40
     */
41
    private $errorStatus;
42
43
    /**
44
     * @var FormatterInterface|null
45
     */
46
    private $messageFormatter;
47
48
    /**
49
     * @param ContainerInterface $container
50
     * @param int                $errorStatus
51
     */
52
    public function __construct(
53
        ContainerInterface $container,
54
        int $errorStatus = JsonApiResponse::HTTP_UNPROCESSABLE_ENTITY
55
    ) {
56
        $this->container   = $container;
57
        $this->errorStatus = $errorStatus;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function addValidationAttributeError(ErrorInterface $error)
64
    {
65
        $title  = $this->getInvalidValueMessage();
66
        $detail = $this->getValidationMessage($error);
67
        $this->addDataAttributeError($error->getParameterName(), $title, $detail, $this->getErrorStatus());
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function addValidationRelationshipError(ErrorInterface $error)
74
    {
75
        $title  = $this->getInvalidValueMessage();
76
        $detail = $this->getValidationMessage($error);
77
        $this->addRelationshipError($error->getParameterName(), $title, $detail, $this->getErrorStatus());
78
    }
79
80
    /**
81
     * @return int
82
     */
83
    protected function getErrorStatus(): int
84
    {
85
        return $this->errorStatus;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    private function getInvalidValueMessage(): string
92
    {
93
        $message = $this->getMessageFormatter()->formatMessage(ErrorCodes::INVALID_VALUE);
94
95
        return $message;
96
    }
97
98
    /**
99
     * @param ErrorInterface $error
100
     *
101
     * @return string
102
     */
103 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...
104
    {
105
        $context = $error->getMessageContext();
106
        $args    = $context === null ? [] : $context;
107
        $message = $this->getMessageFormatter()->formatMessage($error->getMessageCode(), $args);
108
109
        return $message;
110
    }
111
112
    /**
113
     * @return FormatterInterface
114
     */
115 View Code Duplication
    private function getMessageFormatter(): FormatterInterface
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...
116
    {
117
        if ($this->messageFormatter === null) {
118
            /** @var FormatterFactoryInterface $factory */
119
            $factory                = $this->getContainer()->get(FormatterFactoryInterface::class);
120
            $this->messageFormatter = $factory->createFormatter(Validator::RESOURCES_NAMESPACE);
121
        }
122
123
        return $this->messageFormatter;
124
    }
125
126
    /**
127
     * @return ContainerInterface
128
     */
129
    private function getContainer(): ContainerInterface
130
    {
131
        return $this->container;
132
    }
133
}
134