-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathPostsRepository.php
181 lines (159 loc) · 4.81 KB
/
PostsRepository.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
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
<?php
namespace WebDevEtc\BlogEtc\Repositories;
use Exception;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use WebDevEtc\BlogEtc\Exceptions\PostNotFoundException;
use WebDevEtc\BlogEtc\Models\Post;
/**
* Class BlogEtcPostsRepository.
*/
class PostsRepository
{
/**
* @var Post
*/
private $model;
/**
* BlogEtcPostsRepository constructor.
*/
public function __construct(Post $model)
{
$this->model = $model;
}
/**
* Return blog posts ordered by posted_at, paginated.
*
* @param int $categoryID
*/
public function indexPaginated(int $perPage = 10, int $categoryID = null): LengthAwarePaginator
{
$query = $this->query(true)
->orderBy('posted_at', 'desc');
if ($categoryID > 0) {
$query->whereHas('categories', static function (Builder $query) use ($categoryID) {
$query->where('blog_etc_post_categories.blog_etc_category_id', $categoryID);
})->get();
}
return $query->paginate($perPage);
}
/**
* Return new instance of the Query Builder for this model.
*/
public function query(bool $eagerLoad = false): Builder
{
$queryBuilder = $this->model->newQuery();
if (true === $eagerLoad) {
$queryBuilder->with(['categories']);
}
return $queryBuilder;
}
/**
* Return posts for RSS feed.
*
* @return Builder[]|Collection
*/
public function rssItems(): Collection
{
return $this->query(false)
->orderBy('posted_at', 'desc')
->limit(config('blogetc.rssfeed.posts_to_show_in_rss_feed'))
->with('author')
->get();
}
/**
* Find a blog etc post by slug
* If cannot find, throw exception.
*/
public function findBySlug(string $slug): Post
{
try {
// the published_at + is_published are handled by BlogEtcPublishedScope, and don't take effect if the
// logged in user can manage log posts
return $this->query(true)
->where('slug', $slug)
->firstOrFail();
} catch (ModelNotFoundException $e) {
throw new PostNotFoundException('Unable to find blog post with slug: '.$slug);
}
}
/**
* Find a blog etc post by ID
* If cannot find, throw exception.
*/
public function findById(int $id): Post
{
try {
// the published_at + is_published are handled by BlogEtcPublishedScope, and don't take effect if the
// logged in user can manage log posts
return $this->query(true)
->where('id', $id)
->firstOrFail();
} catch (ModelNotFoundException $e) {
throw new PostNotFoundException('Unable to find blog post with id: '.$id);
}
}
/**
* Create a new BlogEtcPost post.
*/
public function create(array $attributes): Post
{
return $this->query()->create($attributes);
}
/**
* Delete a post.
*
* @throws Exception
*/
public function delete(int $postID): bool
{
$post = $this->find($postID);
return (bool) $post->delete();
}
/**
* Find a blog etc post by ID
* If cannot find, throw exception.
*/
public function find(int $blogEtcPostID): Post
{
try {
return $this->query(true)->findOrFail($blogEtcPostID);
} catch (ModelNotFoundException $e) {
throw new PostNotFoundException('Unable to find blog post with ID: '.$blogEtcPostID);
}
}
/**
* Update image sizes (or in theory any attribute) on a blog etc post.
*
* TODO - currently untested.
*
* @param array $uploadedImages
*/
public function updateImageSizes(Post $post, ?array $uploadedImages): Post
{
if (!empty($uploadedImages)) {
// does not use update() here as it would require fillable for each field - and in theory someone
// might want to add more image sizes.
foreach ($uploadedImages as $size => $imageName) {
$post->$size = $imageName;
}
$post->save();
}
return $post;
}
/**
* Search for posts.
*
* This is a rough implementation - proper full text search has been removed in current version.
*/
public function search(string $search, int $max = 25): Collection
{
$query = $this->query(true)->limit($max);
trim($search)
? $query->where('title', 'like', '%'.$search)
: $query->where('title', '');
return $query->get();
}
}