| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace AppBundle\Controller\Infrastructure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use AppBundle\Entity\Infrasctucture\AbstractRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use AppBundle\Response\CollectionApiResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use AppBundle\Response\FileResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use AppBundle\Response\Infrastructure\AbstractApiResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use AppBundle\Response\Infrastructure\HttpLocationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Form\FormBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Form\FormInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Vehsamrak | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class RestController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     const MIME_TYPE_JSON = 'application/json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 36 |  |     public function respond(AbstractApiResponse $apiResponse): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 36 |  |         $response = $this->get('rockparade.http_response_factory')->createResponse($apiResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 36 |  |         $this->setLocation($response, $apiResponse); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 36 |  |         $this->setContentType($apiResponse, $response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 36 |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * Process form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param FormInterface $form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 13 |  |     protected function processForm(Request $request, FormInterface $form) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 13 |  |         $formData = json_decode($request->getContent(), true) ?? $request->request->all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 13 |  |         $clearMissing = $request->getMethod() != Request::METHOD_PATCH; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 13 |  |         $form->submit($formData, $clearMissing); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 13 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |     /** {@inheritDoc} */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 10 |  |     protected function createFormBuilder($data = null, array $options = []): FormBuilder | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 10 |  |         $options['allow_extra_fields'] = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 10 |  |         return parent::createFormBuilder($data, $options); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Create collection api response with total, limit and offset parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param integer|null $limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @param integer|null $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |     protected function createCompleteCollectionResponse(AbstractRepository $repository, $limit, $offset): CollectionApiResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |         $limit = (int) filter_var($limit, FILTER_VALIDATE_INT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |         $offset = (int) filter_var($offset, FILTER_VALIDATE_INT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |         $entities = $repository->findAllWithLimitAndOffset($limit, $offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |         $entitiesQuantity = $repository->countAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |         $response = new CollectionApiResponse($entities, Response::HTTP_OK, $entitiesQuantity, $limit, $offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 36 |  |     private function setLocation(Response $response, AbstractApiResponse $apiResponse) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 36 |  |         if ($apiResponse instanceof HttpLocationInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  |             $response->headers->set('Location', $apiResponse->getLocation()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 36 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 36 |  |     private function setContentType(AbstractApiResponse $apiResponse, Response $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 36 |  |         if (!$apiResponse instanceof FileResponse) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 36 |  |             $response->headers->set('Content-Type', self::MIME_TYPE_JSON); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 36 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  |