Completed
Push — master ( 4f4fb1...2bb66e )
by Jonathan
02:15
created

src/ServerFactory.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 63
    public function __construct(array $config = [])
40
    {
41 63
        $this->config = $config;
42 63
    }
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(),
0 ignored issues
show
It seems like $this->getSource() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
52 6
            $this->getCache(),
0 ignored issues
show
It seems like $this->getCache() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

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