Completed
Pull Request — master (#732)
by 12345
03:41
created

AbstractFilesystemResolver::setRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Liip\ImagineBundle\Imagine\Cache\Resolver;
4
5
use Liip\ImagineBundle\Binary\BinaryInterface;
6
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
7
use Liip\ImagineBundle\Imagine\Cache\CacheManagerAwareInterface;
8
use Symfony\Component\Filesystem\Exception\IOException;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\HttpFoundation\Request;
11
12
abstract class AbstractFilesystemResolver implements ResolverInterface, CacheManagerAwareInterface
13
{
14
    /**
15
     * @var Request
16
     */
17
    private $request;
18
19
    /**
20
     * @var Filesystem
21
     */
22
    protected $filesystem;
23
24
    /**
25
     * @var string
26
     */
27
    protected $basePath = '';
28
29
    /**
30
     * @var CacheManager
31
     */
32
    protected $cacheManager;
33
34
    /**
35
     * @var int
36
     */
37
    protected $folderPermissions = 0777;
38
39
    /**
40
     * Constructs a filesystem based cache resolver.
41
     *
42
     * @param Filesystem $filesystem
43
     */
44
    public function __construct(Filesystem $filesystem)
45
    {
46
        $this->filesystem = $filesystem;
47
    }
48
49
    /**
50
     * @param Request $request
51
     */
52
    public function setRequest(Request $request = null)
53
    {
54
        $this->request = $request;
55
    }
56
57
    /**
58
     * @param CacheManager $cacheManager
59
     */
60
    public function setCacheManager(CacheManager $cacheManager)
61
    {
62
        $this->cacheManager = $cacheManager;
63
    }
64
65
    /**
66
     * Set the base path to.
67
     *
68
     * @param $basePath
69
     */
70
    public function setBasePath($basePath)
71
    {
72
        $this->basePath = $basePath;
73
    }
74
75
    /**
76
     * @param int $folderPermissions
77
     */
78
    public function setFolderPermissions($folderPermissions)
79
    {
80
        $this->folderPermissions = $folderPermissions;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function isStored($path, $filter)
87
    {
88
        return file_exists($this->getFilePath($path, $filter));
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function store(BinaryInterface $binary, $path, $filter)
95
    {
96
        $filePath = $this->getFilePath($path, $filter);
97
98
        $dir = pathinfo($filePath, PATHINFO_DIRNAME);
99
100
        $this->makeFolder($dir);
101
102
        file_put_contents($filePath, $binary->getContent());
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function remove(array $paths, array $filters)
109
    {
110
        if (empty($paths) && empty($filters)) {
111
            return;
112
        }
113
114
        // TODO: this logic has to be refactored.
115
        list($rootCachePath) = explode(current($filters), $this->getFilePath('whateverpath', current($filters)));
116
117
        if (empty($paths)) {
118
            $filtersCachePaths = array();
119
            foreach ($filters as $filter) {
120
                $filterCachePath = $rootCachePath.$filter;
121
                if (is_dir($filterCachePath)) {
122
                    $filtersCachePaths[] = $filterCachePath;
123
                }
124
            }
125
126
            $this->filesystem->remove($filtersCachePaths);
127
128
            return;
129
        }
130
131
        foreach ($paths as $path) {
132
            foreach ($filters as $filter) {
133
                $this->filesystem->remove($this->getFilePath($path, $filter));
134
            }
135
        }
136
    }
137
138
    /**
139
     * @return Request
140
     *
141
     * @throws \LogicException
142
     */
143
    protected function getRequest()
144
    {
145
        if (false == $this->request) {
146
            throw new \LogicException('The request was not injected, inject it before using resolver.');
147
        }
148
149
        return $this->request;
150
    }
151
152
    /**
153
     * @param string $dir
154
     *
155
     * @throws \RuntimeException
156
     */
157
    protected function makeFolder($dir)
158
    {
159
        if (!is_dir($dir)) {
160
            $parent = dirname($dir);
161
            try {
162
                $this->makeFolder($parent);
163
                $this->filesystem->mkdir($dir);
164
                $this->filesystem->chmod($dir, $this->folderPermissions);
165
            } catch (IOException $e) {
166
                throw new \RuntimeException(sprintf('Could not create directory %s', $dir), 0, $e);
167
            }
168
        }
169
    }
170
171
    /**
172
     * Return the local filepath.
173
     *
174
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
175
     *
176
     * @param string $path   The resource path to convert.
177
     * @param string $filter The name of the imagine filter.
178
     *
179
     * @return string
180
     */
181
    abstract protected function getFilePath($path, $filter);
182
}
183