Passed
Push — main ( 030554...fba1e4 )
by Quentin
11:01 queued 04:37
created

TwicPics::getCrop()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.6111
1
<?php
2
3
namespace A17\Twill\Services\MediaLibrary;
4
5
use Illuminate\Support\Arr;
6
7
class TwicPics implements ImageServiceInterface
8
{
9
    use ImageServiceDefaults;
10
11
    protected $paramsProcessor;
12
13
    public function __construct(TwicPicsParamsProcessor $paramsProcessor)
14
    {
15
        $this->paramsProcessor = $paramsProcessor;
16
    }
17
18
    /**
19
     * @param string $id
20
     * @param array $params
21
     * @return string
22
     */
23
    public function getUrl($id, array $params = [])
24
    {
25
        $defaultParams = config('twill.twicpics.default_params');
26
27
        return $this->createUrl($id, array_replace($defaultParams, $params));
28
    }
29
30
    /**
31
     * @param string $id
32
     * @param array $crop_params
33
     * @param array $params
34
     * @return string
35
     */
36
    public function getUrlWithCrop($id, array $cropParams, array $params = [])
37
    {
38
        return $this->getUrl($id, $this->getCrop($cropParams) + $params);
39
    }
40
41
    /**
42
     * @param string $id
43
     * @param array $cropParams
44
     * @param int $width
45
     * @param int $height
46
     * @param array $params
47
     * @return string
48
     */
49
    public function getUrlWithFocalCrop($id, array $cropParams, $width, $height, array $params = [])
50
    {
51
        return $this->getUrl($id, $this->getFocalPointCrop($cropParams, $width, $height) + $params);
52
53
    }
54
55
    /**
56
     * @param string $id
57
     * @param array $params
58
     * @return string
59
     */
60
    public function getLQIPUrl($id, array $params = [])
61
    {
62
        $defaultParams = config('twill.twicpics.lqip_default_params');
63
64
        return $this->getUrlWithDefaultParams($id, $params, $defaultParams);
65
    }
66
67
    /**
68
     * @param string $id
69
     * @param array $params
70
     * @return string
71
     */
72
    public function getSocialUrl($id, array $params = [])
73
    {
74
        $defaultParams = config('twill.twicpics.social_default_params');
75
76
        return $this->getUrlWithDefaultParams($id, $params, $defaultParams);
77
    }
78
79
    /**
80
     * @param string $id
81
     * @param array $params
82
     * @return string
83
     */
84
    public function getCmsUrl($id, array $params = [])
85
    {
86
        $defaultParams = config('twill.twicpics.cms_default_params');
87
88
        return $this->getUrlWithDefaultParams($id, $params, $defaultParams);
89
    }
90
91
    /**
92
     * @param string $id
93
     * @return string
94
     */
95
    public function getRawUrl($id)
96
    {
97
        $domain = config('twill.twicpics.domain');
98
        $path = config('twill.twicpics.path');
99
100
        if (! empty($path)) {
101
            $path = "{$path}/";
102
        }
103
104
        return "https://{$domain}/{$path}{$id}";
105
    }
106
107
    /**
108
     * @param string $id
109
     * @return array|null
110
     */
111
    public function getDimensions($id)
112
    {
113
        return null;
114
    }
115
116
    /**
117
     * @param string $id
118
     * @param array $params
119
     * @return string
120
     */
121
    protected function createUrl($id, $params = [])
122
    {
123
        $rawUrl = $this->getRawUrl($id);
124
125
        $apiVersion = config('twill.twicpics.api_version');
126
127
        $params = $this->paramsProcessor->process($params);
128
129
        $manipulations = collect($params)->map(function ($value, $key) {
130
            return "{$key}={$value}";
131
        })->join('/');
132
133
        return "{$rawUrl}?twic={$apiVersion}/{$manipulations}";
134
    }
135
136
    /**
137
     * @param string $id
138
     * @param array $params
139
     * @param array $defaultParams
140
     * @return string
141
     */
142
    protected function getUrlWithDefaultParams($id, $params = [], $defaultParams = [])
143
    {
144
        $cropParams = Arr::has($params, $this->cropParamsKeys) ? $this->getCrop($params) : [];
145
146
        $params = Arr::except($params, $this->cropParamsKeys);
147
148
        return $this->getUrl($id, array_replace($defaultParams, $cropParams + $params));
149
    }
150
151
    /**
152
     * @param array $cropParams
153
     * @return array
154
     */
155
    protected function getCrop($cropParams)
156
    {
157
        $cropW = $cropParams['crop_w'] ?? null;
158
        $cropH = $cropParams['crop_h'] ?? null;
159
        $cropX = $cropParams['crop_x'] ?? null;
160
        $cropY = $cropParams['crop_y'] ?? null;
161
162
        if (!filled($cropW) || !filled($cropH)) {
163
            return [];
164
        }
165
166
        $expression = "{$cropW}x{$cropH}";
167
168
        if (filled($cropX) && filled($cropY)) {
169
            $expression .= "@{$cropX}x{$cropY}";
170
        }
171
172
        return ['crop' => $expression];
173
    }
174
175
    /**
176
     * @param array $cropParams
177
     * @param int $width
178
     * @param int $height
179
     * @return array
180
     */
181
    protected function getFocalPointCrop($cropParams, $width, $height)
0 ignored issues
show
Unused Code introduced by
The parameter $height is not used and could be removed. ( Ignorable by Annotation )

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

181
    protected function getFocalPointCrop($cropParams, $width, /** @scrutinizer ignore-unused */ $height)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $width is not used and could be removed. ( Ignorable by Annotation )

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

181
    protected function getFocalPointCrop($cropParams, /** @scrutinizer ignore-unused */ $width, $height)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
    {
183
        if (empty($cropParams)) {
184
            return [];
185
        }
186
187
        $cropW = $cropParams['crop_w'] ?? 0;
188
        $cropH = $cropParams['crop_h'] ?? 0;
189
        $cropX = $cropParams['crop_x'] ?? 0;
190
        $cropY = $cropParams['crop_y'] ?? 0;
191
192
        $focusX = $cropW / 2 + $cropX;
193
        $focusY = $cropH / 2 + $cropY;
194
195
        return ['focus' => "{$focusX}x{$focusY}"];
196
    }
197
}
198