1 | <?php |
||||
2 | |||||
3 | namespace Itstructure\MFU\Models\Albums; |
||||
4 | |||||
5 | use Illuminate\Support\Collection as SupportCollection; |
||||
6 | use Illuminate\Database\Eloquent\{Model, Builder as EloquentBuilder, Collection as EloquentCollection}; |
||||
7 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||||
8 | use Itstructure\MFU\Interfaces\{HasOwnerInterface, BeingOwnerInterface}; |
||||
9 | use Itstructure\MFU\Behaviors\Owner\BehaviorMediafile; |
||||
10 | use Itstructure\MFU\Traits\{OwnerBehavior, Thumbnailable}; |
||||
11 | use Itstructure\MFU\Models\Owners\{OwnerAlbum, OwnerMediafile}; |
||||
12 | |||||
13 | /** |
||||
14 | * Class AlbumBase |
||||
15 | * @package Itstructure\MFU\Models\Albums |
||||
16 | */ |
||||
17 | class AlbumBase extends Model implements HasOwnerInterface, BeingOwnerInterface |
||||
18 | { |
||||
19 | use OwnerBehavior, Thumbnailable; |
||||
20 | |||||
21 | /** |
||||
22 | * @var string |
||||
23 | */ |
||||
24 | protected $table = 'albums'; |
||||
25 | |||||
26 | /** |
||||
27 | * @var array |
||||
28 | */ |
||||
29 | protected $fillable = ['title', 'description', 'type']; |
||||
30 | |||||
31 | /** |
||||
32 | * @return string |
||||
33 | */ |
||||
34 | public function getItsName(): string |
||||
35 | { |
||||
36 | return $this->type; |
||||
37 | } |
||||
38 | |||||
39 | /** |
||||
40 | * @return mixed |
||||
41 | */ |
||||
42 | public function getPrimaryKey() |
||||
43 | { |
||||
44 | return $this->getKey(); |
||||
45 | } |
||||
46 | |||||
47 | /** |
||||
48 | * @return array |
||||
49 | */ |
||||
50 | public static function getBehaviorAttributes(): array |
||||
51 | { |
||||
52 | return []; |
||||
53 | } |
||||
54 | |||||
55 | /** |
||||
56 | * @param int $ownerId |
||||
57 | * @param string $ownerName |
||||
58 | * @param string $ownerAttribute |
||||
59 | * @return bool |
||||
60 | */ |
||||
61 | public function addOwner(int $ownerId, string $ownerName, string $ownerAttribute): bool |
||||
62 | { |
||||
63 | return OwnerAlbum::addOwner($this->id, $ownerId, $ownerName, $ownerAttribute); |
||||
64 | } |
||||
65 | |||||
66 | /** |
||||
67 | * @param string|null $ownerAttribute |
||||
68 | * @return EloquentCollection |
||||
69 | */ |
||||
70 | public function getMediaFiles(string $ownerAttribute = null): EloquentCollection |
||||
71 | { |
||||
72 | return OwnerMediafile::getMediaFiles($this->type, $this->id, $ownerAttribute); |
||||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * @param string|null $ownerAttribute |
||||
77 | * @return EloquentBuilder |
||||
78 | */ |
||||
79 | public function getMediaFilesQuery(string $ownerAttribute = null): EloquentBuilder |
||||
80 | { |
||||
81 | return OwnerMediafile::getMediaFilesQuery([ |
||||
82 | 'owner_name' => $this->type, |
||||
83 | 'owner_id' => $this->id, |
||||
84 | 'owner_attribute' => $ownerAttribute, |
||||
85 | ]); |
||||
86 | } |
||||
87 | |||||
88 | /** |
||||
89 | * @return HasMany |
||||
90 | */ |
||||
91 | public function owners(): HasMany |
||||
92 | { |
||||
93 | return $this->hasMany(OwnerAlbum::class, 'album_id', 'id'); |
||||
94 | } |
||||
95 | |||||
96 | /** |
||||
97 | * @param bool $withOwners |
||||
98 | * @return EloquentBuilder |
||||
99 | */ |
||||
100 | public static function getAllQuery(bool $withOwners = false): EloquentBuilder |
||||
101 | { |
||||
102 | return static::query()->when($withOwners, function ($q) { |
||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||
103 | $q->with('owners'); |
||||
104 | }); |
||||
105 | } |
||||
106 | |||||
107 | /** |
||||
108 | * @param bool $withOwners |
||||
109 | * @return EloquentCollection |
||||
110 | */ |
||||
111 | public static function getAll(bool $withOwners = false): EloquentCollection |
||||
112 | { |
||||
113 | return static::getAllQuery($withOwners)->get(); |
||||
114 | } |
||||
115 | |||||
116 | public static function getAllForOwner(BeingOwnerInterface $ownerModel): SupportCollection |
||||
117 | { |
||||
118 | return static::getAll(true)->map(function ($album) use ($ownerModel) { |
||||
119 | $album->relates = false; |
||||
120 | $album->owners->map(function (OwnerAlbum $ownerAlbum) use ($album, $ownerModel) { |
||||
121 | if ($ownerModel->getItsName() == $ownerAlbum->getOwnerName() && $ownerModel->getPrimaryKey() == $ownerAlbum->getOwnerId()) { |
||||
122 | $album->relates = true; |
||||
123 | } |
||||
124 | }); |
||||
125 | return $album; |
||||
126 | }); |
||||
127 | } |
||||
128 | |||||
129 | protected static function booted(): void |
||||
130 | { |
||||
131 | $behavior = BehaviorMediafile::getInstance(static::getBehaviorAttributes()); |
||||
132 | |||||
133 | static::saved(function (Model $ownerModel) use ($behavior) { |
||||
134 | $ownerModel->wasRecentlyCreated |
||||
135 | ? $behavior->link($ownerModel) |
||||
0 ignored issues
–
show
$ownerModel of type Illuminate\Database\Eloquent\Model is incompatible with the type Itstructure\MFU\Interfaces\BeingOwnerInterface expected by parameter $ownerModel of Itstructure\MFU\Behaviors\Owner\Behavior::link() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
136 | : $behavior->refresh($ownerModel); |
||||
0 ignored issues
–
show
$ownerModel of type Illuminate\Database\Eloquent\Model is incompatible with the type Itstructure\MFU\Interfaces\BeingOwnerInterface expected by parameter $ownerModel of Itstructure\MFU\Behavior...ner\Behavior::refresh() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
137 | }); |
||||
138 | |||||
139 | static::deleted(function (Model $ownerModel) use ($behavior) { |
||||
140 | $behavior->clear($ownerModel); |
||||
0 ignored issues
–
show
$ownerModel of type Illuminate\Database\Eloquent\Model is incompatible with the type Itstructure\MFU\Interfaces\BeingOwnerInterface expected by parameter $ownerModel of Itstructure\MFU\Behaviors\Owner\Behavior::clear() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
141 | }); |
||||
142 | } |
||||
143 | } |
||||
144 |