ServerFactory   C
last analyzed

Coupling/Cohesion

Components 1
Dependencies 20

Complexity

Total Complexity 36

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Test Coverage

Coverage 70.37%

Importance

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

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