Completed
Push — master ( 66d197...72430f )
by Jonathan
03:30
created

ServerFactory::getServer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

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