Passed
Push — feature/uploadable ( 7c6d25...a7ed20 )
by Daniel
11:07
created

FlysystemCacheResolver::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Imagine;
15
16
use League\Flysystem\Filesystem;
17
use League\Flysystem\Visibility;
18
use Liip\ImagineBundle\Binary\BinaryInterface;
19
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class FlysystemCacheResolver implements ResolverInterface
25
{
26
    private Filesystem $filesystem;
27
    private string $webRoot;
28
    private string $cachePrefix;
29
    private string $cacheRoot;
30
    private string $visibility;
31
32 5
    public function __construct(
33
        Filesystem $filesystem,
34
        string $rootUrl,
35
        $cachePrefix = 'media/cache',
36
        $visibility = Visibility::PUBLIC
37
    ) {
38 5
        $this->filesystem = $filesystem;
39 5
        $this->webRoot = rtrim($rootUrl, '/');
40 5
        $this->cachePrefix = ltrim(str_replace('//', '/', $cachePrefix), '/');
41 5
        $this->cacheRoot = $this->cachePrefix;
42 5
        $this->visibility = $visibility;
43 5
    }
44
45
    public function isStored($path, $filter): bool
46
    {
47
        return $this->filesystem->fileExists($this->getFilePath($path, $filter));
48
    }
49
50
    public function resolve($path, $filter): string
51
    {
52
        return sprintf(
53
            '%s/%s',
54
            rtrim($this->webRoot, '/'),
55
            ltrim($this->getFileUrl($path, $filter), '/')
56
        );
57
    }
58
59
    public function store(BinaryInterface $binary, $path, $filter): void
60
    {
61
        $this->filesystem->write(
62
            $this->getFilePath($path, $filter),
63
            $binary->getContent(),
64
            ['visibility' => $this->visibility, 'mimetype' => $binary->getMimeType()]
65
        );
66
    }
67
68
    public function remove(array $paths, array $filters): void
69
    {
70
        if (empty($paths) && empty($filters)) {
71
            return;
72
        }
73
74
        if (empty($paths)) {
75
            foreach ($filters as $filter) {
76
                $filterCacheDir = $this->cacheRoot . '/' . $filter;
77
                $this->filesystem->deleteDirectory($filterCacheDir);
78
            }
79
80
            return;
81
        }
82
83
        foreach ($paths as $path) {
84
            foreach ($filters as $filter) {
85
                if ($this->filesystem->fileExists($this->getFilePath($path, $filter))) {
86
                    $this->filesystem->delete($this->getFilePath($path, $filter));
87
                }
88
            }
89
        }
90
    }
91
92
    protected function getFilePath($path, $filter): string
93
    {
94
        return $this->getFileUrl($path, $filter);
95
    }
96
97
    protected function getFileUrl($path, $filter): string
98
    {
99
        // crude way of sanitizing URL scheme ("protocol") part
100
        $path = str_replace('://', '---', $path);
101
102
        return $this->cachePrefix . '/' . $filter . '/' . ltrim($path, '/');
103
    }
104
}
105