ServerFactory   C
last analyzed

Coupling/Cohesion

Components 1
Dependencies 19

Complexity

Total Complexity 36

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Test Coverage

Coverage 70.19%

Importance

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