SeriesNavigation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 135
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A componentDetails() 0 7 1
A defineProperties() 0 25 1
A getPostPageOptions() 0 4 1
A getSeriesPageOptions() 0 4 1
A onRun() 0 7 1
A listSeries() 0 27 2
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Components;
4
5
use Cms\Classes\Page;
6
use GinoPane\BlogTaxonomy\Plugin;
7
use GinoPane\BlogTaxonomy\Models\Series;
8
use GinoPane\BlogTaxonomy\Models\ModelAbstract;
9
use GinoPane\BlogTaxonomy\Classes\ComponentAbstract;
10
11
/**
12
 * Class SeriesNavigation
13
 *
14
 * @package GinoPane\BlogTaxonomy\Components
15
 */
16
class SeriesNavigation extends ComponentAbstract
17
{
18
    const NAME = 'seriesNavigation';
19
20
    /**
21
     * @var Series
22
     */
23
    public $series;
24
25
    /**
26
     * Post slug
27
     *
28
     * @var string
29
     */
30
    public $slug;
31
32
    /**
33
     * @var int
34
     */
35
    public $smallNav;
36
37
    /**
38
     * Reference to the page name for linking to posts
39
     *
40
     * @var string
41
     */
42
    public $postPage;
43
44
    /**
45
     * Reference to the page name for linking to series
46
     *
47
     * @var string
48
     */
49
    public $seriesPage;
50
51
    /**
52
     * @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...
53
     */
54
    public function componentDetails()
55
    {
56
        return [
57
            'name'        => Plugin::LOCALIZATION_KEY . 'components.series_navigation.name',
58
            'description' => Plugin::LOCALIZATION_KEY . 'components.series_navigation.description'
59
        ];
60
    }
61
62
    /**
63
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,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...
64
     */
65
    public function defineProperties()
66
    {
67
        return [
68
            'slug' => [
69
                'title'       =>  Plugin::LOCALIZATION_KEY . 'components.series_navigation.post_slug_title',
70
                'description' =>  Plugin::LOCALIZATION_KEY . 'components.series_navigation.post_slug_description',
71
                'default'     => '{{ :slug }}',
72
                'type'        => 'string'
73
            ],
74
            'seriesPage' => [
75
                'title'       => Plugin::LOCALIZATION_KEY . 'components.series_navigation.series_page_title',
76
                'description' => Plugin::LOCALIZATION_KEY . 'components.series_navigation.series_page_description',
77
                'type'        => 'dropdown',
78
                'default'     => 'blog/series',
79
                'group'       => Plugin::LOCALIZATION_KEY . 'components.series_navigation.links_group',
80
            ],
81
            'postPage' => [
82
                'title'       => 'rainlab.blog::lang.settings.posts_post',
83
                'description' => 'rainlab.blog::lang.settings.posts_post_description',
84
                'type'        => 'dropdown',
85
                'default'     => 'blog/post',
86
                'group'       => Plugin::LOCALIZATION_KEY . 'components.series_navigation.links_group',
87
            ]
88
        ];
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94
    public function getPostPageOptions()
95
    {
96
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    public function getSeriesPageOptions()
103
    {
104
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
105
    }
106
107
    /**
108
     * @return void
109
     */
110
    public function onRun()
111
    {
112
        $this->postPage   = $this->property('postPage');
113
        $this->seriesPage = $this->property('seriesPage');
114
        $this->slug       = $this->page['slug'] = $this->property('slug');
115
        $this->series     = $this->page['series'] = $this->listSeries();
116
    }
117
118
    /**
119
     * Get Series
120
     *
121
     * @return mixed
122
     */
123
    protected function listSeries()
124
    {
125
        $series = Series::whereHas(
126
            'posts',
127
            function($query) {
128
                ModelAbstract::whereTranslatableProperty($query, 'slug', $this->property('slug'));
129
            }
130
        )->with(
131
            [
132
                'posts' => static function($query) {
133
                    $query->isPublished();
134
                }
135
            ]
136
        )->first();
137
138
        if ($series !== null) {
139
            $seriesComponent = $this->getComponent(SeriesPosts::NAME, $this->seriesPage);
140
141
            $series->setUrl($this->seriesPage, $this->controller, [
142
                'series' => $this->urlProperty($seriesComponent, 'series')
143
            ]);
144
145
            $this->setPostUrls($series->posts);
146
        }
147
148
        return $series;
149
    }
150
}
151