OwnerMediafile   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 25
c 1
b 0
f 0
dl 0
loc 145
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getOwnerThumbnailModel() 0 13 2
A getMediaFile() 0 3 1
A getAudioFiles() 0 3 1
A getOtherFiles() 0 3 1
A getTextFiles() 0 3 1
A getImageFiles() 0 3 1
A getAppFiles() 0 3 1
A getVideoFiles() 0 3 1
A getMediaFilesQuery() 0 3 1
A removeDependency() 0 3 1
A getDependencyKeyName() 0 3 1
A getMediaFiles() 0 3 1
1
<?php
2
3
namespace Itstructure\MFU\Models\Owners;
4
5
use Illuminate\Database\Eloquent\{Collection, Builder as EloquentBuilder};
6
use Itstructure\MFU\Facades\Uploader;
7
use Itstructure\MFU\Traits\HasCompositePrimaryKey;
8
use Itstructure\MFU\Processors\SaveProcessor;
9
use Itstructure\MFU\Models\Mediafile;
10
11
/**
12
 * Class OwnerMediafile
13
 * @package Itstructure\MFU\Models\Owners
14
 */
15
class OwnerMediafile extends Owner
16
{
17
    use HasCompositePrimaryKey;
18
19
    public $incrementing = false;
20
21
    protected $primaryKey = ['mediafile_id', 'owner_id', 'owner_name', 'owner_attribute'];
22
23
    protected $table = 'owners_mediafiles';
24
25
    protected $fillable = ['mediafile_id', 'owner_id', 'owner_name', 'owner_attribute'];
26
27
    /**
28
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
29
     */
30
    public function getMediaFile()
31
    {
32
        return $this->hasOne(Mediafile::class, 'mediafile_id', 'id');
33
    }
34
35
    /**
36
     * Get all mediafiles by owner.
37
     * @param string $ownerName
38
     * @param int    $ownerId
39
     * @param null|string $ownerAttribute
40
     * @return Collection
41
     */
42
    public static function getMediaFiles(string $ownerName, int $ownerId, string $ownerAttribute = null): Collection
43
    {
44
        return static::getMediaFilesQuery(static::buildFilterOptions($ownerId, $ownerName, $ownerAttribute))->get();
45
    }
46
47
    /**
48
     * Get all mediafiles query by owner.
49
     * @param array $args. It can be an array of the next params: owner_name{string}, owner_id{int}, owner_attribute{string}.
50
     * @return EloquentBuilder
51
     */
52
    public static function getMediaFilesQuery(array $args = []): EloquentBuilder
53
    {
54
        return Mediafile::query()->whereIn('id', static::getEntityIdsQuery('mediafile_id', $args)->get()->pluck('mediafile_id'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return Itstructure\MFU\M...>pluck('mediafile_id')) could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
55
    }
56
57
    /**
58
     * Get one owner thumbnail file by owner.
59
     * @param string $ownerName
60
     * @param int    $ownerId
61
     * @return Mediafile|null
62
     */
63
    public static function getOwnerThumbnailModel(string $ownerName, int $ownerId): ?Mediafile
64
    {
65
        $ownerMediafileModel = static::getEntityIdsQuery('mediafile_id', [
66
            'owner_name' => $ownerName,
67
            'owner_id' => $ownerId,
68
            'owner_attribute' => SaveProcessor::FILE_TYPE_THUMB,
69
        ])->first();
70
71
        if (null === $ownerMediafileModel) {
72
            return null;
73
        }
74
75
        return Mediafile::find($ownerMediafileModel->mediafile_id);
76
    }
77
78
    /**
79
     * Get image files by owner.
80
     * @param string $ownerName
81
     * @param int    $ownerId
82
     * @return Collection
83
     */
84
    public static function getImageFiles(string $ownerName, int $ownerId): Collection
85
    {
86
        return static::getMediaFiles($ownerName, $ownerId, SaveProcessor::FILE_TYPE_IMAGE);
87
    }
88
89
    /**
90
     * Get audio files by owner.
91
     * @param string $ownerName
92
     * @param int    $ownerId
93
     * @return Collection
94
     */
95
    public static function getAudioFiles(string $ownerName, int $ownerId): Collection
96
    {
97
        return static::getMediaFiles($ownerName, $ownerId, SaveProcessor::FILE_TYPE_AUDIO);
98
    }
99
100
    /**
101
     * Get video files by owner.
102
     * @param string $ownerName
103
     * @param int    $ownerId
104
     * @return Collection
105
     */
106
    public static function getVideoFiles(string $ownerName, int $ownerId): Collection
107
    {
108
        return static::getMediaFiles($ownerName, $ownerId, SaveProcessor::FILE_TYPE_VIDEO);
109
    }
110
111
    /**
112
     * Get app files by owner.
113
     * @param string $ownerName
114
     * @param int    $ownerId
115
     * @return Collection
116
     */
117
    public static function getAppFiles(string $ownerName, int $ownerId): Collection
118
    {
119
        return static::getMediaFiles($ownerName, $ownerId, SaveProcessor::FILE_TYPE_APP);
120
    }
121
122
    /**
123
     * Get text files by owner.
124
     * @param string $ownerName
125
     * @param int    $ownerId
126
     * @return Collection
127
     */
128
    public static function getTextFiles(string $ownerName, int $ownerId): Collection
129
    {
130
        return static::getMediaFiles($ownerName, $ownerId, SaveProcessor::FILE_TYPE_TEXT);
131
    }
132
133
    /**
134
     * Get other files by owner.
135
     * @param string $ownerName
136
     * @param int    $ownerId
137
     * @return Collection
138
     */
139
    public static function getOtherFiles(string $ownerName, int $ownerId): Collection
140
    {
141
        return static::getMediaFiles($ownerName, $ownerId, SaveProcessor::FILE_TYPE_OTHER);
142
    }
143
144
    /**
145
     * Get model mediafile primary key name.
146
     * @return string
147
     */
148
    protected static function getDependencyKeyName(): string
149
    {
150
        return 'mediafile_id';
151
    }
152
153
    /**
154
     * @param int $mediafileId
155
     * @return bool
156
     */
157
    protected static function removeDependency(int $mediafileId): bool
158
    {
159
        return Uploader::delete($mediafileId);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Itstructure\MFU\Facades\Uploader. 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

159
        return Uploader::/** @scrutinizer ignore-call */ delete($mediafileId);
Loading history...
160
    }
161
}
162