Passed
Push — master ( 9382b9...cf32f2 )
by Quentin
11:18 queued 04:19
created

HasMedias::imagesWithCrops()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 2
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 6
rs 10
1
<?php
2
3
namespace A17\Twill\Models\Behaviors;
4
5
use A17\Twill\Models\Media;
6
use Illuminate\Support\Arr;
7
use ImageService;
0 ignored issues
show
Bug introduced by
The type ImageService 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...
8
9
trait HasMedias
10
{
11
    protected $cropParamsKeys = [
12
        'crop_x',
13
        'crop_y',
14
        'crop_w',
15
        'crop_h',
16
    ];
17
18 22
    public function medias()
19
    {
20 22
        return $this->morphToMany(
0 ignored issues
show
Bug introduced by
It seems like morphToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

20
        return $this->/** @scrutinizer ignore-call */ morphToMany(
Loading history...
21 22
            Media::class,
22 22
            'mediable',
23 22
            config('twill.mediables_table', 'twill_mediables')
24 22
        )->withPivot(array_merge([
25 22
            'crop',
26
            'role',
27
            'crop_w',
28
            'crop_h',
29
            'crop_x',
30
            'crop_y',
31
            'lqip_data',
32
            'ratio',
33
            'metadatas',
34 22
        ], config('twill.media_library.translated_form_fields', false) ? ['locale'] : []))
35 22
            ->withTimestamps()->orderBy(config('twill.mediables_table', 'twill_mediables') . '.id', 'asc');
36
    }
37
38 2
    private function findMedia($role, $crop = "default")
39
    {
40
        $media = $this->medias->first(function ($media) use ($role, $crop) {
41
            if (config('twill.media_library.translated_form_fields', false)) {
42
                $localeScope = $media->pivot->locale === app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

42
                $localeScope = $media->pivot->locale === app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
43
            }
44
45
            return $media->pivot->role === $role && $media->pivot->crop === $crop && ($localeScope ?? true);
46 2
        });
47
48 2
        if (!$media && config('twill.media_library.translated_form_fields', false)) {
49
            $media = $this->medias->first(function ($media) use ($role, $crop) {
50
                return $media->pivot->role === $role && $media->pivot->crop === $crop;
51
            });
52
        }
53
54 2
        return $media;
55
    }
56
57
    public function hasImage($role, $crop = "default")
58
    {
59
        $media = $this->findMedia($role, $crop);
60
61
        return !empty($media);
62
    }
63
64 2
    public function image($role, $crop = "default", $params = [], $has_fallback = false, $cms = false, $media = null)
65
    {
66
67 2
        if (!$media) {
68 2
            $media = $this->findMedia($role, $crop);
69
        }
70
71 2
        if ($media) {
72
73
            $crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys);
74
75
            if ($cms) {
76
77
                return ImageService::getCmsUrl($media->uuid, $crop_params + $params);
78
            }
79
80
            return ImageService::getUrlWithCrop($media->uuid, $crop_params, $params);
81
        }
82
83 2
        if ($has_fallback) {
84
            return null;
85
        }
86
87 2
        return ImageService::getTransparentFallbackUrl();
88
    }
89
90
    public function images($role, $crop = "default", $params = [])
91
    {
92
        $medias = $this->medias->filter(function ($media) use ($role, $crop) {
93
            return $media->pivot->role === $role && $media->pivot->crop === $crop;
94
        });
95
96
        $urls = [];
97
98
        foreach ($medias as $media) {
99
            $urls[] = $this->image($role, $crop, $params, false, false, $media);
100
        }
101
102
        return $urls;
103
    }
104
105
    public function imagesWithCrops($role, $params = [])
106
    {
107
        $medias = $this->medias->filter(function ($media) use ($role) {
108
            return $media->pivot->role === $role;
109
        });
110
111
        $urls = [];
112
113
        foreach ($medias as $media) {
114
            $paramsForCrop = $params[$media->pivot->crop] ?? [];
115
            $urls[$media->id][$media->pivot->crop] = $this->image($role, $media->pivot->crop, $paramsForCrop, false, false, $media);
116
        }
117
118
        return $urls;
119
    }
120
121
    public function imageAsArray($role, $crop = "default", $params = [], $media = null)
122
    {
123
        if (!$media) {
124
            $media = $this->findMedia($role, $crop);
125
        }
126
127
        if ($media) {
128
            return [
129
                'src' => $this->image($role, $crop, $params, false, false, $media),
130
                'width' => $media->pivot->crop_w ?? $media->width,
131
                'height' => $media->pivot->crop_h ?? $media->height,
132
                'alt' => $this->imageAltText($role, $media),
133
                'caption' => $this->imageCaption($role, $media),
134
                'video' => $this->imageVideo($role, $media),
135
            ];
136
        }
137
138
        return [];
139
    }
140
141
    public function imagesAsArrays($role, $crop = "default", $params = [])
142
    {
143
        $medias = $this->medias->filter(function ($media) use ($role, $crop) {
144
            return $media->pivot->role === $role && $media->pivot->crop === $crop;
145
        });
146
147
        $arrays = [];
148
149
        foreach ($medias as $media) {
150
            $arrays[] = $this->imageAsArray($role, $crop, $params, $media);
151
        }
152
153
        return $arrays;
154
    }
155
156
    public function imagesAsArraysWithCrops($role, $params = [])
157
    {
158
        $medias = $this->medias->filter(function ($media) use ($role) {
159
            return $media->pivot->role === $role;
160
        });
161
162
        $arrays = [];
163
164
        foreach ($medias as $media) {
165
            $paramsForCrop = $params[$media->pivot->crop] ?? [];
166
            $arrays[$media->id][$media->pivot->crop] = $this->imageAsArray($role, $media->pivot->crop, $paramsForCrop, $media);
167
        }
168
169
        return $arrays;
170
    }
171
172
    public function imageAltText($role, $media = null)
173
    {
174
        if (!$media) {
175
            $media = $this->medias->first(function ($media) use ($role) {
176
                if (config('twill.media_library.translated_form_fields', false)) {
177
                    $localeScope = $media->pivot->locale === app()->getLocale();
178
                }
179
180
                return $media->pivot->role === $role && ($localeScope ?? true);;
181
            });
182
        }
183
184
        if ($media) {
185
            return $media->getMetadata('altText', 'alt_text');
186
        }
187
188
        return '';
189
    }
190
191
    public function imageCaption($role, $media = null)
192
    {
193
        if (!$media) {
194
            $media = $this->medias->first(function ($media) use ($role) {
195
                if (config('twill.media_library.translated_form_fields', false)) {
196
                    $localeScope = $media->pivot->locale === app()->getLocale();
197
                }
198
199
                return $media->pivot->role === $role && ($localeScope ?? true);;
200
            });
201
        }
202
203
        if ($media) {
204
            return $media->getMetadata('caption');
205
        }
206
207
        return '';
208
    }
209
210
    public function imageVideo($role, $media = null)
211
    {
212
        if (!$media) {
213
            $media = $this->medias->first(function ($media) use ($role) {
214
                if (config('twill.media_library.translated_form_fields', false)) {
215
                    $localeScope = $media->pivot->locale === app()->getLocale();
216
                }
217
218
                return $media->pivot->role === $role && ($localeScope ?? true);;
219
            });
220
        }
221
222
        if ($media) {
223
            $metadatas = (object) json_decode($media->pivot->metadatas);
224
            $language = app()->getLocale();
225
            return $metadatas->video->$language ?? (is_object($metadatas->video) ? '' : ($metadatas->video ?? ''));
226
        }
227
228
        return '';
229
    }
230
231
    public function imageObject($role, $crop = "default")
232
    {
233
        return $this->findMedia($role, $crop);
234
    }
235
236
    public function lowQualityImagePlaceholder($role, $crop = "default", $params = [], $has_fallback = false)
0 ignored issues
show
Unused Code introduced by
The parameter $params 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

236
    public function lowQualityImagePlaceholder($role, $crop = "default", /** @scrutinizer ignore-unused */ $params = [], $has_fallback = false)

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...
237 2
    {
238
        $media = $this->findMedia($role, $crop);
239 2
240
        if ($media) {
241
            return $media->pivot->lqip_data ?? ImageService::getTransparentFallbackUrl();
242 3
        }
243
244 3
        if ($has_fallback) {
245
            return null;
246 3
        }
247
248
        return ImageService::getTransparentFallbackUrl();
249
250 3
    }
251
252
    public function socialImage($role, $crop = "default", $params = [], $has_fallback = false)
253
    {
254
        $media = $this->findMedia($role, $crop);
255
256
        if ($media) {
257
            $crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys);
258
259
            return ImageService::getSocialUrl($media->uuid, $crop_params + $params);
260
        }
261
262
        if ($has_fallback) {
263
            return null;
264
        }
265
266
        return ImageService::getSocialFallbackUrl();
267
    }
268
269
    public function cmsImage($role, $crop = "default", $params = [])
270
    {
271
        return $this->image($role, $crop, $params, false, true, false) ?? ImageService::getTransparentFallbackUrl($params);
272
    }
273
274
    public function defaultCmsImage($params = [])
275
    {
276
        $media = $this->medias->first();
277
278
        if ($media) {
279
            return $this->image(null, null, $params, true, true, $media) ?? ImageService::getTransparentFallbackUrl($params);
280
        }
281
282
        return ImageService::getTransparentFallbackUrl($params);
283
    }
284
285
    public function imageObjects($role, $crop = "default")
286
    {
287
        return $this->medias->filter(function ($media) use ($role, $crop) {
288
            return $media->pivot->role === $role && $media->pivot->crop === $crop;
289
        });
290
    }
291
}
292