Passed
Push — laravel-9-support ( aa220f...3f4979 )
by Harings
44:54 queued 29:22
created

Media::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Str;
8
use A17\Twill\Services\MediaLibrary\ImageService;
9
10
class Media extends Model
11
{
12
    public $timestamps = true;
13
14
    protected $fillable = [
15
        'uuid',
16
        'filename',
17
        'alt_text',
18
        'caption',
19
        'width',
20
        'height',
21
    ];
22
23
    public function __construct(array $attributes = [])
24
    {
25
        $this->fillable(array_merge($this->fillable, Collection::make(config('twill.media_library.extra_metadatas_fields'))->map(function ($field) {
26
            return $field['name'];
27
        })->toArray()));
28
29
        Collection::make(config('twill.media_library.translatable_metadatas_fields'))->each(function ($field) {
30
            $this->casts[$field] = 'json';
31
        });
32
33
        parent::__construct($attributes);
34
    }
35
36
    public function scopeUnused ($query)
37
    {
38
        $usedIds = DB::table(config('twill.mediables_table'))->get()->pluck('media_id');
39
        return $query->whereNotIn('id', $usedIds->toArray())->get();
40
    }
41
42
    public function getDimensionsAttribute()
43
    {
44
        return $this->width . 'x' . $this->height;
0 ignored issues
show
Bug Best Practice introduced by
The property width does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property height does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
45
    }
46
47
    public function altTextFrom($filename)
48
    {
49
        $filename = pathinfo($filename, PATHINFO_FILENAME);
50
        if (Str::endsWith($filename, '@2x')) {
0 ignored issues
show
Bug introduced by
It seems like $filename can also be of type array; however, parameter $haystack of Illuminate\Support\Str::endsWith() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

50
        if (Str::endsWith(/** @scrutinizer ignore-type */ $filename, '@2x')) {
Loading history...
51
            $filename = substr($filename, 0, -2);
0 ignored issues
show
Bug introduced by
It seems like $filename can also be of type array; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

51
            $filename = substr(/** @scrutinizer ignore-type */ $filename, 0, -2);
Loading history...
52
        }
53
54
        return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename($filename)));
0 ignored issues
show
Bug introduced by
It seems like $filename can also be of type array; however, parameter $filename of sanitizeFilename() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

54
        return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename(/** @scrutinizer ignore-type */ $filename)));
Loading history...
55
    }
56
57
    public function canDeleteSafely()
58
    {
59
        return DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->count() === 0;
60
    }
61
62
    public function isReferenced()
63
    {
64
        return DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->count() > 0;
65
    }
66
67
    public function toCmsArray()
