Failed Conditions
Pull Request — master (#6)
by Florian
06:38 queued 03:42
created

FileStorage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Test Coverage

Coverage 59.51%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
c 1
b 0
f 0
dl 0
loc 142
ccs 25
cts 42
cp 0.5951
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 15 3
A removeVariant() 0 20 3
A __construct() 0 8 1
A getStorage() 0 3 1
A runCallbacks() 0 14 3
A store() 0 18 3
1
<?php
2
3
/**
4
 * Copyright (c) Florian Krämer (https://florian-kraemer.net)
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright Copyright (c) Florian Krämer (https://florian-kraemer.net)
10
 * @author    Florian Krämer
11
 * @link      https://github.com/Phauthentic
12
 * @license   https://opensource.org/licenses/MIT MIT License
13
 */
14
15
declare(strict_types=1);
16
17
namespace Phauthentic\Infrastructure\Storage;
18
19
use InvalidArgumentException;
20
use League\Flysystem\AdapterInterface;
21
use League\Flysystem\Config;
22
use Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilder;
23
use Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface;
24
use Phauthentic\Infrastructure\Storage\Processor\Exception\VariantDoesNotExistException;
25
use Phauthentic\Infrastructure\Storage\Processor\Exception\VariantException;
26
use Phauthentic\Infrastructure\Storage\UrlBuilder\NoopUrlBuilder;
0 ignored issues
show
Bug introduced by
The type Phauthentic\Infrastructu...lBuilder\NoopUrlBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use Phauthentic\Infrastructure\Storage\UrlBuilder\UrlBuilderInterface;
28
29
/**
30
 * File Storage
31
 */
32
class FileStorage implements FileStorageInterface
33
{
34
    /**
35
     * @var array
36
     */
37
    protected array $callbacks = [
38
        'beforeSave' => [],
39
        'afterSave' => [],
40
        'beforeRemove' => [],
41
        'afterRemove' => [],
42
    ];
43
44
    /**
45
     * @var \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface|null
46
     */
47
    protected ?PathBuilderInterface $pathBuilder;
48
49
    /**
50
     * @var \Phauthentic\Infrastructure\Storage\UrlBuilder\UrlBuilderInterface|null
51
     */
52
    protected ?UrlBuilderInterface $urlBuilder;
53
54
    /**
55
     * @var \Phauthentic\Infrastructure\Storage\StorageServiceInterface
56
     */
57
    protected StorageServiceInterface $storageService;
58
59
    /**
60
     * Constructor
61
     *
62
     * @param \Phauthentic\Infrastructure\Storage\StorageServiceInterface $storageService Storage Service
63
     * @param \Phauthentic\Infrastructure\Storage\PathBuilder\PathBuilderInterface|null $pathBuilder Path Builder
64
     * @param \Phauthentic\Infrastructure\Storage\UrlBuilder\UrlBuilderInterface|null $urlBuilder Path Builder
65
     */
66 1
    public function __construct(
67
        StorageServiceInterface $storageService,
68
        ?PathBuilderInterface $pathBuilder = null,
69
        ?UrlBuilderInterface $urlBuilder = null
70
    ) {
71 1
        $this->storageService = $storageService;
72 1
        $this->pathBuilder = $pathBuilder ?? new PathBuilder();
73 1
        $this->urlBuilder = $urlBuilder;
74 1
    }
75
76
    /**
77
     * @param string $type Type
78
     * @param \Phauthentic\Infrastructure\Storage\FileInterface $file File
79
     * @return \Phauthentic\Infrastructure\Storage\FileInterface
80
     */
81 1
    public function runCallbacks(string $type, FileInterface $file): FileInterface
82
    {
83 1
        if (!isset($this->callbacks[$type])) {
84
            throw new InvalidArgumentException(sprintf(
85
                'Type %s is invalid',
86
                $type
87
            ));
88
        }
89
90 1
        foreach ($this->callbacks[$type] as $callback) {
91
            $file = $callback($file);
92
        }
93
94 1
        return $file;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 1
    public function store(FileInterface $file): FileInterface
101
    {
102 1
        $config = new Config();
103
104 1
        if ($this->pathBuilder !== null) {
105 1
            $file = $file->buildPath($this->pathBuilder);
106
        }
107
108 1
        if ($this->urlBuilder !== null) {
109
            $file = $file->buildUrl($this->urlBuilder);
110
        }
111
112 1
        $file = $this->runCallbacks('beforeSave', $file);
113
114 1
        $storage = $this->getStorage($file->storage());
115 1
        $storage->writeStream($file->path(), $file->resource(), $config);
116
117 1
        return $this->runCallbacks('afterSave', $file);
118
    }
119
120
    /**
121
     * @inheritDoc
122
     */
123 1
    public function remove(FileInterface $file): FileInterface
124
    {
125 1
        $file = $this->runCallbacks('beforeRemove', $file);
126
127
        // Delete all variants of the file
128 1
        foreach ($file->variants() as $variant) {
129
            if (!empty($variant['path'])) {
130
                $this->getStorage($file->storage())->delete($variant['path']);
131
            }
132
        }
133
134
        // Delete the file
135 1
        $this->getStorage($file->storage())->delete($file->path());
136
137 1
        return $this->runCallbacks('afterRemove', $file);
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function removeVariant(FileInterface $file, string $name): FileInterface
144
    {
145
        if (!$file->hasVariant($name)) {
146
            throw VariantDoesNotExistException::withName($name);
147
        }
148
149
        $variant = $file->variant($name);
150
        if (empty($variant['path'])) {
151
            throw new VariantException(sprintf(
152
                'Variant `%s` is missing a path',
153
                $name
154
            ));
155
        }
156
157
        $this->getStorage($file->storage())->delete($variant['path']);
158
159
        $variants = $file->variants();
160
        unset($variants[$name]);
161
162
        return $file->withVariants($variants, false);
163
    }
164
165
    /**
166
     * Gets the storage abstraction to use
167
     *
168
     * @param string $storage Storage name to use
169
     * @return \League\Flysystem\AdapterInterface
170
     */
171 1
    public function getStorage(string $storage): AdapterInterface
172
    {
173 1
        return $this->storageService->adapter($storage);
174
    }
175
}
176