Completed
Pull Request — master (#282)
by
unknown
13:02
created

ServerFactory::getCachePathCallable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace League\Glide;
4
5
use Intervention\Image\ImageManager;
6
use InvalidArgumentException;
7
use League\Flysystem\Adapter\Local;
8
use League\Flysystem\Filesystem;
9
use League\Flysystem\FilesystemInterface;
10
use League\Glide\Api\Api;
11
use League\Glide\Manipulators\Background;
12
use League\Glide\Manipulators\Blur;
13
use League\Glide\Manipulators\Border;
14
use League\Glide\Manipulators\Brightness;
15
use League\Glide\Manipulators\Contrast;
16
use League\Glide\Manipulators\Crop;
17
use League\Glide\Manipulators\Encode;
18
use League\Glide\Manipulators\Filter;
19
use League\Glide\Manipulators\Flip;
20
use League\Glide\Manipulators\Gamma;
21
use League\Glide\Manipulators\Orientation;
22
use League\Glide\Manipulators\Pixelate;
23
use League\Glide\Manipulators\Sharpen;
24
use League\Glide\Manipulators\Size;
25
use League\Glide\Manipulators\Watermark;
26
use League\Glide\Responses\ResponseFactoryInterface;
27
28
class ServerFactory
29
{
30
    /**
31
     * Configuration parameters.
32
     * @var array
33
     */
34
    protected $config;
35
36
    /**
37
     * Create ServerFactory instance.
38
     * @param array $config Configuration parameters.
39
     */
40
    public function __construct(array $config = [])
41
    {
42
        $this->config = $config;
43
    }
44
45
    /**
46
     * Get configured server.
47
     * @return Server Configured Glide server.
48
     */
49
    public function getServer()
50
    {
51
        $server = new Server(
52
            $this->getSource(),
53
            $this->getCache(),
54
            $this->getApi()
55
        );
56
57
        $server->setSourcePathPrefix($this->getSourcePathPrefix());
58
        $server->setCachePathPrefix($this->getCachePathPrefix());
59
        $server->setGroupCacheInFolders($this->getGroupCacheInFolders());
60
        $server->setCacheWithFileExtensions($this->getCacheWithFileExtensions());
61
        $server->setDefaults($this->getDefaults());
62
        $server->setPresets($this->getPresets());
63
        $server->setBaseUrl($this->getBaseUrl());
64
        $server->setResponseFactory($this->getResponseFactory());
65
        $server->setCachePathCallable($this->getCachePathCallable());
66
67
        return $server;
68
    }
69
70
    /**
71
     * Get source file system.
72
     * @return FilesystemInterface Source file system.
73
     */
74
    public function getSource()
75
    {
76
        if (!isset($this->config['source'])) {
77
            throw new InvalidArgumentException('A "source" file system must be set.');
78
        }
79
80
        if (is_string($this->config['source'])) {
81
            return new Filesystem(
82
                new Local($this->config['source'])
83
            );
84
        }
85
86
        return $this->config['source'];
87
    }
88
89
    /**
90
     * Get source path prefix.
91
     * @return string|null Source path prefix.
92
     */
93
    public function getSourcePathPrefix()
94
    {
95
        if (isset($this->config['source_path_prefix'])) {
96
            return $this->config['source_path_prefix'];
97
        }
98
    }
99
100
    /**
101
     * Get cache file system.
102
     * @return FilesystemInterface Cache file system.
103
     */
104
    public function getCache()
105
    {
106
        if (!isset($this->config['cache'])) {
107
            throw new InvalidArgumentException('A "cache" file system must be set.');
108
        }
109
110
        if (is_string($this->config['cache'])) {
111
            return new Filesystem(
112
                new Local($this->config['cache'])
113
            );
114
        }
115
116
        return $this->config['cache'];
117
    }
118
119
    /**
120
     * Get cache path prefix.
121
     * @return string|null Cache path prefix.
122
     */
123
    public function getCachePathPrefix()
124
    {
125
        if (isset($this->config['cache_path_prefix'])) {
126
            return $this->config['cache_path_prefix'];
127
        }
128
    }
129
130
    /**
131
     * Get cache path callable.
132
     * @return callable|null Cache path callable.
133
     */
134
    public function getCachePathCallable()
135
    {
136
        if (isset($this->config['cache_path_callable'])) {
137
            return $this->config['cache_path_callable'];
138
        }
139
    }
140
141
    /**
142
     * Get the group cache in folders setting.
143
     * @return bool Whether to group cache in folders.
144
     */
145
    public function getGroupCacheInFolders()
146
    {
147
        if (isset($this->config['group_cache_in_folders'])) {
148
            return $this->config['group_cache_in_folders'];
149
        }
150
151
        return true;
152
    }
153
154
    /**
155
     * Get the cache with file extensions setting.
156
     * @return bool Whether to cache with file extensions.
157
     */
158
    public function getCacheWithFileExtensions()
159
    {
160
        if (isset($this->config['cache_with_file_extensions'])) {
161
            return $this->config['cache_with_file_extensions'];
162
        }
163
164
        return false;
165
    }
166
167
    /**
168
     * Get watermarks file system.
169
     * @return FilesystemInterface|null Watermarks file system.
170
     */
171
    public function getWatermarks()
172
    {
173
        if (!isset($this->config['watermarks'])) {
174
            return;
175
        }
176
177
        if (is_string($this->config['watermarks'])) {
178
            return new Filesystem(
179
                new Local($this->config['watermarks'])
180
            );
181
        }
182
183
        return $this->config['watermarks'];
184
    }
185
186
    /**
187
     * Get watermarks path prefix.
188
     * @return string|null Watermarks path prefix.
189
     */
190
    public function getWatermarksPathPrefix()
191
    {
192
        if (isset($this->config['watermarks_path_prefix'])) {
193
            return $this->config['watermarks_path_prefix'];
194
        }
195
    }
196
197
    /**
198
     * Get image manipulation API.
199
     * @return Api Image manipulation API.
200
     */
201
    public function getApi()
202
    {
203
        return new Api(
204
            $this->getImageManager(),
205
            $this->getManipulators()
206
        );
207
    }
208
209
    /**
210
     * Get Intervention image manager.
211
     * @return ImageManager Intervention image manager.
212
     */
213
    public function getImageManager()
214
    {
215
        $driver = 'gd';
216
217
        if (isset($this->config['driver'])) {
218
            $driver = $this->config['driver'];
219
        }
220
221
        return new ImageManager([
222
            'driver' => $driver,
223
        ]);
224
    }
225
226
    /**
227
     * Get image manipulators.
228
     * @return array Image manipulators.
229
     */
230
    public function getManipulators()
231
    {
232
        return [
233
            new Orientation(),
234
            new Crop(),
235
            new Size($this->getMaxImageSize()),
236
            new Brightness(),
237
            new Contrast(),
238
            new Gamma(),
239
            new Sharpen(),
240
            new Filter(),
241
            new Flip(),
242
            new Blur(),
243
            new Pixelate(),
244
            new Watermark($this->getWatermarks(), $this->getWatermarksPathPrefix()),
245
            new Background(),
246
            new Border(),
247
            new Encode(),
248
        ];
249
    }
250
251
    /**
252
     * Get maximum image size.
253
     * @return int|null Maximum image size.
254
     */
255
    public function getMaxImageSize()
256
    {
257
        if (isset($this->config['max_image_size'])) {
258
            return $this->config['max_image_size'];
259
        }
260
    }
261
262
    /**
263
     * Get default image manipulations.
264
     * @return array Default image manipulations.
265
     */
266
    public function getDefaults()
267
    {
268
        if (isset($this->config['defaults'])) {
269
            return $this->config['defaults'];
270
        }
271
272
        return [];
273
    }
274
275
    /**
276
     * Get preset image manipulations.
277
     * @return array Preset image manipulations.
278
     */
279
    public function getPresets()
280
    {
281
        if (isset($this->config['presets'])) {
282
            return $this->config['presets'];
283
        }
284
285
        return [];
286
    }
287
288
    /**
289
     * Get base URL.
290
     * @return string|null Base URL.
291
     */
292
    public function getBaseUrl()
293
    {
294
        if (isset($this->config['base_url'])) {
295
            return $this->config['base_url'];
296
        }
297
    }
298
299
    /**
300
     * Get response factory.
301
     * @return ResponseFactoryInterface|null Response factory.
302
     */
303
    public function getResponseFactory()
304
    {
305
        if (isset($this->config['response'])) {
306
            return $this->config['response'];
307
        }
308
    }
309
310
    /**
311
     * Create configured server.
312
     * @param  array  $config Configuration parameters.
313
     * @return Server Configured server.
314
     */
315
    public static function create(array $config = [])
316
    {
317
        return (new self($config))->getServer();
318
    }
319
}
320