Passed
Push — master ( 94362d...823402 )
by Quentin
11:49 queued 05:32
created

Glide   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 245
Duplicated Lines 0 %

Test Coverage

Coverage 38.16%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
eloc 75
dl 0
loc 245
ccs 29
cts 76
cp 0.3816
rs 10
c 7
b 0
f 1
wmc 23

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getCrop() 0 12 2
A getCmsUrl() 0 9 2
A getPresetUrl() 0 3 1
A getFocalPointCrop() 0 24 3
A getUrlWithFocalCrop() 0 3 1
A getUrl() 0 4 2
A getSocialUrl() 0 9 2
A getDimensions() 0 15 2
A render() 0 7 2
A getUrlWithCrop() 0 3 1
A getRawUrl() 0 3 1
A getLQIPUrl() 0 9 2
A __construct() 0 24 2
1
<?php
2
3
namespace A17\Twill\Services\MediaLibrary;
4
5
use Illuminate\Config\Repository as Config;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Str;
10
use League\Glide\Responses\LaravelResponseFactory;
11
use League\Glide\ServerFactory;
12
use League\Glide\Signatures\SignatureFactory;
13
use League\Glide\Urls\UrlBuilderFactory;
14
15
class Glide implements ImageServiceInterface
16
{
17
    use ImageServiceDefaults;
18
19
    /**
20
     * @var Config
21
     */
22
    protected $config;
23
24
    /**
25
     * @var Application
26
     */
27
    protected $app;
28
29
    /**
30
     * @var Request
31
     */
32
    protected $request;
33
34
    /**
35
     * @var League\Glide\Server
0 ignored issues
show
Bug introduced by
The type A17\Twill\Services\Media...ary\League\Glide\Server was not found. Did you mean League\Glide\Server? If so, make sure to prefix the type with \.
Loading history...
36
     */
37
    private $server;
38
39
    /**
40
     * @var UrlBuilder
0 ignored issues
show
Bug introduced by
The type A17\Twill\Services\MediaLibrary\UrlBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     */
42
    private $urlBuilder;
43
44
    /**
45
     * @param Config $config
46
     * @param Application $app
47
     * @param Request $request
48
     */
49 10
    public function __construct(Config $config, Application $app, Request $request)
50
    {
51 10
        $this->config = $config;
52 10
        $this->app = $app;
53 10
        $this->request = $request;
54
55 10
        $baseUrl = join('/', [
56 10
            rtrim($this->config->get('twill.glide.base_url'), '/'),
57 10
            ltrim($this->config->get('twill.glide.base_path'), '/'),
58
        ]);
59
60 10
        $this->server = ServerFactory::create([
0 ignored issues
show
Documentation Bug introduced by
It seems like League\Glide\ServerFacto...'twill.glide.driver'))) of type League\Glide\Server is incompatible with the declared type A17\Twill\Services\Media...ary\League\Glide\Server of property $server.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61 10
            'response' => new LaravelResponseFactory($this->request),
62 10
            'source' => $this->config->get('twill.glide.source'),
63 10
            'cache' => $this->config->get('twill.glide.cache'),
64 10
            'cache_path_prefix' => $this->config->get('twill.glide.cache_path_prefix'),
65 10
            'base_url' => $baseUrl,
66 10
            'presets' => $this->config->get('twill.glide.presets', []),
67 10
            'driver' => $this->config->get('twill.glide.driver')
68
        ]);
69
70 10
        $this->urlBuilder = UrlBuilderFactory::create(
0 ignored issues
show
Documentation Bug introduced by
It seems like League\Glide\Urls\UrlBui...lide.sign_key') : null) of type League\Glide\Urls\UrlBuilder is incompatible with the declared type A17\Twill\Services\MediaLibrary\UrlBuilder of property $urlBuilder.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71 10
            $baseUrl,
72 10
            $this->config->get('twill.glide.use_signed_urls') ? $this->config->get('twill.glide.sign_key') : null
73
        );
74 10
    }
75
76
    /**
77
     * @param string $path
78
     * @return StreamedResponse
0 ignored issues
show
Bug introduced by
The type A17\Twill\Services\MediaLibrary\StreamedResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
79
     */
80
    public function render($path)
81
    {
82
        if ($this->config->get('twill.glide.use_signed_urls')) {
83
            SignatureFactory::create($this->config->get('twill.glide.sign_key'))->validateRequest($this->config->get('twill.glide.base_path') . '/' . $path, $this->request->all());
84
        }
85
86
        return $this->server->getImageResponse($path, $this->request->all());
87
    }
88
89
    /**
90
     * @param string $id
91
     * @param array $params
92
     * @return string
93
     */
94 6
    public function getUrl($id, array $params = [])
95
    {
96 6
        $defaultParams = config('twill.glide.default_params');
97 6
        return $this->urlBuilder->getUrl($id, Str::endsWith($id, '.svg') ? [] : array_replace($defaultParams, $params));
98
    }
