ServerFactory   C
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 1
Dependencies 19

Complexity

Total Complexity 36

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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