Completed
Pull Request — master (#300)
by
unknown
12:55
created

ServerFactory   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 292
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 20

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 38
lcom 1
cbo 20
dl 0
loc 292
ccs 0
cts 162
cp 0
rs 9.36
c 0
b 0
f 0

20 Methods

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