1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use App\Models\Post; |
6
|
|
|
use App\Models\Tag; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
|
9
|
|
|
class PostProcesses |
10
|
|
|
{ |
11
|
|
|
protected $tag; |
12
|
|
|
protected $postsData; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Create a new job instance. |
16
|
|
|
* |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
|
|
public function __construct($tag) |
20
|
|
|
{ |
21
|
|
|
$this->tag = $tag; |
22
|
|
|
$this->handle(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Execute the job. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function handle() |
31
|
|
|
{ |
32
|
|
|
if ($this->tag) { |
33
|
|
|
$this->postsData = $this->tagIndexData($this->tag); |
34
|
|
|
|
35
|
|
|
return $this->tagIndexData($this->tag); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->postsData = $this->normalIndexData(); |
39
|
|
|
|
40
|
|
|
return $this->normalIndexData(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets the response. |
45
|
|
|
* |
46
|
|
|
* @return private postsData[] |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function getResponse() |
49
|
|
|
{ |
50
|
|
|
return $this->postsData; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return data for normal index page. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
protected function normalIndexData() |
59
|
|
|
{ |
60
|
|
|
$posts = Post::allPublishedPosts()->simplePaginate(config('blog.posts_per_page')); |
61
|
|
|
|
62
|
|
|
return [ |
63
|
|
|
'title' => config('blog.title'), |
64
|
|
|
'subtitle' => config('blog.subtitle'), |
65
|
|
|
'posts' => $posts, |
66
|
|
|
'post_image' => config('blog.home_page_image'), |
67
|
|
|
'meta_description' => config('blog.description'), |
68
|
|
|
'reverse_direction' => config('blog.reverse_pagination_direction'), |
69
|
|
|
'tag' => null, |
70
|
|
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Return data for a tag index page. |
75
|
|
|
* |
76
|
|
|
* @param string $tag |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
protected function tagIndexData($tag) |
81
|
|
|
{ |
82
|
|
|
$tag = Tag::where('tag', $tag)->firstOrFail(); |
83
|
|
|
$reverse_direction = (bool) $tag->reverse_direction; |
84
|
|
|
|
85
|
|
|
$posts = Post::where('published_at', '<=', Carbon::now()) |
86
|
|
|
->whereHas('tags', function ($q) use ($tag) { |
87
|
|
|
$q->where('tag', '=', $tag->tag); |
88
|
|
|
}) |
89
|
|
|
->where('is_draft', 0) |
90
|
|
|
->orderBy('published_at', $reverse_direction ? 'asc' : 'desc') |
91
|
|
|
->simplePaginate(99999999999999); // No limit in theory |
92
|
|
|
|
93
|
|
|
$posts->appends('tag', $tag->tag); |
94
|
|
|
|
95
|
|
|
$post_image = $tag->post_image ?: config('blog.post_image'); |
96
|
|
|
|
97
|
|
|
return [ |
98
|
|
|
'title' => $tag->title, |
99
|
|
|
'subtitle' => $tag->subtitle, |
100
|
|
|
'posts' => $posts, |
101
|
|
|
'post_image' => $post_image, |
102
|
|
|
'tag' => $tag, |
103
|
|
|
'reverse_direction' => $reverse_direction, |
104
|
|
|
'meta_description' => $tag->meta_description ?: \ config('blog.description'), |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|