Passed
Push — main ( 3cfdb8...a45320 )
by PRATIK
10:34
created

PostRepository::showPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Adminetic\Website\Repositories;
4
5
use Adminetic\Website\Models\Admin\Post;
6
use Illuminate\Support\Facades\Cache;
7
use Adminetic\Website\Models\Admin\Template;
8
use Adminetic\Website\Http\Requests\PostRequest;
9
use Adminetic\Category\Models\Admin\Category;
10
use Adminetic\Website\Contracts\PostRepositoryInterface;
11
12
13
14
class PostRepository implements PostRepositoryInterface
15
{
16
    // Post Index
17
    public function indexPost()
18
    {
19
        Cache::rememberForever('posts', function () {
20
            return Post::with('author')->latest()->get();
21
        });
22
        Cache::rememberForever('latest_limited_posts', function () {
23
            return Post::latestLimitedPosts()->get();
24
        });
25
        Cache::rememberForever('featured_limited_posts', function () {
26
            return Post::featuredLimitedPosts()->get();
27
        });
28
        Cache::rememberForever('limited_breaking_news', function () {
29
            return Post::limitedBreakingNews()->get();
30
        });
31
        Cache::rememberForever('limited_hot_news', function () {
32
            return Post::limitedHotNews()->get();
33
        });
34
        Cache::rememberForever('limited_trending_posts', function () {
35
            return Post::trending();
36
        });
37
        Cache::rememberForever('limited_priority_posts', function () {
38
            return Post::limitedPriorityPosts()->get();
39
        });
40
        Cache::rememberForever('yesterday_most_visited_posts', function () {
41
            return Post::yesterdayMostVisitedPosts()->get();
42
        });
43
        Cache::rememberForever('week_most_visited_posts', function () {
44
            return Post::weekMostVisitedPosts()->get();
45
        });
46
        Cache::rememberForever('most_visited_posts_chunked', function () {
47
            return Post::mostVisitedPostsChunked();
48
        });
49
        return [];
50
    }
51
52
    // Post Create
53
    public function createPost()
54
    {
55
        $categories = Cache::get('categories', Category::with('parent', 'categories')->latest()->get());
56
        $tags = Post::existingTags()->pluck('name');
57
        $templates = Cache::get('templates', Template::latest()->get());
58
        return compact('categories', 'tags', 'templates');
59
    }
60
61
    // Post Store
62
    public function storePost(PostRequest $request)
63
    {
64
        $post = Post::create($request->validated());
65
        if (request()->tags) {
66
            $post->tag(array_unique(request()->tags));
67
        }
68
        $request->image ? $this->uploadImage($post) : '';
69
    }
70
71
    // Post Show
72
    public function showPost(Post $post)
73
    {
74
        $tags = $post->tagged->pluck('tag_name');
75
        return compact('post', 'tags');
76
    }
77
78
    // Post Edit
79
    public function editPost(Post $post)
80
    {
81
        $categories = Cache::get('categories', Category::with('parent', 'categories')->latest()->get());
82
        $tags = $post->existingTags()->pluck('name');
83
        $remove_tags = $post->tagged->pluck('tag_name');
84
        $templates = Cache::get('templates', Template::latest()->get());
85
        return compact('post', 'categories', 'tags', 'remove_tags', 'templates');
86
    }
87
88
    // Post Update
89
    public function updatePost(PostRequest $request, Post $post)
90
    {
91
        $post->update($request->validated());
92
        if (request()->remove_tags) {
93
            $post->untag(request()->remove_tags);
94
        }
95
        if (request()->tags) {
96
            $post->tag(array_unique(request()->tags));
97
        }
98
        $request->image ? $this->uploadImage($post) : '';
99
    }
100
101
    // Post Destroy
102
    public function destroyPost(Post $post)
103
    {
104
        $post->image ? $post->hardDelete('image') : '';
105
        $post->delete();
106
    }
107
108
    // Upload Image
109
    protected function uploadImage(Post $post)
110
    {
111
        if (request()->image) {
112
            $thumbnails = [
113
                'storage' => 'website/post/' . validImageFolder($post->id, 'post'),
0 ignored issues
show
Bug introduced by
The function validImageFolder was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
                'storage' => 'website/post/' . /** @scrutinizer ignore-call */ validImageFolder($post->id, 'post'),
Loading history...
114
                'width' => '1200',
115
                'height' => '630',
116
                'quality' => '100',
117
                'thumbnails' => [
118
                    [
119
                        'thumbnail-name' => 'medium',
120
                        'thumbnail-width' => '730',
121
                        'thumbnail-height' => '500',
122
                        'thumbnail-quality' => '90'
123
                    ],
124
                    [
125
                        'thumbnail-name' => 'small',
126
                        'thumbnail-width' => '80',
127
                        'thumbnail-height' => '70',
128
                        'thumbnail-quality' => '70'
129
                    ]
130
                ]
131
            ];
132
            $post->makeThumbnail('image', $thumbnails);
133
        }
134
    }
135
}
136