Passed
Push — master ( 3a3605...2e332d )
by Quentin
06:36 queued 10s
created

HasMedias   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Test Coverage

Coverage 22.32%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 112
dl 0
loc 248
ccs 25
cts 112
cp 0.2232
rs 7.44
c 2
b 0
f 0
wmc 52

16 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultCmsImage() 0 9 2
A imageObjects() 0 4 2
A imageAltText() 0 17 5
A cmsImage() 0 3 1
B findMedia() 0 17 7
A socialImage() 0 15 3
A imagesAsArrays() 0 13 3
A imageVideo() 0 19 6
A imageCaption() 0 17 5
A hasImage() 0 5 1
A imageAsArray() 0 18 3
A images() 0 13 3
A image() 0 24 5
A lowQualityImagePlaceholder() 0 13 3
A imageObject() 0 3 1
A medias() 0 18 2

How to fix   Complexity   

Complex Class

Complex classes like HasMedias often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use HasMedias, and based on these observations, apply Extract Interface, too.

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 20
    public function medias()
19
    {
20 20
        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 20
            Media::class,
22 20
            'mediable',
23 20
            config('twill.mediables_table', 'twill_mediables')
24 20
        )->withPivot(array_merge([
25 20
            'crop',
26
            'role',
27
            'crop_w',
28
            'crop_h',
29
            'crop_x',
30
            'crop_y',
31
            'lqip_data',
32
            'ratio',
33
            'metadatas',
34 20
        ], config('twill.media_library.translated_form_fields', false) ? ['locale'] : []))
35 20
            ->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 imageAsArray($role, $crop = "default", $params = [], $media = null)
106
    {
107
        if (!$media) {
108
            $media = $this->findMedia($role, $crop);
109
        }
110
111
        if ($media) {
112
            return [
113
                'src' => $this->image($role, $crop, $params, false, false, $media),
114
                'width' => $media->pivot->crop_w ?? $media->width,
115
                'height' => $media->pivot->crop_h ?? $media->height,
116
                'alt' => $this->imageAltText($role, $media),
117
                'caption' => $this->imageCaption($role, $media),
118
                'video' => $this->imageVideo($role, $media),
119
            ];
120
        }
121
122
        return [];
123
    }
124
125
    public function imagesAsArrays($role, $crop = "default", $params = [])
126
    {
127
        $medias = $this->medias->filter(function ($media) use ($role, $crop) {
128
            return $media->pivot->role === $role && $media->pivot->crop === $crop;
129
        });
130
131
        $arrays = [];
132
133
        foreach ($medias as $media) {
134
            $arrays[] = $this->imageAsArray($role, $crop, $params, $media);
135
        }
136
137
        return $arrays;
138
    }
139
140
    public function imageAltText($role, $media = null)
141
    {
142
        if (!$media) {
143
            $media = $this->medias->first(function ($media) use ($role) {
144
                if (config('twill.media_library.translated_form_fields', false)) {
145
                    $localeScope = $media->pivot->locale === app()->getLocale();
146
                }
147
148
                return $media->pivot->role === $role && ($localeScope ?? true);;
149
            });
150
        }
151
152
        if ($media) {
153
            return $media->getMetadata('altText', 'alt_text');
154
        }
155
156
        return '';
157
    }
158
159
    public function imageCaption($role, $media = null)
160
    {
161
        if (!$media) {
162
            $media = $this->medias->first(function ($media) use ($role) {
163
                if (config('twill.media_library.translated_form_fields', false)) {
164
                    $localeScope = $media->pivot->locale === app()->getLocale();
165
                }
166
167
                return $media->pivot->role === $role && ($localeScope ?? true);;
168
            });
169
        }
170
171
        if ($media) {
172
            return $media->getMetadata('caption');
173
        }
174
175
        return '';
176
    }
177
178
    public function imageVideo($role, $media = null)
179
    {
180
        if (!$media) {
181
            $media = $this->medias->first(function ($media) use ($role) {
182
                if (config('twill.media_library.translated_form_fields', false)) {
183
                    $localeScope = $media->pivot->locale === app()->getLocale();
184
                }
185
186
                return $media->pivot->role === $role && ($localeScope ?? true);;
187
            });
188
        }
189
190
        if ($media) {
191
            $metadatas = (object) json_decode($media->pivot->metadatas);
192
            $language = app()->getLocale();
193
            return $metadatas->video->$language ?? (is_object($metadatas->video) ? '' : ($metadatas->video ?? ''));
194
        }
195
196
        return '';
197
    }
198
199
    public function imageObject($role, $crop = "default")
200
    {
201
        return $this->findMedia($role, $crop);
202
    }
203
204
    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

204
    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...
205
    {
206
        $media = $this->findMedia($role, $crop);
207
208
        if ($media) {
209
            return $media->pivot->lqip_data ?? ImageService::getTransparentFallbackUrl();
210
        }
211
212
        if ($has_fallback) {
213
            return null;
214
        }
215
216
        return ImageService::getTransparentFallbackUrl();
217
218
    }
219
220
    public function socialImage($role, $crop = "default", $params = [], $has_fallback = false)
221
    {
222
        $media = $this->findMedia($role, $crop);
223
224
        if ($media) {
225
            $crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys);
226
227
            return ImageService::getSocialUrl($media->uuid, $crop_params + $params);
228
        }
229
230
        if ($has_fallback) {
231
            return null;
232
        }
233
234
        return ImageService::getSocialFallbackUrl();
235
    }
236
237 2
    public function cmsImage($role, $crop = "default", $params = [])
238
    {
239 2
        return $this->image($role, $crop, $params, false, true, false) ?? ImageService::getTransparentFallbackUrl($params);
240
    }
241
242 2
    public function defaultCmsImage($params = [])
243
    {
244 2
        $media = $this->medias->first();
245
246 2
        if ($media) {
247
            return $this->image(null, null, $params, true, true, $media) ?? ImageService::getTransparentFallbackUrl($params);
248
        }
249
250 2
        return ImageService::getTransparentFallbackUrl($params);
251
    }
252
253
    public function imageObjects($role, $crop = "default")
254
    {
255
        return $this->medias->filter(function ($media) use ($role, $crop) {
256
            return $media->pivot->role === $role && $media->pivot->crop === $crop;
257
        });
258
    }
259
}
260