99
100
    /**
101
     * @param string $id
102
     * @param array $cropParams
103
     * @param array $params
104
     * @return string
105
     */
106
    public function getUrlWithCrop($id, array $cropParams, array $params = [])
107
    {
108
        return $this->getUrl($id, $this->getCrop($cropParams) + $params);
109
    }
110
111
    /**
112
     * @param string $id
113
     * @param array $cropParams
114
     * @param mixed $width
115
     * @param mixed $height
116
     * @param array $params
117
     * @return string
118
     */
119
    public function getUrlWithFocalCrop($id, array $cropParams, $width, $height, array $params = [])
120
    {
121
        return $this->getUrl($id, $this->getFocalPointCrop($cropParams, $width, $height) + $params);
122
    }
123
124
    /**
125
     * @param string $id
126
     * @param array $params
127
     * @return string
128
     */
129
    public function getLQIPUrl($id, array $params = [])
130
    {
131
        $defaultParams = config('twill.glide.lqip_default_params');
132
133
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
134
135
        $params = Arr::except($params, $this->cropParamsKeys);
136
137
        return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams));
138
    }
139
140
    /**
141
     * @param string $id
142
     * @param array $params
143
     * @return string
144
     */
145
    public function getSocialUrl($id, array $params = [])
146
    {
147
        $defaultParams = config('twill.glide.social_default_params');
148
149
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
150
151
        $params = Arr::except($params, $this->cropParamsKeys);
152
153
        return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams));
154
    }
155
156
    /**
157
     * @param string $id
158
     * @return string
159
     */
160 3
    public function getCmsUrl($id, array $params = [])
161
    {
162 3
        $defaultParams = config('twill.glide.cms_default_params');
163
164 3
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
165
166 3
        $params = Arr::except($params, $this->cropParamsKeys);
167
168 3
        return $this->getUrl($id, array_replace($defaultParams, $params + $cropParams));
169
    }
170
171
    /**
172
     * @param string $id, string $preset
173
     * @return string
174
     */
175
    public function getPresetUrl($id, $preset)
176
    {
177
        return $this->getRawUrl($id) . '?p=' . $preset;
0 ignored issues
show
Bug introduced by
Are you sure $this->getRawUrl($id) of type array|null can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

177
        return /** @scrutinizer ignore-type */ $this->getRawUrl($id) . '?p=' . $preset;
Loading history...
178
    }
179
180
    /**
181
     * @param string $id
182
     * @return array|null
183
     */
184 3
    public function getRawUrl($id)
185
    {
186 3
        return $this->urlBuilder->getUrL($id);
187
    }
188
189
    /**
190
     * @param string $id
191
     * @return array
192
     */
193
    public function getDimensions($id)
194
    {
195
        $url = $this->urlBuilder->getUrL($id);
196
197
        try {
198
            list($w, $h) = getimagesize($url);
199
200
            return [
201
                'width' => $w,
202
                'height' => $h,
203
            ];
204
        } catch (\Exception $e) {
205
            return [
206
                'width' => 0,
207
                'height' => 0,
208
            ];
209
        }
210
    }
211
212
    /**
213
     * @param array $crop_params
214
     * @return array
215
     */
216
    protected function getCrop($crop_params)
217
    {
218
        if (!empty($crop_params)) {
219
            return ['crop' =>
220
                $crop_params['crop_w'] . ',' .
221
                $crop_params['crop_h'] . ',' .
222
                $crop_params['crop_x'] . ',' .
223
                $crop_params['crop_y'],
224
            ];
225
        }
226
227
        return [];
228
    }
229
230
    /**
231
     * @param array $crop_params
232
     * @param int $width
233
     * @param int $height
234
     * @return array
235
     */
236
    protected function getFocalPointCrop($crop_params, $width, $height)
237
    {
238
        if (!empty($crop_params)) {
239
            // determine center coordinates of user crop and express it in term of original image width and height percentage
240
            $fpX = 100 * ($crop_params['crop_w'] / 2 + $crop_params['crop_x']) / $width;
241
            $fpY = 100 * ($crop_params['crop_h'] / 2 + $crop_params['crop_y']) / $height;
242
243
            // determine focal zoom
244
            if ($crop_params['crop_w'] > $crop_params['crop_h']) {
245
                $fpZ = $width / ($crop_params['crop_w'] ?? $width);
246
            } else {
247
                $fpZ = $height / ($crop_params['crop_h'] ?? $height);
248
            }
249
250
            $fpX = number_format($fpX, 0, ".", "");
251
            $fpY = number_format($fpY, 0, ".", "");
252
            $fpZ = number_format($fpZ, 4, ".", "");
253
254
            $params = ['fit' => 'crop-' . $fpX . '-' . $fpY . '-' . $fpZ];
255
256
            return $params;
257
        }
258
259
        return [];
260
    }
261
}
262