Passed
Push — dependabot/npm_and_yarn/docs/i... ( c833a7...57c7e0 )
by
unknown
15:22 queued 08:01
created

Media::isReferenced()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace A17\Twill\Models;
4
5
use Exception;
6
use Illuminate\Support\Collection;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Str;
9
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...
10
11
class Media extends Model
12
{
13
    public $timestamps = true;
14
15
    protected $fillable = [
16
        'uuid',
17
        'filename',
18
        'alt_text',
19
        'caption',
20
        'width',
21
        'height',
22
    ];
23 49
24
    public function __construct(array $attributes = [])
25 49
    {
26
        $this->fillable(array_merge($this->fillable, Collection::make(config('twill.media_library.extra_metadatas_fields'))->map(function ($field) {
27 49
            return $field['name'];
28
        })->toArray()));
29 49
30
        Collection::make(config('twill.media_library.translatable_metadatas_fields'))->each(function ($field) {
31 49
            $this->casts[$field] = 'json';
32
        });
33 49
34 49
        parent::__construct($attributes);
35
    }
36
37
    public function scopeUnused ($query)
38
    {
39
        $usedIds = DB::table(config('twill.mediables_table'))->get()->pluck('media_id');
40
        return $query->whereNotIn('id', $usedIds->toArray())->get();
41
    }
42
43
    public function getDimensionsAttribute()
44
    {
45
        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...
46
    }
47 3
48
    public function altTextFrom($filename)
49 3
    {
50 3
        $filename = pathinfo($filename, PATHINFO_FILENAME);
51
        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

51
        if (Str::endsWith(/** @scrutinizer ignore-type */ $filename, '@2x')) {
Loading history...
52
            $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

52
            $filename = substr(/** @scrutinizer ignore-type */ $filename, 0, -2);
Loading history...
53
        }
54 3
55
        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

55
        return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename(/** @scrutinizer ignore-type */ $filename)));
Loading history...
56
    }
57 3
58
    public function canDeleteSafely()
59 3
    {
60
        return DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->count() === 0;
61
    }
62 3
63
    public function isReferenced()
64
    {
65 3
        return DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->count() > 0;
66 3
    }
67 3
68 3
    public function toCmsArray()
69 3
    {
70 3
        return [
71 3
            'id' => $this->id,
72 3
            '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...
73 1
            '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...
74 3
            'original' => ImageService::getRawUrl($this->uuid),
75 3
            'medium' => ImageService::getUrl($this->uuid, ["h" => "430"]),
76 3
            '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...
77 3
            '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...
78 3
            'tags' => $this->tags->map(function ($tag) {
0 ignored issues
show
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

78
            '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...
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...
79
                return $tag->name;
80
            }),
81 3
            '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

81
            'deleteUrl' => $this->canDeleteSafely() ? moduleRoute('medias', 'media-library', 'destroy', /** @scrutinizer ignore-type */ $this->id) : null,
Loading history...
82 3
            'updateUrl' => route('admin.media-library.medias.single-update'),
83
            'updateBulkUrl' => route('admin.media-library.medias.bulk-update'),
84 3
            'deleteBulkUrl' => route('admin.media-library.medias.bulk-delete'),
85
            'metadatas' => [
86
                'default' => [
87
                    '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...
88 3
                    '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...
89
                    'video' => null,
90
                ] + Collection::make(config('twill.media_library.extra_metadatas_fields'))->mapWithKeys(function ($field) {
91
                    return [
92
                        $field['name'] => $this->{$field['name']},
93
                    ];
94
                })->toArray(),
95
                'custom' => [
96
                    'caption' => null,
97
                    'altText' => null,
98
                    'video' => null,
99
                ],
100
            ],
101
        ];
102
    }
103
104
    public function getMetadata($name, $fallback = null)
105
    {
106
        $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...
107
        $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

107
        $language = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
108
109
        if ($metadatas->$name->$language ?? false) {
110
            return $metadatas->$name->$language;
111
        }
112
113
        $fallbackLocale = config('translatable.fallback_locale');
114
115
        if (in_array($name, config('twill.media_library.translatable_metadatas_fields', [])) && config('translatable.use_property_fallback', false) && ($metadatas->$name->$fallbackLocale ?? false)) {
116
            return $metadatas->$name->$fallbackLocale;
117
        }
118
119
        $fallbackValue = $fallback ? $this->$fallback : $this->$name;
120
121
        $fallback = $fallback ?? $name;
122
123
        if (in_array($fallback, config('twill.media_library.translatable_metadatas_fields', []))) {
124
            $fallbackValue = $fallbackValue[$language] ?? '';
125
126
            if ($fallbackValue === '' && config('translatable.use_property_fallback', false)) {
127
                $fallbackValue = $this->$fallback[config('translatable.fallback_locale')] ?? '';
128
            }
129
        }
130
131
        if (is_object($metadatas->$name ?? null)) {
132 49
            return $fallbackValue ?? '';
133
        }
134 49
135
        return $metadatas->$name ?? $fallbackValue ?? '';
136
    }
137
138
    public function replace($fields)
139
    {
140
        $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...
141
        $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...
142
143
        if ($this->update($fields) && $this->isReferenced())
144
        {
145
            DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->get()->each(function ($mediable) use ($prevWidth, $prevHeight) {
146
                
147
                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...
148
                    $mediable->crop_x = 0;
149
                    $mediable->crop_w = $this->width;
150
                }
151
152
                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...
153
                    $mediable->crop_y = 0;
154
                    $mediable->crop_h = $this->height;
155
                }
156
157
                DB::table(config('twill.mediables_table', 'twill_mediables'))->where('id', $mediable->id)->update((array)$mediable);
158
            });
159
        }
160
    }
161
162
    public function getTable()
163
    {
164
        return config('twill.medias_table', 'twill_medias');
165
    }
166
}
167