Completed
Push — master ( 85038d...dc924c )
by Gino
01:31
created

SeriesPosts::setPostUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Series;
8
use GinoPane\BlogTaxonomy\Classes\PostListAbstract;
9
10
/**
11
 * Class SeriesPosts
12
 *
13
 * @package GinoPane\BlogTaxonomy\Components
14
 */
15
class SeriesPosts extends PostListAbstract
16
{
17
    // Param name to be used in URLs: ":series"
18
    const URL_PARAM_NAME = 'series';
19
20
    const NAME = 'postsInSeries';
21
22
    /**
23
     * @var Series
24
     */
25
    public $series;
26
27
    /**
28
     * @return array
29
     */
30
    public function componentDetails(): array
31
    {
32
        return [
33
            'name'        => Plugin::LOCALIZATION_KEY . 'components.series_posts.name',
34
            'description' => Plugin::LOCALIZATION_KEY . 'components.series_posts.description'
35
        ];
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function defineProperties(): array
42
    {
43
        return [
44
                'series' => [
45
                    'title'       => Plugin::LOCALIZATION_KEY . 'components.series_posts.series_title',
46
                    'description' => Plugin::LOCALIZATION_KEY . 'components.series_posts.series_description',
47
                    'type'        => 'string',
48
                    'default'     => '{{ :series }}',
49
                ],
50
            ] + parent::defineProperties();
51
    }
52
53
    /**
54
     * @inheritDoc
55
     */
56
    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...
57
    {
58
        // load series
59
        $this->series = Series::whereTranslatable('slug', $this->property('series'))->first();
60
61
        return $this->series;
62
    }
63
64
    /**
65
     * @return mixed
66
     */
67
    protected function getPostsQuery()
68
    {
69
        $query = Post::whereHas('series', function ($query) {
70
            $query->whereTranslatable('slug', $this->series->slug);
71
        })->isPublished();
72
73
        return $query;
74
    }
75
76
    protected function setPostUrl(Post $post)
77
    {
78
        $post->setUrl(
79
            $this->postPage,
80
            $this->controller,
81
            [
82
                self::URL_PARAM_NAME => $this->series->slug
83
            ]
84
        );
85
    }
86
}
87