| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Class Link | Link.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @package Faulancer\View\Helper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author  Florian Knapp <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Faulancer\View\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Faulancer\Exception\RouteInvalidException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Faulancer\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Faulancer\Service\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Faulancer\Service\RequestService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Faulancer\Translate\Translator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Faulancer\View\AbstractViewHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Faulancer\View\ViewController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Class Link | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | class Link extends AbstractViewHelper | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      * @param ViewController $view | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * @param string         $routeName | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @param array          $elementAttributes | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @throws RouteInvalidException | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @codeCoverageIgnore | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __invoke(ViewController $view, $routeName, $elementAttributes = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $id    = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $class = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $style = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $serviceLocator = $this->getServiceLocator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         /** @var Config $config */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $config = $serviceLocator->get(Config::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         /** @var Request $request */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $request = $serviceLocator->get(RequestService::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $route = $config->get('routes:' . $routeName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (empty($route)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             throw new RouteInvalidException('No valid route for "' . $routeName . '" found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if ($request->getUri() === $route['path']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $elementAttributes['class'][] = 'selected'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 55 |  | View Code Duplication |         if (!empty($elementAttributes['id'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             $id = ' id="' . implode(' ', $elementAttributes['id']) . '" '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 59 |  | View Code Duplication |         if (!empty($elementAttributes['class'])){ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $class = 'class="' . implode(' ', $elementAttributes['class']) . '" '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |         if (!empty($elementAttributes['style'])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $style = 'style="' . implode(' ', $elementAttributes['style']) . '" '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $linkPattern = '<a ' . $id . $class . $style . 'href="%s" onfocus="blur()">%s</a>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (!empty($route['i18n_key']) && !empty($route['title'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $translator = new Translator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $route['title'] = $translator->translate($route['i18n_key']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $link = sprintf($linkPattern, $route['path'], $route['title']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return $link; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 80 |  |  | } | 
            
                        
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.