PostsRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 78
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 4 1
A getTranslatableModel() 0 4 1
A getPublic() 0 8 1
A getPopularPublic() 0 11 1
A findBySlug() 0 10 1
A incrementViewCount() 0 4 1
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Post;
6
use App\PostTranslation;
7
8
class PostsRepository extends Repository
9
{
10
    /**
11
     * @return Post
12
     */
13
    public function getModel()
14
    {
15
        return new Post();
16
    }
17
18
    /**
19
     * @return PostTranslation
20
     */
21
    public function getTranslatableModel()
22
    {
23
        return new PostTranslation();
24
    }
25
26
    /**
27
     * Get public posts.
28
     *
29
     * @param $perPage
30
     * @return \Illuminate\Database\Eloquent\Collection
31
     */
32
    public function getPublic($perPage = 7)
33
    {
34
        return self::getModel()
0 ignored issues
show
Bug introduced by
The method published() does not exist on App\Post. Did you maybe mean scopePublished()?

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...
35
            ->published()
36
            ->active()
37
            ->orderBy('id', self::DESC)
38
            ->paginate($perPage);
39
    }
40
41
    /**
42
     * Get popular public posts.
43
     *
44
     * @param int $count
45
     * @return \Illuminate\Database\Eloquent\Collection
46
     */
47
    public function getPopularPublic($count = 4)
48
    {
49
        // todo: popular featured posts.
50
       return $this->getModel()
0 ignored issues
show
Bug introduced by
The method published() does not exist on App\Post. Did you maybe mean scopePublished()?

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...
51
           ->published()
52
           ->active()
53
           ->orderBy('view_count', self::DESC)
54
           ->orderBy('id', self::DESC)
55
           ->take($count)
56
           ->get();
57
    }
58
59
    /**
60
     * Get post by slug.
61
     *
62
     * @param $slug
63
     * @return mixed
64
     */
65
    public function findBySlug($slug)
66
    {
67
        return self::getModel()
0 ignored issues
show
Documentation Bug introduced by
The method select does not exist on object<App\Post>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
68
            ->select('*')
69
            ->translated()
70
            ->whereSlug($slug)
71
            ->published()
72
            ->active()
73
            ->first();
74
    }
75
76
    /**
77
     * Increment view_counter by 1.
78
     *
79
     * @param Post $post
80
     */
81
    public function incrementViewCount($post)
82
    {
83
        $post->increment('view_count');
0 ignored issues
show
Bug introduced by
The method increment() cannot be called from this context as it is declared protected in class Illuminate\Database\Eloquent\Model.

This check looks for access to methods that are not accessible from the current context.

If you need to make a method accessible to another context you can raise its visibility level in the defining class.

Loading history...
84
    }
85
}