ServerFactory   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 20

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 20
dl 0
loc 280
ccs 0
cts 162
cp 0
rs 9.52
c 0
b 0
f 0

19 Methods

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