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

SeriesList::defineProperties()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 24

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
dl 31
loc 31
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Components;
4
5
use Cms\Classes\Page;
6
use Cms\Classes\ComponentBase;
7
use GinoPane\BlogTaxonomy\Plugin;
8
use GinoPane\BlogTaxonomy\Models\Series;
9
10
/**
11
 * Class SeriesList
12
 *
13
 * @package GinoPane\BlogTaxonomy\Components
14
 */
15 View Code Duplication
class SeriesList extends ComponentBase
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...
16
{
17
    use UrlHelperTrait;
18
19
    /**
20
     * @var Series
21
     */
22
    public $series;
23
24
    /**
25
     * Reference to the page name for linking to series
26
     *
27
     * @var string
28
     */
29
    public $seriesPage;
30
31
    /**
32
     * If the series list should be ordered by another attribute
33
     *
34
     * @var string
35
     */
36
    public $orderBy;
37
38
    /**
39
     * Whether display or not empty series
40
     *
41
     * @var bool
42
     */
43
    public $displayEmpty;
44
45
    /**
46
     * Limits the number of records to display
47
     *
48
     * @var int
49
     */
50
    public $limit;
51
52
    /**
53
     * @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...
54
     */
55
    public function componentDetails()
56
    {
57
        return [
58
            'name'        => Plugin::LOCALIZATION_KEY . 'components.series_list.name',
59
            'description' => Plugin::LOCALIZATION_KEY . 'components.series_list.description'
60
        ];
61
    }
62
63
    /**
64
     * @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...
65
     */
66
    public function defineProperties()
67
    {
68
        return [
69
            'displayEmpty' => [
70
                'title'       =>    Plugin::LOCALIZATION_KEY . 'components.series_list.display_empty_title',
71
                'description' =>    Plugin::LOCALIZATION_KEY . 'components.series_list.display_empty_description',
72
                'type'        =>    'checkbox',
73
                'default'     =>    false
74
            ],
75
            'seriesPage' => [
76
                'title'       =>    Plugin::LOCALIZATION_KEY . 'components.series_list.series_page_title',
77
                'description' =>    Plugin::LOCALIZATION_KEY . 'components.series_list.series_page_description',
78
                'type'        =>    'dropdown',
79
                'default'     =>    'blog/series'
80
            ],
81
            'orderBy' => [
82
                'title'       =>    Plugin::LOCALIZATION_KEY . 'components.series_list.order_title',
83
                'description' =>    Plugin::LOCALIZATION_KEY . 'components.series_list.order_description',
84
                'type'        =>    'dropdown',
85
                'default'     =>    'title asc'
86
            ],
87
            'limit' => [
88
                'title'             => Plugin::LOCALIZATION_KEY . 'components.series_list.limit_title',
89
                'description'       => Plugin::LOCALIZATION_KEY . 'components.series_list.limit_description',
90
                'type'              => 'string',
91
                'default'           => '0',
92
                'validationPattern' => '^[0-9]+$',
93
                'validationMessage' => Plugin::LOCALIZATION_KEY . 'components.series_list.validation_message'
94
            ],
95
        ];
96
    }
97
98
    /**
99
     * @return mixed
100
     */
101
    public function getSeriesPageOptions()
102
    {
103
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getOrderByOptions()
110
    {
111
        return Series::$sortingOptions;
112
    }
113
114
    /**
115
     * Prepare and return a series list
116
     *
117
     * @return mixed
118
     */
119
    public function onRun()
120
    {
121
        $this->seriesPage = $this->property('seriesPage', '');
122
        $this->orderBy = $this->property('orderBy', 'title asc');
123
        $this->displayEmpty = $this->property('displayEmpty', false);
124
        $this->limit = $this->property('limit', 0);
125
126
        $this->series = $this->listSeries();
127
    }
128
129
    /**
130
     * Get Series
131
     *
132
     * @return mixed
133
     */
134
    protected function listSeries()
135
    {
136
        $series = Series::listFrontend([
137
            'sort' => $this->orderBy,
138
            'displayEmpty' => $this->displayEmpty,
139
            'limit' => $this->limit
140
        ]);
141
142
        $this->setUrls($series, $this->seriesPage, $this->controller);
143
144
        return $series;
145
    }
146
}
147