Completed
Push — master ( 5939e9...7f76f6 )
by Gino
01:38
created

SeriesList::handleUrls()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Classes\ComponentAbstract;
9
use GinoPane\BlogTaxonomy\Classes\TranslateArrayTrait;
10
11
/**
12
 * Class SeriesList
13
 *
14
 * @package GinoPane\BlogTaxonomy\Components
15
 */
16
class SeriesList extends ComponentAbstract
17
{
18
    const NAME = 'seriesList';
19
20
    use TranslateArrayTrait;
21
22
    /**
23
     * @var Series
24
     */
25
    public $series;
26
27
    /**
28
     * Reference to the page name for linking to series
29
     *
30
     * @var string
31
     */
32
    public $seriesPage;
33
34
    /**
35
     * If the series list should be ordered by another attribute
36
     *
37
     * @var string
38
     */
39
    public $orderBy;
40
41
    /**
42
     * Whether display or not empty series
43
     *
44
     * @var bool
45
     */
46
    public $displayEmpty;
47
48
    /**
49
     * Limits the number of records to display
50
     *
51
     * @var int
52
     */
53
    public $limit;
54
55
    /**
56
     * Whether to query related posts or not
57
     *
58
     * @var string
59
     */
60
    private $fetchPosts;
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,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 componentDetails()
66
    {
67
        return [
68
            'name'        => Plugin::LOCALIZATION_KEY . 'components.series_list.name',
69
            'description' => Plugin::LOCALIZATION_KEY . 'components.series_list.description'
70
        ];
71
    }
72
73
    /**
74
     * @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|false>>.

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...
75
     */
76
    public function defineProperties()
77
    {
78
        return [
79
            'displayEmpty' => [
80
                'title'       =>    Plugin::LOCALIZATION_KEY . 'components.series_list.display_empty_title',
81
                'description' =>    Plugin::LOCALIZATION_KEY . 'components.series_list.display_empty_description',
82
                'type'        =>    'checkbox',
83
                'default'     =>    false,
84
                'showExternalParam' => false
85
            ],
86
            'fetchPosts' => [
87
                'title'             => Plugin::LOCALIZATION_KEY . 'components.series_list.fetch_posts_title',
88
                'description'       => Plugin::LOCALIZATION_KEY . 'components.series_list.fetch_posts_description',
89
                'type'              => 'checkbox',
90
                'default'           => false,
91
                'showExternalParam' => false
92
            ],
93
            'limit' => [
94
                'title'             => Plugin::LOCALIZATION_KEY . 'components.series_list.limit_title',
95
                'description'       => Plugin::LOCALIZATION_KEY . 'components.series_list.limit_description',
96
                'type'              => 'string',
97
                'default'           => '0',
98
                'validationPattern' => '^[0-9]+$',
99
                'validationMessage' => Plugin::LOCALIZATION_KEY . 'components.series_list.limit_validation_message',
100
                'showExternalParam' => false
101
            ],
102
            'orderBy' => [
103
                'title'       =>    Plugin::LOCALIZATION_KEY . 'components.series_list.order_title',
104
                'description' =>    Plugin::LOCALIZATION_KEY . 'components.series_list.order_description',
105
                'type'        =>    'dropdown',
106
                'default'     =>    'title asc',
107
                'showExternalParam' => false
108
            ],
109
            'seriesPage' => [
110
                'group'         =>  Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.links_group',
111
                'title'         =>  Plugin::LOCALIZATION_KEY . 'components.series_list.series_page_title',
112
                'description'   =>  Plugin::LOCALIZATION_KEY . 'components.series_list.series_page_description',
113
                'type'          =>  'dropdown',
114
                'default'       =>  'blog/series',
115
                'showExternalParam' => false
116
            ],
117
        ];
118
    }
119
120
    /**
121
     * @return mixed
122
     */
123
    public function getSeriesPageOptions()
124
    {
125
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
126
    }
127
128
    /**
129
     * @return string[]
130
     */
131
    public function getOrderByOptions()
132
    {
133
        $order = $this->translate(Series::$sortingOptions);
134
135
        asort($order);
136
137
        return $order;
138
    }
139
140
    /**
141
     * Prepare and return a series list
142
     *
143
     * @return mixed
144
     */
145
    public function onRun()
146
    {
147
        $this->seriesPage = $this->getProperty('seriesPage');
148
        $this->orderBy = $this->getProperty('orderBy');
149
        $this->displayEmpty = (bool) $this->getProperty('displayEmpty');
150
        $this->fetchPosts = (bool) $this->getProperty('fetchPosts');
0 ignored issues
show
Documentation Bug introduced by
The property $fetchPosts was declared of type string, but (bool) $this->getProperty('fetchPosts') is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
151
        $this->limit = $this->getProperty('limit');
152
153
        $this->series = $this->listSeries();
154
    }
155
156
    /**
157
     * Get Series
158
     *
159
     * @return mixed
160
     */
161
    protected function listSeries()
162
    {
163
        $series = Series::listFrontend([
164
            'sort' => $this->orderBy,
165
            'displayEmpty' => $this->displayEmpty,
166
            'limit' => $this->limit,
167
            'fetchPosts' => $this->fetchPosts
168
        ]);
169
170
        $this->handleUrls($series);
171
172
        return $series;
173
    }
174
175
    /**
176
     * @param $series
177
     */
178
    private function handleUrls($series)
179
    {
180
        $seriesComponent = $this->getComponent(SeriesPosts::NAME, $this->seriesPage);
181
182
        $this->setUrls(
183
            $series,
184
            $this->seriesPage,
185
            $this->controller,
186
            [
187
                'series' => $this->urlProperty($seriesComponent, 'series')
188
            ]
189
        );
190
    }
191
}
192