| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Kunstmaan\MediaBundle\Helper\Imagine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class CacheManager extends \Liip\ImagineBundle\Imagine\Cache\CacheManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public function generateUrl($path, $filter, array $runtimeConfig = array(), $resolver = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $originalPath = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $filterConf = $this->filterConfig->get($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $path = $this->changeFileExtension(ltrim($path, '/'), $filterConf['format']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $params = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'path' => ltrim($path, '/'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             'filter' => $filter, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         if ($resolver) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $params['resolver'] = $resolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if (empty($runtimeConfig)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $filterUrl = $this->router->generate('liip_imagine_filter', $params, UrlGeneratorInterface::ABSOLUTE_URL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $params['filters'] = $runtimeConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $params['hash'] = $this->signer->sign($originalPath, $runtimeConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             $filterUrl = $this->router->generate('liip_imagine_filter_runtime', $params, UrlGeneratorInterface::ABSOLUTE_URL); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $filterUrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function resolve($path, $filter, $resolver = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $filterConf = $this->filterConfig->get($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $path = $this->changeFileExtension($path, $filterConf['format']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return parent::resolve($path, $filter, $resolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     public function getBrowserPath($path, $filter, array $runtimeConfig = array(), $resolver = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $infoPath = parse_url($path, PHP_URL_PATH); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $info = pathinfo($infoPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $url = parent::getBrowserPath($path, $filter, $runtimeConfig, $resolver); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         $newPath = parse_url($url, PHP_URL_PATH); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $newInfo = pathinfo($newPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         if ($info['extension'] != $newInfo['extension']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             $query = parse_url($url, PHP_URL_QUERY); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             $url .= ($query ? '&' : '?') . 'originalExtension=' . $info['extension']; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         return $url; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param string $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 73 |  | View Code Duplication |     private function changeFileExtension($path, $format) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         if (!$format) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $info = pathinfo($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $path = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '.' . $format; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |    } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  | 
            
                        
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.