| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Superdesk Web Publisher Content Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2015 Sourcefabric z.u. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2015 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace SWP\Bundle\ContentBundle\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use FOS\RestBundle\Controller\FOSRestController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use FOS\RestBundle\View\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SWP\Bundle\ContentBundle\Form\Type\RouteType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use SWP\Component\Common\Event\HttpCacheEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\HttpKernel\Exception\ConflictHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | class RouteController extends FOSRestController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Lists current tenant routes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @ApiDoc( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      *     resource=true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      *     description="Lists current tenant routes", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      *     statusCodes={ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      *         200="Returned on success." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      *     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @Route("/api/{version}/content/routes/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_list_routes") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @Method("GET") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @Cache(expires="10 minutes", public=true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |     public function listAction(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $baseroute = $this->get('swp.provider.route')->getBaseRoute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         $routes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         if (null !== $baseroute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |             $routes = $this->get('knp_paginator')->paginate($baseroute->getRouteChildren()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         return $this->handleView(View::create($this->get('swp_pagination_rep')->createRepresentation($routes, $request), 200)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Show single tenant route. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @ApiDoc( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      *     resource=true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      *     description="Show single tenant route", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      *     statusCodes={ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      *         200="Returned on success." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      *     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @Route("/api/{version}/content/routes/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_show_routes", requirements={"id"=".+"}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @Method("GET") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @Cache(expires="10 minutes", public=true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function getAction($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $this->handleView(View::create($this->findOr404($id), 200)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * Delete single tenant route. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @ApiDoc( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      *     resource=true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      *     description="Delete single tenant route", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      *     statusCodes={ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      *         204="Returned on success." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      *     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @Route("/api/{version}/content/routes/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_delete_routes", requirements={"id"=".+"}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @Method("DELETE") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |     public function deleteAction($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         $repository = $this->get('swp.repository.route'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         $route = $this->findOr404($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         $this->get('event_dispatcher') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             ->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($route)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         if ($route->getChildren()->count() > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |             throw new ConflictHttpException('Route have children routes or content attached to it.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |         $repository->remove($route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         return $this->handleView(View::create(true, 204)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Creates routes for current tenant. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Parameter `type` cane have one of two values: `content` or `collection`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Content path should be provided without tenant information: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Instead full content path like:  ```/swp/<tenant_code>/content/test-content-article``` provide path like this: ```test-content-article``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @ApiDoc( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      *     resource=true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      *     description="Creates routes for current tenant", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      *     statusCodes={ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      *         201="Returned on success." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      *     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      *     input="SWP\Bundle\ContentBundle\Form\Type\RouteType" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @Route("/api/{version}/content/routes/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_create_routes") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @Method("POST") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 127 | 15 | View Code Duplication |     public function createAction(Request $request) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 15 |  |         $route = $this->get('swp.factory.route')->create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 15 |  |         $form = $this->createForm(RouteType::class, $route, ['method' => $request->getMethod()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 15 |  |         $form->handleRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 15 |  |         if ($form->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 14 |  |             $route = $this->get('swp.service.route')->createRoute($form->getData()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 14 |  |             $this->get('swp.repository.route')->add($route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 14 |  |             $this->get('event_dispatcher') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 14 |  |                 ->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($route)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 14 |  |             return $this->handleView(View::create($route, 201)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |         return $this->handleView(View::create($form, 200)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Updates routes for current tenant. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * Parameter `type` cane have one of two values: `content` or `collection`. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * Content path should be provided without tenant information: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * Instead full content path like:  ```/swp/<tenant_code>/content/test-content-article``` provide path like this: ```test-content-article``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @ApiDoc( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      *     resource=true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      *     description="Updates routes for current tenant", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      *     statusCodes={ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      *         200="Returned on success.", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      *         404="Returned when route not found." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      *     }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |      *     input="SWP\Bundle\ContentBundle\Form\Type\RouteType" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * @Route("/api/{version}/content/routes/{id}", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_update_routes", requirements={"id"=".+"}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @Method("PATCH") | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 168 | 4 | View Code Duplication |     public function updateAction(Request $request, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 170 | 4 |  |         $objectManager = $this->get('swp.object_manager.route'); | 
            
                                                                        
                            
            
                                    
            
            
                | 171 | 4 |  |         $route = $this->findOr404($id); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 | 4 |  |         $form = $this->createForm(RouteType::class, $route, ['method' => $request->getMethod()]); | 
            
                                                                        
                            
            
                                    
            
            
                | 173 | 4 |  |         $form->handleRequest($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 175 | 4 |  |         if ($form->isValid()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 176 | 4 |  |             $route = $this->get('swp.service.route')->updateRoute($form->getData()); | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 178 | 4 |  |             $objectManager->flush(); | 
            
                                                                        
                            
            
                                    
            
            
                | 179 | 4 |  |             $this->get('event_dispatcher') | 
            
                                                                        
                            
            
                                    
            
            
                | 180 | 4 |  |             ->dispatch(HttpCacheEvent::EVENT_NAME, new HttpCacheEvent($route)); | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 182 | 4 |  |             return $this->handleView(View::create($route, 200)); | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |         return $this->handleView(View::create($form, 500)); | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 5 |  |     private function findOr404($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 | 5 |  |         if (null === $route = $this->get('swp.provider.route')->getOneById($id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             throw new NotFoundHttpException('Route was not found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 5 |  |         return $route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 196 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 197 |  |  |  | 
            
                        
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.