68
    {
69
        return [
70
            'id' => $this->id,
71
            'name' => $this->filename,
0 ignored issues
show
Bug Best Practice introduced by
The property filename does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
72
            'thumbnail' => ImageService::getCmsUrl($this->uuid, ["h" => "256"]),
0 ignored issues
show
Bug Best Practice introduced by
The property uuid does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method getCmsUrl() does not exist on A17\Twill\Services\MediaLibrary\ImageService. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

72
            'thumbnail' => ImageService::/** @scrutinizer ignore-call */ getCmsUrl($this->uuid, ["h" => "256"]),
Loading history...
73
            'original' => ImageService::getRawUrl($this->uuid),
0 ignored issues
show
Bug introduced by
The method getRawUrl() does not exist on A17\Twill\Services\MediaLibrary\ImageService. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

73
            'original' => ImageService::/** @scrutinizer ignore-call */ getRawUrl($this->uuid),
Loading history...
74
            'medium' => ImageService::getUrl($this->uuid, ["h" => "430"]),
0 ignored issues
show
Bug introduced by
The method getUrl() does not exist on A17\Twill\Services\MediaLibrary\ImageService. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

74
            'medium' => ImageService::/** @scrutinizer ignore-call */ getUrl($this->uuid, ["h" => "430"]),
Loading history...
75
            'width' => $this->width,
0 ignored issues
show
Bug Best Practice introduced by
The property width does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
76
            'height' => $this->height,
0 ignored issues
show
Bug Best Practice introduced by
The property height does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
77
            'tags' => $this->tags->map(function ($tag) {
0 ignored issues
show
Bug Best Practice introduced by
The property tags does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method map() does not exist on null. ( Ignorable by Annotation )

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

77
            'tags' => $this->tags->/** @scrutinizer ignore-call */ map(function ($tag) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
                return $tag->name;
79
            }),
80
            'deleteUrl' => $this->canDeleteSafely() ? moduleRoute('medias', 'media-library', 'destroy', $this->id) : null,
0 ignored issues
show
Bug introduced by
$this->id of type integer is incompatible with the type array expected by parameter $parameters of moduleRoute(). ( Ignorable by Annotation )

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

80
            'deleteUrl' => $this->canDeleteSafely() ? moduleRoute('medias', 'media-library', 'destroy', /** @scrutinizer ignore-type */ $this->id) : null,
Loading history...
81
            'updateUrl' => route('admin.media-library.medias.single-update'),
82
            'updateBulkUrl' => route('admin.media-library.medias.bulk-update'),
83
            'deleteBulkUrl' => route('admin.media-library.medias.bulk-delete'),
84
            'metadatas' => [
85
                'default' => [
86
                    'caption' => $this->caption,
0 ignored issues
show
Bug Best Practice introduced by
The property caption does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
87
                    'altText' => $this->alt_text,
0 ignored issues
show
Bug Best Practice introduced by
The property alt_text does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
88
                    'video' => null,
89
                ] + Collection::make(config('twill.media_library.extra_metadatas_fields'))->mapWithKeys(function ($field) {
90
                    return [
91
                        $field['name'] => $this->{$field['name']},
92
                    ];
93
                })->toArray(),
94
                'custom' => [
95
                    'caption' => null,
96
                    'altText' => null,
97
                    'video' => null,
98
                ],
99
            ],
100
        ];
101
    }
102
103
    public function getMetadata($name, $fallback = null)
104
    {
105
        $metadatas = (object) json_decode($this->pivot->metadatas);
0 ignored issues
show
Bug introduced by
The property metadatas does not seem to exist on Illuminate\Database\Eloquent\Relations\Relation.
Loading history...
Bug Best Practice introduced by
The property pivot does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
106
        $language = 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

106
        $language = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
107
108
        if ($metadatas->$name->$language ?? false) {
109
            return $metadatas->$name->$language;
110
        }
111
112
        $fallbackLocale = config('translatable.fallback_locale');
113
114
        if (in_array($name, config('twill.media_library.translatable_metadatas_fields', [])) && config('translatable.use_property_fallback', false) && ($metadatas->$name->$fallbackLocale ?? false)) {
115
            return $metadatas->$name->$fallbackLocale;
116
        }
117
118
        $fallbackValue = $fallback ? $this->$fallback : $this->$name;
119
120
        $fallback = $fallback ?? $name;
121
122
        if (in_array($fallback, config('twill.media_library.translatable_metadatas_fields', []))) {
123
            $fallbackValue = $fallbackValue[$language] ?? '';
124
125
            if ($fallbackValue === '' && config('translatable.use_property_fallback', false)) {
126
                $fallbackValue = $this->$fallback[config('translatable.fallback_locale')] ?? '';
127
            }
128
        }
129
130
        if (is_object($metadatas->$name ?? null)) {
131
            return $fallbackValue ?? '';
132
        }
133
134
        return $metadatas->$name ?? $fallbackValue ?? '';
135
    }
136
137
    public function replace($fields)
138
    {
139
        $prevHeight = $this->height;
0 ignored issues
show
Bug Best Practice introduced by
The property height does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
140
        $prevWidth = $this->width;
0 ignored issues
show
Bug Best Practice introduced by
The property width does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
141
142
        if ($this->update($fields) && $this->isReferenced())
143
        {
144
            DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->get()->each(function ($mediable) use ($prevWidth, $prevHeight) {
145
146
                if ($prevWidth != $this->width) {
0 ignored issues
show
Bug Best Practice introduced by
The property width does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
147
                    $mediable->crop_x = 0;
148
                    $mediable->crop_w = $this->width;
149
                }
150
151
                if ($prevHeight != $this->height) {
0 ignored issues
show
Bug Best Practice introduced by
The property height does not exist on A17\Twill\Models\Media. Since you implemented __get, consider adding a @property annotation.
Loading history...
152
                    $mediable->crop_y = 0;
153
                    $mediable->crop_h = $this->height;
154
                }
155
156
                DB::table(config('twill.mediables_table', 'twill_mediables'))->where('id', $mediable->id)->update((array)$mediable);
157
            });
158
        }
159
    }
160
161
    public function delete()
162
    {
163
        if ($this->canDeleteSafely()) {
164
            return parent::delete();
165
        }
166
        return false;
167
    }
168
169
    public function getTable()
170
    {
171
        return config('twill.medias_table', 'twill_medias');
172
    }
173
}
174