Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

MediaBundle/Helper/Imagine/CacheManager.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     *
72
     * @return string
73
     */
74 View Code Duplication
    private function changeFileExtension($path, $format)
0 ignored issues
show
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...
75
    {
76
        if (!$format) {
77
            return $path;
78
        }
79
80
        $info = pathinfo($path);
81
        $path = $info['dirname'] . DIRECTORY_SEPARATOR . $info['filename'] . '.' . $format;
82
83
        return $path;
84
    }
85
}
86