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