| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Superdesk Web Publisher Content Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2016 Sourcefabric z.ú. 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 2016 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace SWP\Bundle\ContentBundle\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\RouteObjectInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use SWP\Bundle\ContentBundle\Event\RouteEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use SWP\Bundle\ContentBundle\Model\RouteInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SWP\Bundle\ContentBundle\Provider\ArticleProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SWP\Bundle\ContentBundle\RouteEvents; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class RouteService implements RouteServiceInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var ArticleProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $articleProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var EventDispatcherInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * RouteService constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param ArticleProviderInterface $articleProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param EventDispatcherInterface $eventDispatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 22 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ArticleProviderInterface $articleProvider, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         EventDispatcherInterface $eventDispatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 22 |  |         $this->articleProvider = $articleProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 22 |  |         $this->eventDispatcher = $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 | 21 | View Code Duplication |     public function createRoute(RouteObjectInterface $route) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 21 |  |         $this->dispatchRouteEvent(RouteEvents::PRE_CREATE, $route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 21 |  |         $route = $this->fillRoute($route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 21 |  |         $this->dispatchRouteEvent(RouteEvents::POST_CREATE, $route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 21 |  |         return $route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 67 | 4 | View Code Duplication |     public function updateRoute(RouteObjectInterface $route) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 4 |  |         $this->dispatchRouteEvent(RouteEvents::PRE_UPDATE, $route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 4 |  |         $route = $this->fillRoute($route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |         $this->dispatchRouteEvent(RouteEvents::POST_UPDATE, $route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  |         return $route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 22 |  |     private function dispatchRouteEvent($eventName, RouteInterface $route) | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 22 |  |         $this->eventDispatcher->dispatch($eventName, new RouteEvent($route)); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 22 |  |     private function fillRoute(RouteObjectInterface $route) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 22 |  |         switch ($route->getType()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 22 |  |             case RouteInterface::TYPE_CONTENT: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 9 |  |                 $route->setVariablePattern(null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 9 |  |                 $route->setRequirements([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 9 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 15 |  |             case RouteInterface::TYPE_COLLECTION: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 15 |  |                 $route->setVariablePattern('/{slug}'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 15 |  |                 $route->setRequirement('slug', '[a-zA-Z1-9\-_\/]+'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 15 |  |                 $route->setDefault('slug', null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 15 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 throw new \InvalidArgumentException(sprintf('Route type "%s" is unsupported!', $route->getType())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 22 |  |         return $route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |  | 
            
                        
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.