Completed
Pull Request — 5.0 (#1500)
by Sander
133:08 queued 122:35
created

CacheManager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 14.1 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 11
loc 78
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B generateUrl() 0 26 3
A resolve() 0 7 1
A getBrowserPath() 0 14 3
A changeFileExtension() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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