Completed
Push — master ( e88dbf...166286 )
by Nicolas
02:37
created

EloquentPostRepository::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

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