PostRepository   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 30
c 1
b 0
f 1
dl 0
loc 80
rs 10
wmc 14

9 Methods

Rating   Name   Duplication   Size   Complexity  
A syncTags() 0 10 3
A updatePost() 0 5 1
A editPost() 0 5 1
A indexPost() 0 9 3
A createPost() 0 5 1
A storePost() 0 5 1
A showPost() 0 3 1
A destroyPost() 0 4 1
A uploadImage() 0 6 2
1
<?php
2
3
namespace Adminetic\Website\Repositories;
4
5
use Adminetic\Website\Contracts\PostRepositoryInterface;
6
use Adminetic\Website\Http\Requests\PostRequest;
7
use Adminetic\Website\Models\Admin\Post;
8
use Adminetic\Website\Models\Admin\Tag;
9
use Illuminate\Support\Facades\Cache;
10
use Illuminate\Support\Str;
11
12
class PostRepository implements PostRepositoryInterface
13
{
14
    // Post Index
15
    public function indexPost()
16
    {
17
        $posts = config('adminetic.caching', true)
18
            ? (Cache::has('posts') ? Cache::get('posts') : Cache::rememberForever('posts', function () {
19
                return Post::orderBy('position')->get();
20
            }))
21
            : Post::orderBy('position')->get();
22
23
        return compact('posts');
24
    }
25
26
    // Post Create
27
    public function createPost()
28
    {
29
        $tags = Tag::latest()->get();
30
31
        return compact('tags');
32
    }
33
34
    // Post Store
35
    public function storePost(PostRequest $request)
36
    {
37
        $post = Post::create($request->validated());
38
        $this->syncTags($post);
39
        $this->uploadImage($post);
40
    }
41
42
    // Post Show
43
    public function showPost(Post $post)
44
    {
45
        return compact('post');
46
    }
47
48
    // Post Edit
49
    public function editPost(Post $post)
50
    {
51
        $tags = Tag::latest()->get();
52
53
        return compact('post', 'tags');
54
    }
55
56
    // Post Update
57
    public function updatePost(PostRequest $request, Post $post)
58
    {
59
        $post->update($request->validated());
60
        $this->syncTags($post);
61
        $this->uploadImage($post);
62
    }
63
64
    // Post Destroy
65
    public function destroyPost(Post $post)
66
    {
67
        $post->tags()->detach();
68
        $post->delete();
69
    }
70
71
    // Upload Image
72
    private function uploadImage(Post $post)
73
    {
74
        if (request()->has('image')) {
75
            $post
76
                ->addFromMediaLibraryRequest(request()->image)
77
                ->toMediaCollection('image');
78
        }
79
    }
80
81
    // Tags
82
    private function syncTags(Post $post)
83
    {
84
        if (request()->has('tags')) {
85
            $post->tags()->detach();
86
            foreach (request()->tags as $tag_name) {
87
                $tag = Tag::firstOrCreate([
88
                    'name' => trim($tag_name),
89
                    'slug' => Str::slug(trim($tag_name)),
90
                ]);
91
                $post->tags()->attach($tag->id);
92
            }
93
        }
94
    }
95
}
96