Passed
Push — 2.x ( c00759...e3c99f )
by Quentin
07:55
created

Media::scopeUnused()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
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 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...
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 49
    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 49
        })->toArray()));
28
29
        Collection::make(config('twill.media_library.translatable_metadatas_fields'))->each(function ($field) {
30
            $this->casts[$field] = 'json';
31 49
        });
32
33 49
        parent::__construct($attributes);
34 49
    }
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 3
    public function altTextFrom($filename)
48
    {
49 3
        $filename = pathinfo($filename, PATHINFO_FILENAME);
50 3
        if (Str::endsWith($filename, '@2x')) {
51
            $filename = substr($filename, 0, -2);
52
        }
53
54 3
        return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename($filename)));
55
    }
56
57 3
    public function canDeleteSafely()
58
    {
59 3
        return DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->count() === 0;
60
    }
61
62 3
    public function toCmsArray()
63
    {
64
        return [
65 3
            'id' => $this->id,
66 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...
67 3
            '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...
68 3
            'original' => ImageService::getRawUrl($this->uuid),
69 3
            'medium' => ImageService::getUrl($this->uuid, ["h" => "430"]),
70 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...
71 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...
72
            '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

72
            '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...
73 1
                return $tag->name;
74 3
            }),
75 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

75
            'deleteUrl' => $this->canDeleteSafely() ? moduleRoute('medias', 'media-library', 'destroy', /** @scrutinizer ignore-type */ $this->id) : null,
Loading history...
76 3
            'updateUrl' => route('admin.media-library.medias.single-update'),
77 3
            'updateBulkUrl' => route('admin.media-library.medias.bulk-update'),
78 3
            'deleteBulkUrl' => route('admin.media-library.medias.bulk-delete'),
79
            'metadatas' => [
80
                'default' => [
81 3
                    '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...
82 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...
83
                    'video' => null,
84
                ] + Collection::make(config('twill.media_library.extra_metadatas_fields'))->mapWithKeys(function ($field) {
85
                    return [
86
                        $field['name'] => $this->{$field['name']},
87
                    ];
88 3
                })->toArray(),
89
                'custom' => [
90
                    'caption' => null,
91
                    'altText' => null,
92
                    'video' => null,
93
                ],
94
            ],
95
        ];
96
    }
97
98
    public function getMetadata($name, $fallback = null)
99
    {
100
        $metadatas = (object) json_decode($this->pivot->metadatas);
0 ignored issues
show
Bug introduced by
It seems like $this->pivot->metadatas can also be of type DateTimeZone and Illuminate\Support\HigherOrderCollectionProxy; however, parameter $json of json_decode() 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

100
        $metadatas = (object) json_decode(/** @scrutinizer ignore-type */ $this->pivot->metadatas);
Loading history...
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...
101
        $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

101
        $language = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
102
103
        if ($metadatas->$name->$language ?? false) {
104
            return $metadatas->$name->$language;
105
        }
106
107
        $fallbackLocale = config('translatable.fallback_locale');
108
109
        if (in_array($name, config('twill.media_library.translatable_metadatas_fields', [])) && config('translatable.use_property_fallback', false) && ($metadatas->$name->$fallbackLocale ?? false)) {
110
            return $metadatas->$name->$fallbackLocale;
111
        }
112
113
        $fallbackValue = $fallback ? $this->$fallback : $this->$name;
114
115
        $fallback = $fallback ?? $name;
116
117
        if (in_array($fallback, config('twill.media_library.translatable_metadatas_fields', []))) {
118
            $fallbackValue = $fallbackValue[$language] ?? '';
119
120
            if ($fallbackValue === '' && config('translatable.use_property_fallback', false)) {
121
                $fallbackValue = $this->$fallback[config('translatable.fallback_locale')] ?? '';
122
            }
123
        }
124
125
        if (is_object($metadatas->$name ?? null)) {
126
            return $fallbackValue ?? '';
127
        }
128
129
        return $metadatas->$name ?? $fallbackValue ?? '';
130
    }
131
132 49
    public function getTable()
133
    {
134 49
        return config('twill.medias_table', 'twill_medias');
135
    }
136
}
137