Completed
Push — master ( a449a7...7f0061 )
by Gino
12:11
created

TagPosts::onRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Components;
4
5
use Rainlab\Blog\Models\Post;
6
use GinoPane\BlogTaxonomy\Plugin;
7
use GinoPane\BlogTaxonomy\Models\Tag;
8
9
/**
10
 * Class TagPosts
11
 *
12
 * @package GinoPane\BlogTaxonomy\Components
13
 */
14 View Code Duplication
class TagPosts extends PostListAbstract
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var Tag
18
     */
19
    public $tag;
20
21
    /**
22
     * Component Registration
23
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
24
     */
25
    public function componentDetails()
26
    {
27
        return [
28
            'name'        => Plugin::LOCALIZATION_KEY . 'components.tag_posts.name',
29
            'description' => Plugin::LOCALIZATION_KEY . 'components.tag_posts.description'
30
        ];
31
    }
32
33
    /**
34
     * Component Properties
35
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
36
     */
37
    public function defineProperties()
38
    {
39
        $properties = [
40
            'tag' => [
41
                'title'         => 'Tag',
42
                'description'   => 'The URL parameter used to search for posts',
43
                'default'       => '{{ :tag }}',
44
                'type'          => 'string'
45
            ]
46
        ] + parent::defineProperties();
47
48
        return $properties;
49
    }
50
51
    /**
52
     * Prepare variables
53
     */
54
    protected function prepareContextItem()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
55
    {
56
        // load tag
57
        $this->tag = Tag::where('slug', $this->property('tag'))->first();
58
59
        return $this->tag;
60
    }
61
62
    /**
63
     * @return mixed
64
     */
65
    protected function getPostsQuery()
66
    {
67
        $query = Post::whereHas('tags', function ($query) {
68
            $query->where('slug', $this->tag->slug);
69
        })->isPublished();
70
71
        return $query;
72
    }
73
}
74