| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Superdesk Web Publisher Core Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright 2015 Sourcefabric z.u. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright 2021 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace SWP\Bundle\CoreBundle\Routing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SWP\Component\MultiTenancy\Context\TenantContextInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Symfony\Bundle\FrameworkBundle\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Symfony\Component\Routing\RequestContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class PwaAwareRouter extends Router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var TenantContextInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private $tenantContext; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 33 |  | View Code Duplication |     public function __construct( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         ContainerInterface $container, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $resource, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         array $options = [], | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         RequestContext $context = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         ContainerInterface $parameters = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         LoggerInterface $logger = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         string $defaultLocale = null | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this->tenantContext = $container->get('swp_multi_tenancy.tenant_context'); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         parent::__construct($container, $resource, $options, $context, $parameters, $logger, $defaultLocale); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function generate($name, $parameters = [], $referenceType = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $url = parent::generate($name, $parameters, $referenceType); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if($name === 'swp_user_verify_email') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $url = $this->applyPWAUrl($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private function applyPWAUrl (string $url): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if ($this->tenantContext->getTenant() && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $this->tenantContext->getTenant()->getPWAConfig() && | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $this->tenantContext->getTenant()->getPWAConfig()->getUrl() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $PWAUrlParts = parse_url($this->tenantContext->getTenant()->getPWAConfig()->getUrl()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $urlParts = parse_url($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $url = $PWAUrlParts['scheme'].'://'.$PWAUrlParts['host'].':'.$PWAUrlParts['port'].$urlParts['path'].'?'.$urlParts['query']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                        
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.