Passed
Push — master ( a9f547...49cca7 )
by Raffael
04:18
created

Storage::getFileMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
crap 2
eloc 2
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\Filesystem;
13
14
use Balloon\Filesystem\Node\File;
15
use Balloon\Filesystem\Node\NodeInterface;
16
use Balloon\Filesystem\Storage\Adapter\AdapterInterface;
17
use Psr\Log\LoggerInterface;
18
19
class Storage
20
{
21
    /**
22
     * Logger.
23
     *
24
     * @var LoggerInterface
25
     */
26
    protected $logger;
27
28
    /**
29
     * Storage adapter.
30
     *
31
     * @var array
32
     */
33
    protected $adapter = [];
34
35
    /**
36
     * Storage handler.
37
     */
38
    public function __construct(LoggerInterface $logger)
39
    {
40
        $this->logger = $logger;
41
    }
42
43
    /**
44
     * Has adapter.
45
     */
46
    public function hasAdapter(string $name): bool
47
    {
48
        return isset($this->adapter[$name]);
49
    }
50
51
    /**
52
     * Inject adapter.
53
     *
54
     * @param string $name
55
     *
56
     * @return Storage
57
     */
58
    public function injectAdapter(AdapterInterface $adapter, ?string $name = null): self
59
    {
60
        if (null === $name) {
61
            $name = get_class($adapter);
62
        }
63
64
        if ($this->hasAdapter($name)) {
65
            throw new Exception('storage adapter '.$name.' is already registered');
66
        }
67
68
        $this->logger->debug('inject storage adapter ['.$name.'] of type ['.get_class($adapter).']', [
69
            'category' => get_class($this),
70
        ]);
71
72
        $this->adapter[$name] = $adapter;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Get adapter.
79
     */
80
    public function getAdapter(string $name): AdapterInterface
81
    {
82
        if (!$this->hasAdapter($name)) {
83
            throw new Exception('storage adapter '.$name.' is not registered');
84
        }
85
86
        return $this->adapter[$name];
87
    }
88
89
    /**
90
     * Get adapters.
91
     *
92
     *
93
     * @return AdapterInterface[]
94
     */
95
    public function getAdapters(array $adapters = []): array
0 ignored issues
show
Unused Code introduced by
The parameter $adapters is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
    {
97
        if (empty($adapter)) {
0 ignored issues
show
Bug introduced by
The variable $adapter does not exist. Did you mean $adapters?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
98
            return $this->adapter;
99
        }
100
        $list = [];
101
        foreach ($adapter as $name) {
102
            if (!$this->hasAdapter($name)) {
103
                throw new Exception('storage adapter '.$name.' is not registered');
104
            }
105
            $list[$name] = $this->adapter[$name];
106
        }
107
108
        return $list;
109
    }
110
111
    /**
112
     * Check if node exists.
113
     *
114
     * @param array  $attributes
115
     * @param string $adapter
116
     */
117
    public function hasNode(NodeInterface $node, ?array $attributes = null, ?string $adapter = null): bool
118
    {
119
        return $this->execAdapter('hasNode', $node, $attributes, $adapter);
120
    }
121
122
    /**
123
     * Delete file.
124
     *
125
     * @param array  $attributes
126
     * @param string $adapter
127
     */
128
    public function deleteFile(File $file, ?array $attributes = null, ?string $adapter = null): bool
129
    {
130
        return $this->execAdapter('deleteFile', $file, $attributes, $adapter);
131
    }
132
133
    /**
134
     * Get stored file.
135
     *
136
     * @param array  $attributes
137
     * @param string $adapter
138
     *
139
     * @return resource
140
     */
141
    public function getFile(File $file, ?array $attributes = null, ?string $adapter = null)
142
    {
143
        return $this->execAdapter('getFile', $file, $attributes, $adapter);
144
    }
145
146
    /**
147
     * Store file.
148
     *
149
     * @param resource $contents
150
     * @param string   $adapter
151
     */
152
    public function storeFile(File $file, $contents, ?string &$adapter = null)
153
    {
154
        $attrs = $file->getAttributes();
155
156
        if ($attrs['storage_adapter']) {
157
            $adapter = $attrs['storage_adapter'];
158
        } elseif (null === $adapter) {
159
            $adapter = 'gridfs';
160
        }
161
162
        return $this->getAdapter($adapter)->storeFile($file, $contents);
163
    }
164
165
    /**
166
     * Execute command on adapter.
167
     *
168
     * @param array  $attributes
169
     * @param string $adapter
170
     */
171
    protected function execAdapter(string $method, File $file, ?array $attributes = null, ?string $adapter = null)
172
    {
173
        $attrs = $file->getAttributes();
174
175
        if ($attrs['storage_adapter']) {
176
            $adapter = $attrs['storage_adapter'];
177
        } elseif (null === $adapter) {
178
            $adapter = 'gridfs';
179
        }
180
181
        if ($attributes === null) {
182
            $attributes = $attrs['storage'];
183
        }
184
185
        return $this->getAdapter($adapter)->{$method}($file, $attributes);
186
    }
187
}
188