-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathUploadedPhotosRepository.php
65 lines (56 loc) · 1.5 KB
/
UploadedPhotosRepository.php
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
<?php
namespace WebDevEtc\BlogEtc\Repositories;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use WebDevEtc\BlogEtc\Exceptions\UploadedPhotoNotFoundException;
use WebDevEtc\BlogEtc\Models\UploadedPhoto;
class UploadedPhotosRepository
{
/**
* @var UploadedPhoto
*/
private $model;
/**
* Constructor.
*/
public function __construct(UploadedPhoto $model)
{
$this->model = $model;
}
/**
* Create a new Uploaded Photo row in the database.
*/
public function create(array $attributes): UploadedPhoto
{
return $this->query()->create($attributes);
}
/**
* Return new instance of the Query Builder for this model.
*/
public function query(): Builder
{
return $this->model->newQuery();
}
/**
* Delete a uploaded photo from the database.
*/
public function delete(int $uploadedPhotoID): ?bool
{
$uploadedPhoto = $this->find($uploadedPhotoID);
return $uploadedPhoto->delete();
}
/**
* Find a blog etc uploaded photo by ID.
*
* If cannot find, throw exception.
*/
public function find(int $uploadedPhotoID): UploadedPhoto
{
try {
return $this->query()->findOrFail($uploadedPhotoID);
} catch (ModelNotFoundException $e) {
throw new UploadedPhotoNotFoundException('Unable to find Uploaded Photo with ID: '.$uploadedPhotoID);
}
}
}