Completed
Push — master ( 0136c1...94aca9 )
by Lukas Kahwe
03:29
created

FlysystemResolver::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace Liip\ImagineBundle\Imagine\Cache\Resolver;
4
5
use League\Flysystem\Filesystem;
6
use Liip\ImagineBundle\Binary\BinaryInterface;
7
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
8
use Symfony\Component\Routing\RequestContext;
9
10
class FlysystemResolver implements ResolverInterface
11
{
12
    /**
13
     * @var Filesystem
14
     */
15
    protected $flysystem;
16
17
    /**
18
     * @var RequestContext
19
     */
20
    protected $requestContext;
21
22
    /**
23
     * @var string
24
     */
25
    protected $webRoot;
26
27
    /**
28
     * @var string
29
     */
30
    protected $cachePrefix;
31
32
    /**
33
     * @var string
34
     */
35
    protected $cacheRoot;
36
37
    /**
38
     * FlysystemResolver constructor.
39
     *
40
     * @param Filesystem     $flysystem
41
     * @param RequestContext $requestContext
42
     * @param $rootUrl
43
     * @param string $cachePrefix
44
     */
45
    public function __construct(
46
        Filesystem $flysystem,
47
        RequestContext $requestContext,
48
        $rootUrl,
49
        $cachePrefix = 'media/cache'
50
    ) {
51
        $this->flysystem = $flysystem;
52
        $this->requestContext = $requestContext;
53
54
        $this->webRoot = rtrim($rootUrl, '/');
55
        $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/');
56
        $this->cacheRoot = $this->cachePrefix;
57
    }
58
59
    /**
60
     * Checks whether the given path is stored within this Resolver.
61
     *
62
     * @param string $path
63
     * @param string $filter
64
     *
65
     * @return bool
66
     */
67
    public function isStored($path, $filter)
68
    {
69
        return $this->flysystem->has($this->getFilePath($path, $filter));
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    protected function getFilePath($path, $filter)
76
    {
77
        return $this->getFileUrl($path, $filter);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 View Code Duplication
    protected function getFileUrl($path, $filter)
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...
84
    {
85
        // crude way of sanitizing URL scheme ("protocol") part
86
        $path = str_replace('://', '---', $path);
87
88
        return $this->cachePrefix.'/'.$filter.'/'.ltrim($path, '/');
89
    }
90
91
    /**
92
     * Resolves filtered path for rendering in the browser.
93
     *
94
     * @param string $path   The path where the original file is expected to be.
95
     * @param string $filter The name of the imagine filter in effect.
96
     *
97
     * @return string The absolute URL of the cached image.
98
     *
99
     * @throws NotResolvableException
100
     */
101
    public function resolve($path, $filter)
102
    {
103
        return sprintf(
104
            '%s/%s',
105
            $this->webRoot,
106
            $this->getFileUrl($path, $filter)
107
        );
108
    }
109
110
    /**
111
     * Stores the content of the given binary.
112
     *
113
     * @param BinaryInterface $binary The image binary to store.
114
     * @param string          $path   The path where the original file is expected to be.
115
     * @param string          $filter The name of the imagine filter in effect.
116
     */
117
    public function store(BinaryInterface $binary, $path, $filter)
118
    {
119
        $this->flysystem->put(
120
            $this->getFilePath($path, $filter),
121
            $binary->getContent()
122
        );
123
    }
124
125
    /**
126
     * @param string[] $paths   The paths where the original files are expected to be.
127
     * @param string[] $filters The imagine filters in effect.
128
     */
129
    public function remove(array $paths, array $filters)
130
    {
131
        if (empty($paths) && empty($filters)) {
132
            return;
133
        }
134
135
        if (empty($paths)) {
136
            foreach ($filters as $filter) {
137
                $filterCacheDir = $this->cacheRoot.'/'.$filter;
138
                $this->flysystem->deleteDir($filterCacheDir);
139
            }
140
141
            return;
142
        }
143
144
        foreach ($paths as $path) {
145
            foreach ($filters as $filter) {
146
                if ($this->flysystem->has($this->getFilePath($path, $filter))) {
147
                    $this->flysystem->delete($this->getFilePath($path, $filter));
148
                }
149
            }
150
        }
151
    }
152
}
153