Completed
Push — 2.0 ( b61aac...db38c3 )
by Nicolas
12:38
created

EloquentPostRepository::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Modules\Blog\Repositories\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Modules\Blog\Entities\Post;
7
use Modules\Blog\Entities\Status;
8
use Modules\Blog\Events\PostIsCreating;
9
use Modules\Blog\Events\PostIsUpdating;
10
use Modules\Blog\Events\PostWasCreated;
11
use Modules\Blog\Events\PostWasDeleted;
12
use Modules\Blog\Events\PostWasUpdated;
13
use Modules\Blog\Repositories\Collection;
14
use Modules\Blog\Repositories\PostRepository;
15
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
16
17
class EloquentPostRepository extends EloquentBaseRepository implements PostRepository
18
{
19
    /**
20
     * @param  int    $id
21
     * @return object
22
     */
23
    public function find($id)
24
    {
25
        return $this->model->with('translations', 'tags')->find($id);
0 ignored issues
show
Bug introduced by
The method find does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
26
    }
27
28
    /**
29
     * @return \Illuminate\Database\Eloquent\Collection
30
     */
31
    public function all()
32
    {
33
        return $this->model->with('translations', 'tags')->orderBy('created_at', 'DESC')->get();
34
    }
35
36
    /**
37
     * Update a resource
38
     * @param $post
39
     * @param  array $data
40
     * @return mixed
41
     */
42 3
    public function update($post, $data)
43
    {
44 3
        event($event = new PostIsUpdating($post, $data));
45 3
        $post->update($event->getAttributes());
46
47 3
        $post->tags()->sync(array_get($data, 'tags', []));
48
49 3
        event(new PostWasUpdated($post, $data));
50
51 3
        return $post;
52
    }
53
54
    /**
55
     * Create a blog post
56
     * @param  array $data
57
     * @return Post
58
     */
59 9
    public function create($data)
60
    {
61 9
        event($event = new PostIsCreating($data));
62 9
        $post = $this->model->create($event->getAttributes());
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Database\Eloquent\Model. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
63
64 9
        $post->tags()->sync(array_get($data, 'tags', []));
65
66 9
        event(new PostWasCreated($post, $data));
67
68 9
        return $post;
69
    }
70
71
    public function destroy($model)
72
    {
73
        event(new PostWasDeleted($model->id, get_class($model)));
74
75
        return $model->delete();
76
    }
77
78
    /**
79
     * Return all resources in the given language
80
     *
81
     * @param  string                                   $lang
82
     * @return \Illuminate\Database\Eloquent\Collection
83
     */
84 1
    public function allTranslatedIn($lang)
85
    {
86
        return $this->model->whereHas('translations', function (Builder $q) use ($lang) {
87 1
            $q->where('locale', "$lang");
88 1
            $q->where('title', '!=', '');
89 1
        })->with('translations')->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'DESC')->get();
90
    }
91
92
    /**
93
     * Return the latest x blog posts
94
     * @param int $amount
95
     * @return Collection
96
     */
97
    public function latest($amount = 5)
98
    {
99
        return $this->model->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'desc')->take($amount)->get();
100
    }
101
102
    /**
103
     * Get the previous post of the given post
104
     * @param object $post
105
     * @return object
106
     */
107
    public function getPreviousOf($post)
108
    {
109
        return $this->model->where('created_at', '<', $post->created_at)
110
            ->whereStatus(Status::PUBLISHED)->orderBy('created_at', 'desc')->first();
111
    }
112
113
    /**
114
     * Get the next post of the given post
115
     * @param object $post
116
     * @return object
117
     */
118
    public function getNextOf($post)
119
    {
120
        return $this->model->where('created_at', '>', $post->created_at)
121
            ->whereStatus(Status::PUBLISHED)->first();
122
    }
123
124
    /**
125
     * Find a resource by the given slug
126
     *
127
     * @param  string $slug
128
     * @return object
129
     */
130
    public function findBySlug($slug)
131
    {
132
        return $this->model->whereHas('translations', function (Builder $q) use ($slug) {
133
            $q->where('slug', "$slug");
134
        })->with('translations')->whereStatus(Status::PUBLISHED)->firstOrFail();
135
    }
136
}
137