WebDevEtc /
BlogEtc
| 1 | <?php |
||
| 2 | |||
| 3 | namespace WebDevEtc\BlogEtc\Services; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
||
| 7 | use Illuminate\Database\Eloquent\Collection; |
||
| 8 | use LogicException; |
||
| 9 | use WebDevEtc\BlogEtc\Models\Post; |
||
| 10 | use WebDevEtc\BlogEtc\Repositories\PostsRepository; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Class BlogEtcPostsService. |
||
| 14 | * |
||
| 15 | * Service class to handle most logic relating to BlogEtcPosts. |
||
| 16 | * |
||
| 17 | * Some Eloquent/DB things are in here - but query heavy method belong in the repository, accessible |
||
| 18 | * as $this->repository, or publicly via repository() |
||
| 19 | */ |
||
| 20 | class PostsService |
||
| 21 | { |
||
| 22 | /** @var PostsRepository */ |
||
| 23 | private $repository; |
||
| 24 | |||
| 25 | /** @var UploadsService */ |
||
| 26 | private $uploadsService; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * PostsService constructor. |
||
| 30 | */ |
||
| 31 | public function __construct(PostsRepository $repository, UploadsService $uploadsService) |
||
| 32 | { |
||
| 33 | $this->repository = $repository; |
||
| 34 | $this->uploadsService = $uploadsService; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * BlogEtcPosts repository - for query heavy method. |
||
| 39 | * |
||
| 40 | * I don't stick 100% to all queries belonging in the repo - some Eloquent |
||
| 41 | * things are fine to have in the service where it makes sense. |
||
| 42 | */ |
||
| 43 | public function repository(): PostsRepository |
||
| 44 | { |
||
| 45 | return $this->repository; |
||
| 46 | } |
||
| 47 | |||
| 48 | // |
||
| 49 | // /** |
||
| 50 | // * Create a new BlogEtcPost entry, and process any uploaded image. |
||
| 51 | // * |
||
| 52 | // * (I'm never keen on passing around entire Request objects - this will get |
||
| 53 | // * refactored out) |
||
| 54 | // * |
||
| 55 | // * @throws Exception |
||
| 56 | // */ |
||
| 57 | // public function create(PostRequest $request, ?int $userID): Post |
||
| 58 | // { |
||
| 59 | // throw new LogicException('PostsService create is not yet ready for use'); |
||
| 60 | // $attributes = $request->validated() + ['user_id' => $userID]; |
||
| 61 | // |
||
| 62 | // // set default posted_at, if none were submitted |
||
| 63 | // if (empty($attributes['posted_at'])) { |
||
| 64 | // $attributes['posted_at'] = Carbon::now(); |
||
| 65 | // } |
||
| 66 | // |
||
| 67 | // $newBlogPost = $this->repository->create($request->validated()); |
||
| 68 | // |
||
| 69 | // if (config('blogetc.image_upload_enabled')) { |
||
| 70 | // $uploadedImages = $this->uploadsService->processFeaturedUpload($request, $newBlogPost); |
||
| 71 | // $this->repository->updateImageSizes($newBlogPost, $uploadedImages); |
||
| 72 | // } |
||
| 73 | // |
||
| 74 | // if (count($request->categories())) { |
||
| 75 | // $newBlogPost->categories()->sync($request->categories()); |
||
| 76 | // } |
||
| 77 | // |
||
| 78 | // event(new BlogPostAdded($newBlogPost)); |
||
| 79 | // |
||
| 80 | // return $newBlogPost; |
||
| 81 | // } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Return all results, paginated. |
||
| 85 | * |
||
| 86 | * @param int $perPage |
||
| 87 | */ |
||
| 88 | public function indexPaginated($perPage = 10, int $categoryID = null): LengthAwarePaginator |
||
| 89 | { |
||
| 90 | return $this->repository->indexPaginated($perPage, $categoryID); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Return posts for rss feed. |
||
| 95 | * |
||
| 96 | * @return Collection|Post[] |
||
| 97 | */ |
||
| 98 | public function rssItems(): Collection |
||
| 99 | { |
||
| 100 | return $this->repository->rssItems(); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Update a BlogEtcPost with new attributes and/or images. |
||
| 105 | * |
||
| 106 | * N.B. I dislike sending the whole Request object around, this will get refactored. |
||
| 107 | * |
||
| 108 | * Does not currently use repo calls - works direct on Eloquent. This will change. |
||
| 109 | * |
||
| 110 | * @throws Exception |
||
| 111 | */ |
||
| 112 | public function update(/** @scrutinizer ignore-unused */ int $blogPostID, /*PostRequest*/ /** @scrutinizer ignore-unused */ $request): Post |
||
| 113 | { |
||
| 114 | throw new LogicException('PostsService update is not yet ready for use'); |
||
| 115 | // $post = $this->repository->find($blogPostID); |
||
| 116 | // |
||
| 117 | // // TODO - split this into a repo call. |
||
| 118 | // $post->fill($request->validated()); |
||
| 119 | // |
||
| 120 | // // TODO - copy logic from create! this is now wrong |
||
| 121 | // $this->uploadsService->processFeaturedUpload($request, $post); |
||
| 122 | // |
||
| 123 | // $post->save(); |
||
| 124 | // |
||
| 125 | // $post->categories()->sync($request->categories()); |
||
| 126 | // |
||
| 127 | // event(new BlogPostEdited($post)); |
||
| 128 | // |
||
| 129 | // return $post; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Delete a blog etc post, return the deleted post and an array of featured images which were associated |
||
| 134 | * to the blog post (but which were not deleted form the filesystem). |
||
| 135 | * |
||
| 136 | * @throws Exception |
||
| 137 | * |
||
| 138 | * @return array - [Post (deleted post), array (remaining featured photos) |
||
| 139 | */ |
||
| 140 | public function delete(int $postID): array |
||
|
0 ignored issues
–
show
|
|||
| 141 | { |
||
| 142 | throw new LogicException('PostsService delete is not yet ready for use'); |
||
| 143 | // $post = $this->repository->find($postID); |
||
| 144 | // |
||
| 145 | // event(new BlogPostWillBeDeleted($post)); |
||
| 146 | // |
||
| 147 | // $this->repository->delete($postID); |
||
| 148 | // |
||
| 149 | // $remainingPhotos = []; |
||
| 150 | // |
||
| 151 | // foreach ((array) config('blogetc.image_sizes') as $imageSize => $imageSizeInfo) { |
||
| 152 | // if ($post->$imageSize) { |
||
| 153 | // $fullPath = config('blogetc.blog_upload_dir', 'blog_images').'/'.$post->$imageSize; |
||
| 154 | // |
||
| 155 | // $fileSize = UploadsService::disk()->getSize($fullPath); |
||
| 156 | // |
||
| 157 | // if (false !== $fileSize) { |
||
| 158 | // $fileSize = $this->humanReadableFileSize($fileSize); |
||
| 159 | // } |
||
| 160 | // |
||
| 161 | // $remainingPhotos[] = [ |
||
| 162 | // 'filename' => $post->$imageSize, |
||
| 163 | // 'full_path' => $fullPath, |
||
| 164 | // 'file_size' => $fileSize, |
||
| 165 | // 'url' => UploadsService::disk()->url($fullPath), |
||
| 166 | // ]; |
||
| 167 | // } |
||
| 168 | // } |
||
| 169 | // |
||
| 170 | // return [$post, $remainingPhotos]; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get human readable file size (in kb). |
||
| 175 | */ |
||
| 176 | protected function humanReadableFileSize(int $fileSize): string |
||
| 177 | { |
||
| 178 | return round($fileSize / 1000, 1).' kb'; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Find and return a blog post based on slug. |
||
| 183 | */ |
||
| 184 | public function findBySlug(string $slug): Post |
||
| 185 | { |
||
| 186 | return $this->repository->findBySlug($slug); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Find and return a blog post based on id. |
||
| 191 | */ |
||
| 192 | public function findById(int $id): Post |
||
| 193 | { |
||
| 194 | return $this->repository->findById($id); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Search for posts. |
||
| 199 | */ |
||
| 200 | public function search(string $query, $max = 25): Collection |
||
| 201 | { |
||
| 202 | return $this->repository->search($query, $max); |
||
| 203 | } |
||
| 204 | |||
| 205 | public function clearImageSizes(Post $post, array $sizes) |
||
| 206 | { |
||
| 207 | foreach ($sizes as $size) { |
||
| 208 | $post->$size = null; |
||
| 209 | } |
||
| 210 | |||
| 211 | $post->save(); |
||
| 212 | } |
||
| 213 | } |
||
| 214 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.