| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace QualityCode\ApiFeaturesBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use FOS\RestBundle\View\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Hateoas\Configuration\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Hateoas\Representation\Factory\PagerfantaFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Hateoas\Representation\PaginatedRepresentation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Form\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Description of controllerBasicFeatures. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @author flavien-metivier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | trait ControllerBasicFeaturesTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Gets a container service by its id. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param string $id The service id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @return object The service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     abstract protected function get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Creates and returns a Form instance from the type of the form. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param string $type    The fully qualified class name of the form type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param mixed  $data    The initial data for the form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @param array  $options Options for the form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     abstract protected function createForm($type, $data = null, array $options = []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string  $repositoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return PaginatedRepresentation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |     protected function getCollectionPaginated(Request $request, $repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $limit = $request->query->getInt('limit', 10); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         $page = $request->query->getInt('page', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         $sorting = $this->get('fos_rest.normalizer.camel_keys')->normalize($request->query->get('sorting', [])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         $searching = $this->get('fos_rest.normalizer.camel_keys')->normalize($request->query->get('searching', [])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         $collectionPager = $this->get('doctrine.orm.entity_manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |                 ->getRepository($repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |                 ->findAllPaginated($limit, $page, $sorting, $searching); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |         $pagerFactory = new PagerfantaFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         return $pagerFactory->createRepresentation( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $collectionPager, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             new Route($request->get('_route'), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |                 'limit' => $limit, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |                 'page' => $page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |                 'sorting' => $sorting, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |                 'searching' => $searching, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param string  $repositoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return mixed|\FOS\RestBundle\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |     protected function getAnElement(Request $request, $repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         $anElement = $this->get('doctrine.orm.entity_manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |                 ->getRepository($repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |                 ->find($request->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |         if (empty($anElement)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |             return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         return $anElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param mixed   $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string  $formClassName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |     protected function createAnElement(Request $request, $element, $formClassName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         $form = $this->createForm($formClassName, $element, ['csrf_protection' => false]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 2 |  |         $form->submit($request->request->all(), false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |         return $this->checkFormAndPersist($form, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param string  $repositoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |     protected function removeAnElement(Request $request, $repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |         $em = $this->get('doctrine.orm.entity_manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |         $element = $em->getRepository($repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |                 ->find($request->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |         if ($element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 1 |  |             $em->remove($element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 1 |  |             $em->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param Request $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param bool    $clearMissing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param $repositoryName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param $formClassName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @return mixed|Form|\FOS\RestBundle\View\View | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 6 |  |     protected function updateAnElement(Request $request, $clearMissing, $repositoryName, $formClassName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 6 |  |         $element = $this->get('doctrine.orm.entity_manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 6 |  |                 ->getRepository($repositoryName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 6 |  |                 ->find($request->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 6 |  |         if (empty($element)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 2 |  |             return View::create(['message' => 'Element not found'], Response::HTTP_NOT_FOUND); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 4 |  |         $form = $this->createForm($formClassName, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 4 |  |         $form->submit($request->request->all(), $clearMissing); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 4 |  |         return $this->checkFormAndPersist($form, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * @param Form  $form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param mixed $element | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return mixed|Form | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 152 | 6 |  |     private function checkFormAndPersist(Form $form, $element) | 
            
                                                        
            
                                    
            
            
                | 153 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 154 | 6 |  |         if ($form->isSubmitted() && $form->isValid()) { | 
            
                                                        
            
                                    
            
            
                | 155 | 3 |  |             $em = $this->get('doctrine.orm.entity_manager'); | 
            
                                                        
            
                                    
            
            
                | 156 | 3 |  |             $em->persist($element); | 
            
                                                        
            
                                    
            
            
                | 157 | 3 |  |             $em->flush(); | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 159 | 3 |  |             return $element; | 
            
                                                        
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 162 | 3 |  |         return $form; | 
            
                                                        
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |  |