-
-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathBaseAlbumImpl.php
More file actions
325 lines (301 loc) · 12.4 KB
/
BaseAlbumImpl.php
File metadata and controls
325 lines (301 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/
namespace App\Models;
use App\Constants\AccessPermissionConstants as APC;
use App\Constants\RandomID;
use App\Contracts\Models\HasRandomID;
use App\DTO\PhotoSortingCriterion;
use App\Enum\ColumnSortingType;
use App\Enum\OrderSortingType;
use App\Enum\PhotoLayoutType;
use App\Enum\TimelinePhotoGranularity;
use App\Models\Builders\BaseAlbumImplBuilder;
use App\Models\Extensions\HasBidirectionalRelationships;
use App\Models\Extensions\HasRandomIDAndLegacyTimeBasedID;
use App\Models\Extensions\ThrowsConsistentExceptions;
use App\Models\Extensions\ToArrayThrowsNotImplemented;
use App\Models\Extensions\UTCBasedTimes;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Auth;
/**
* App\Models\BaseAlbumImpl.
*
* This class contains the shared implementation of {@link \App\Models\Album}
* and {@link \App\Models\TagAlbum} which normally would be put into a common
* parent class.
* However, Eloquent does not provide mapping of class inheritance to table
* inheritance.
* (For an introduction into this topic see
* [Martin Fowler](https://martinfowler.com/eaaCatalog/classTableInheritance.html)
* and
* [Doctrine Documentation](https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/inheritance-mapping.html#class-table-inheritance)).
* The main obstacle is that Eloquent cannot handle attributes of a child
* class which are either stored in the table of the parent class (if the
* attributes are inherited) or stored in the table the child class (if the
* attributes are specific to the child class).
* Hence, we take the second best approach here: using composites.
* The actual child classes {@link \App\Models\Album} and
* {@link \App\Models\TagAlbum} both extend
* {@link \Illuminate\Database\Eloquent\Model} and are composites which
* use this class as "building block".
* This means instead of inheriting from this class, {@link \App\Models\Album}
* and {@link \App\Models\TagAlbum} hold a reference to the implementation of
* their "parent" class.
* Basically, the architecture looks like this
*
* +---------+ +-----------------+
* | Model | | <<interface>> |
* +---------+ | BaseAlbum |
* ^ ^ ^ +-----------------+
* | | \ ^ ^
* | | \ | |
* | \ \-----------------|------\ |
* | \----------------\ | \ |
* | +-------+ \ |
* | | Album | | |
* +---------------+ <---X +-------+ +----------+
* | BaseAlbumImpl | | TagAlbum |
* +---------------+ <----------------X +----------+
*
* (Note: A sideways arrow with an X, i.e. <-----X, shall denote a composite.)
* All child classes and this class extend
* {@link \Illuminate\Database\Eloquent\Model}, because they map to a single
* DB table.
* All methods and properties which are common to any sort of persistable
* album is declared in the interface {@link \App\Contracts\BaseAlbum}
* and thus {@link \App\Models\Album} and {@link \App\Models\TagAlbum}
* realize it.
* However, for any method which is implemented identically for all
* child classes and thus would normally be defined in a true parent class,
* the child classes forward the call to this class via the composite.
* For this reason, this class is called `BaseAlbumImpl` like _implementation_.
* Also note, that this class does not realize
* {@link \App\Contracts\BaseAlbum} intentionally.
* The interface {@link \App\Contracts\BaseAlbum} requires methods from
* albums which this class cannot implement reasonably, because the
* implementation depends on the specific sub-type of album and thus must
* be implemented by the child classes.
* For example, every album contains photos and thus must provide
* {@link \App\Contracts\Models\AbstractAlbum::$photos}, but the way how an album
* defines its collection of photos is specific for the album.
* Normally, a proper parent class would use abstract methods for these cases,
* but this class is not a proper parent class (it just provides an
* implementation of it) and we need this class to be instantiable.
*
* @property string $id
* @property Carbon $created_at
* @property Carbon $updated_at
* @property string $title
* @property string|null $description
* @property PhotoLayoutType|null $photo_layout
* @property TimelinePhotoGranularity $photo_timeline
* @property int $owner_id
* @property User $owner
* @property bool $is_nsfw
* @property Collection $shared_with
* @property int|null $shared_with_count
* @property PhotoSortingCriterion|null $photo_sorting
* @property string|null $sorting_col
* @property string|null $sorting_order
* @property Collection<int,AccessPermission> $access_permissions
* @property int|null $access_permissions_count
* @property string|null $copyright
*
* @method static BaseAlbumImplBuilder|BaseAlbumImpl addSelect($column)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl join(string $table, string $first, string $operator = null, string $second = null, string $type = 'inner', string $where = false)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl leftJoin(string $table, string $first, string $operator = null, string $second = null)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl newModelQuery()
* @method static BaseAlbumImplBuilder|BaseAlbumImpl newQuery()
* @method static BaseAlbumImplBuilder|BaseAlbumImpl orderBy($column, $direction = 'asc')
* @method static BaseAlbumImplBuilder|BaseAlbumImpl query()
* @method static BaseAlbumImplBuilder|BaseAlbumImpl select($columns = [])
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereCreatedAt($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereDescription($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereId($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereIn(string $column, string $values, string $boolean = 'and', string $not = false)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereIsNsfw($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereLegacyId($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereNotIn(string $column, string $values, string $boolean = 'and')
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereOwnerId($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereSortingCol($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereSortingOrder($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereTitle($value)
* @method static BaseAlbumImplBuilder|BaseAlbumImpl whereUpdatedAt($value)
*
* @mixin \Eloquent
*/
class BaseAlbumImpl extends Model implements HasRandomID
{
/** @phpstan-use HasRandomIDAndLegacyTimeBasedID<BaseAlbumImpl> */
use HasRandomIDAndLegacyTimeBasedID;
use ThrowsConsistentExceptions;
use UTCBasedTimes;
use HasBidirectionalRelationships;
use ToArrayThrowsNotImplemented;
protected $table = 'base_albums';
/**
* @var string The type of the primary key
*/
protected $keyType = RandomID::ID_TYPE;
/**
* Indicates if the model's primary key is auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The model's attributes.
*
* We must list all attributes explicitly here, otherwise the attributes
* of a new model will accidentally be set on the child class.
* The trait {@link \App\Models\Extensions\ForwardsToParentImplementation}
* only works properly, if it knows which attributes belong to the parent
* class and which attributes belong to the child class.
*
* @var array<string, mixed>
*/
protected $attributes = [
'id' => null,
'created_at' => null,
'updated_at' => null,
'title' => null, // Sic! `title` is actually non-nullable, but using `null` here forces the caller to actually set a title before saving.
'description' => null,
'owner_id' => 0,
'sorting_col' => null,
'sorting_order' => null,
'copyright' => null,
// Special visibility attributes
'is_nsfw' => false,
'photo_layout' => null,
// 'statistics' => null,
];
/**
* @var array<string, string>
*/
protected $casts = [
'id' => RandomID::ID_TYPE,
'created_at' => 'datetime',
'updated_at' => 'datetime',
'is_nsfw' => 'boolean',
'owner_id' => 'integer',
'photo_layout' => PhotoLayoutType::class,
];
/**
* The relationships that should always be eagerly loaded by default.
*/
protected $with = ['owner', 'access_permissions', 'statistics'];
public function newEloquentBuilder($query): BaseAlbumImplBuilder
{
return new BaseAlbumImplBuilder($query);
}
/**
* Returns the relationship between an album and its owner.
*
* @return BelongsTo<User,$this>
*/
public function owner(): BelongsTo
{
return $this->belongsTo(User::class, 'owner_id', 'id');
}
/**
* Returns the relationship between an album and all users with whom
* this album is shared.
*
* @return BelongsToMany<User,$this>
*/
public function shared_with(): BelongsToMany
{
return $this->belongsToMany(
User::class,
APC::ACCESS_PERMISSIONS,
APC::BASE_ALBUM_ID,
APC::USER_ID
)->wherePivotNotNull('user_id');
}
/**
* Returns the relationship between an album and its associated permissions.
*
* @return hasMany<AccessPermission,$this>
*/
public function access_permissions(): hasMany
{
return $this->hasMany(AccessPermission::class, APC::BASE_ALBUM_ID, 'id');
}
/**
* Returns the relationship between an album and its associated current user permissions.
*/
public function current_user_permissions(): AccessPermission|null
{
return $this->access_permissions->first(fn (AccessPermission $p) => $p->user_id !== null && $p->user_id === Auth::id());
}
/**
* Returns the relationship between an album and its associated public permissions.
*/
public function public_permissions(): AccessPermission|null
{
return $this->access_permissions->first(fn (AccessPermission $p) => $p->user_id === null);
}
/**
* Returns the relationship between an album and its associated statistics.
*
* @return HasOne<Statistics,$this>
*/
public function statistics(): HasOne
{
return $this->hasOne(Statistics::class, 'album_id', 'id');
}
protected function getPhotoSortingAttribute(): ?PhotoSortingCriterion
{
$sorting_column = $this->attributes['sorting_col'];
$sorting_order = $this->attributes['sorting_order'];
return ($sorting_column === null || $sorting_order === null) ?
null :
new PhotoSortingCriterion(
ColumnSortingType::from($sorting_column),
OrderSortingType::from($sorting_order));
}
protected function setPhotoSortingAttribute(?PhotoSortingCriterion $sorting): void
{
$this->attributes['sorting_col'] = $sorting?->column->value;
$this->attributes['sorting_order'] = $sorting?->order->value;
}
/**
* Defines accessor for the Aspect Ratio.
*/
protected function getPhotoLayoutAttribute(): ?PhotoLayoutType
{
return PhotoLayoutType::tryFrom($this->attributes['photo_layout']);
}
/**
* Defines setter for Aspect Ratio.
*/
protected function setPhotoLayoutAttribute(?PhotoLayoutType $aspect_ratio): void
{
$this->attributes['photo_layout'] = $aspect_ratio?->value;
}
/**
* Defines accessor for the Photo Timeline.
*/
protected function getPhotoTimelineAttribute(): ?TimelinePhotoGranularity
{
return TimelinePhotoGranularity::tryFrom($this->attributes['photo_timeline']);
}
/**
* Defines setter for Photo Timeline.
*/
protected function setPhotoTimelineAttribute(?TimelinePhotoGranularity $photo_timeline): void
{
$this->attributes['photo_timeline'] = $photo_timeline?->value;
}
}