Completed
Push — master ( c45cef...ec5c79 )
by Jonathan
03:46
created

ServerFactory::getGroupCacheInFolders()   A

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 8
Code Lines 4

Code Coverage

Tests 4
CRAP Score 2

Importance

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