| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Author: Nil Portugués Calderó <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Date: 12/2/15 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Time: 9:38 PM. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace NilPortugues\Api\JsonApi\Server\Actions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use NilPortugues\Api\JsonApi\JsonApiSerializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use NilPortugues\Api\JsonApi\Server\Actions\Traits\ResponseTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use NilPortugues\Api\JsonApi\Server\Data\DataException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use NilPortugues\Api\JsonApi\Server\Data\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use NilPortugues\Api\JsonApi\Server\Errors\Error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use NilPortugues\Api\JsonApi\Server\Errors\ErrorBag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use NilPortugues\Api\JsonApi\Server\Errors\NotFoundError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Class PatchResource. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 24 |  | View Code Duplication | class PatchResource | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     use ResponseTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var \NilPortugues\Api\JsonApi\Server\Errors\ErrorBag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $errorBag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var JsonApiSerializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param JsonApiSerializer $serializer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function __construct(JsonApiSerializer $serializer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->serializer = $serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->errorBag = new ErrorBag(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string   $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param array    $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param string   $className | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param callable $findOneCallable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param callable $update | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function get($id, array $data, $className, callable $findOneCallable, callable $update) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             DataObject::assertPatch($data, $this->serializer, $className, $this->errorBag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $model = $findOneCallable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             if (empty($model)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $values = DataObject::getAttributes($data, $this->serializer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $update($model, $values, $this->errorBag); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $response = $this->resourceUpdated($this->serializer->serialize($model)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } catch (Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $response = $this->getErrorResponse($e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param $e | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return \Symfony\Component\HttpFoundation\Response | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |     protected function getErrorResponse(Exception $e) | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |         switch (get_class($e)) { | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |             case DataException::class: | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |                 $response = $this->unprocessableEntity($this->errorBag); | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |             default: | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |                 $response = $this->errorResponse( | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |                     new ErrorBag([new Error('Bad Request', 'Request could not be served.')]) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |                 ); | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         return $response; | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                        
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.