Completed
Push — master ( 381b4d...e69b5e )
by Nicolas
02:32
created

EloquentPostRepository::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Modules\Blog\Repositories\Eloquent;
2
3
use Illuminate\Database\Eloquent\Builder;
4
use Modules\Blog\Entities\Post;
5
use Modules\Blog\Entities\Status;
6
use Modules\Blog\Events\PostWasCreated;
7
use Modules\Blog\Events\PostWasDeleted;
8
use Modules\Blog\Events\PostWasUpdated;
9
use Modules\Blog\Repositories\Collection;
10
use Modules\Blog\Repositories\PostRepository;
11
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
12
13
class EloquentPostRepository extends EloquentBaseRepository implements PostRepository
14
{
15
    /**
16
     * @param  int    $id
17
     * @return object
18
     */
19
    public function find($id)
20
    {
21
        return $this->model->with('translations', 'tags')->find($id);
22
    }
23
24
    /**
25
     * @return \Illuminate\Database\Eloquent\Collection
26
     */
27
    public function all()
28
    {
29
        return $this->model->with('translations', 'tags')->orderBy('created_at', 'DESC')->get();
30
    }
31
32
    /**
33
     * Update a resource
34
     * @param $post
35
     * @param  array $data
36
     * @return mixed
37
     */
38
    public function update($post, $data)
39
    {
40
        $post->update($data);
41
42
        $post->tags()->sync(array_get($data, 'tags', []));
43
44
        event(new PostWasUpdated($post->id, $data));
45
46
        return $post;
47
    }
48
49
    /**
50
     * Create a blog post
51
     * @param  array $data
52
     * @return Post
53
     */
54
    public function create($data)
55
    {
56
        $post = $this->model->create($data);
57
58
        $post->tags()->sync(array_get($data, 'tags', []));
59
60
        event(new PostWasCreated($post, $data));
61
62
        return $post;
63
    }
64
65
    public function destroy($model)
66
    {
67
        event(new PostWasDeleted($model->id, get_class($model)));
68
69
        return $model->delete();
70
    }
71
72
    /**
73
     * Return all resources in the given language
74
     *
75
     * @param  string                                   $lang
76
     * @return \Illuminate\Database\Eloquent\Collection
77
     */
78
    public function allTranslatedIn($lang)
79
    {
80
        return $this->model->whereHas('translations', function (Builder $q) use ($lang) {
81
            $q->where('locale', "$lang");
82
            $q->where('title', '!=', '');
83
        })->with('translations')->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'DESC')->get();
84
    }
85
86
    /**
87
     * Return the latest x blog posts
88
     * @param int $amount
89
     * @return Collection
90
     */
91
    public function latest($amount = 5)
92
    {
93
        return $this->model->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'desc')->take($amount)->get();
94
    }
95
96
    /**
97
     * Get the previous post of the given post
98
     * @param object $post
99
     * @return object
100
     */
101
    public function getPreviousOf($post)
102
    {
103
        return $this->model->where('created_at', '<', $post->created_at)
104
            ->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'desc')->first();
105
    }
106
107
    /**
108
     * Get the next post of the given post
109
     * @param object $post
110
     * @return object
111
     */
112
    public function getNextOf($post)
113
    {
114
        return $this->model->where('created_at', '>', $post->created_at)
115
            ->whereStatus(Status::PUBLISHED)->first();
116
    }
117
118
    /**
119
     * Find a resource by the given slug
120
     *
121
     * @param  string $slug
122
     * @return object
123
     */
124
    public function findBySlug($slug)
125
    {
126
        return $this->model->whereHas('translations', function (Builder $q) use ($slug) {
127
            $q->where('slug', "$slug");
128
        })->with('translations')->whereStatus(Status::PUBLISHED)->firstOrFail();
129
    }
130
}
131