Completed
Push — master ( 883ebd...28a518 )
by Gino
03:07 queued 01:32
created

PostListAbstract::getExceptionProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A PostListAbstract::handleOrder() 0 12 3
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Classes;
4
5
use Cms\Classes\Page;
6
use Illuminate\Http\Response;
7
use Rainlab\Blog\Models\Post;
8
use GinoPane\BlogTaxonomy\Plugin;
9
use October\Rain\Database\Builder;
10
use Illuminate\Http\RedirectResponse;
11
use Illuminate\Support\Facades\Redirect;
12
use Illuminate\Database\Eloquent\Collection;
13
14
/**
15
 * Class PostListAbstract
16
 *
17
 * @package GinoPane\BlogTaxonomy\Classes
18
 */
19
abstract class PostListAbstract extends ComponentAbstract
0 ignored issues
show
Coding Style introduced by
PostListAbstract does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
20
{
21
    use TranslateArrayTrait;
22
    use PostListExceptionsTrait;
23
24
    /**
25
     * @var Collection | array
26
     */
27
    public $posts = [];
28
29
    /**
30
     * @var integer             The current page
31
     */
32
    public $currentPage;
33
34
    /**
35
     * @var integer             The number of results per page
36
     */
37
    public $resultsPerPage;
38
39
    /**
40
     * If the post list should be ordered by another attribute
41
     *
42
     * @var string
43
     */
44
    public $orderBy;
45
46
    /**
47
     * The attributes on which the post list can be ordered
48
     * @var array
49
     */
50
    public static $postAllowedSortingOptions = [
51
        'title asc' => Plugin::LOCALIZATION_KEY . 'order_options.title_asc',
52
        'title desc' => Plugin::LOCALIZATION_KEY . 'order_options.title_desc',
53
        'created_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_asc',
54
        'created_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.created_at_desc',
55
        'updated_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.updated_at_asc',
56
        'updated_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.updated_at_desc',
57
        'published_at asc' => Plugin::LOCALIZATION_KEY . 'order_options.published_at_asc',
58
        'published_at desc' => Plugin::LOCALIZATION_KEY . 'order_options.published_at_desc',
59
        'random' => Plugin::LOCALIZATION_KEY . 'order_options.random'
60
    ];
61
62
    /**
63
     * Component properties
64
     *
65
     * @return array
66
     */
67
    public function defineProperties(): array
68
    {
69
        $properties = [
70
            'orderBy' => [
71
                'title'       => 'rainlab.blog::lang.settings.posts_order',
72
                'description' => 'rainlab.blog::lang.settings.posts_order_description',
73
                'type'        => 'dropdown',
74
                'default'     => 'published_at asc',
75
                'showExternalParam' => false
76
            ]
77
        ];
78
79
        return array_merge(
80
            $properties,
81
            $this->getPaginationProperties(),
82
            $this->getPageLinkProperties(),
83
            $this->getPostExceptionProperties()
84
        );
85
    }
86
87
    /**
88
     * @see Post::$allowedSortingOptions
89
     *
90
     * @return string[]
91
     */
92
    public function getOrderByOptions(): array
93
    {
94
        $order = $this->translate(static::$postAllowedSortingOptions);
95
96
        asort($order);
97
98
        return $order;
99
    }
100
101
    /**
102
     * Query the item and posts belonging to it
103
     *
104
     * @return void|RedirectResponse
105
     */
106
    public function onRun()
107
    {
108
        if ($this->prepareContextItem() === null) {
109
            return Redirect::to($this->controller->pageUrl(Response::HTTP_NOT_FOUND));
110
        }
111
112
        $this->prepareVars();
113
114
        $this->listPosts();
115
    }
116
117
    /**
118
     * Load a list of posts
119
     */
120
    public function listPosts()
121
    {
122
        $query = $this->getPostsQuery();
123
124
        $this->handlePostExceptions($query);
125
126
        $this->handleOrder($query);
127
128
        $posts = $query->paginate($this->resultsPerPage, $this->currentPage);
129
130
        $this->setPostUrls($posts);
131
132
        $this->posts = $posts;
133
    }
134
135
    /**
136
     * @return mixed
137
     */
138
    public function getPostPageOptions()
139
    {
140
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
141
    }
142
143
    /**
144
     * @return mixed
145
     */
146
    public function getCategoryPageOptions()
147
    {
148
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
149
    }
150
151
    /**
152
     * Prepare variables
153
     */
154
    abstract protected function prepareContextItem();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
155
156
    /**
157
     * @return mixed
158
     */
159
    abstract protected function getPostsQuery();
160
161
    /**
162
     * Prepare variables
163
     */
164
    protected function prepareVars()
165
    {
166
        // Paginator settings
167
        $this->populatePagination();
168
        // Page links
169
        $this->populateLinks();
170
        // Exceptions
171
        $this->populateExceptions();
172
173
        $this->orderBy = $this->property('orderBy');
174
    }
175
176
    /**
177
     * Properties for pagination handling
178
     *
179
     * @return array
180
     */
181
    private function getPaginationProperties(): array
182
    {
183
        return [
184
            'page' => [
185
                'group'         => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_group',
186
                'title'         => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.page_parameter_title',
187
                'description'   => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.page_parameter_description',
188
                'default'       => '{{ :page }}',
189
                'type'          => 'string',
190
            ],
191
            'resultsPerPage' => [
192
                'group'         => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_group',
193
                'title'         => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_per_page_title',
194
                'description'   => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_per_page_description',
195
                'default'       => 10,
196
                'type'          => 'string',
197
                'validationPattern' => '^(0+)?[1-9]\d*$',
198
                'validationMessage' => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.pagination_validation_message',
199
                'showExternalParam' => false,
200
            ]
201
        ];
202
    }
203
204
    /**
205
     * Properties for proper links handling
206
     *
207
     * @return array
208
     */
209
    private function getPageLinkProperties(): array
210
    {
211
        return [
212
            'postPage' => [
213
                'group'       => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.links_group',
214
                'title'       => 'rainlab.blog::lang.settings.posts_post',
215
                'description' => 'rainlab.blog::lang.settings.posts_description',
216
                'type'        => 'dropdown',
217
                'default'     => 'blog/post',
218
                'showExternalParam' => false,
219
            ],
220
            'categoryPage' => [
221
                'group'       => Plugin::LOCALIZATION_KEY . 'components.post_list_abstract.links_group',
222
                'title'       => 'rainlab.blog::lang.settings.posts_category',
223
                'description' => 'rainlab.blog::lang.settings.posts_category_description',
224
                'type'        => 'dropdown',
225
                'default'     => 'blog/category',
226
                'showExternalParam' => false,
227
            ],
228
        ];
229
    }
230
231
    /**
232
     * @return void
233
     */
234
    private function populatePagination()
235
    {
236
        $this->currentPage = (int)$this->property('page', 1) ?: (int)post('page');
237
        $this->resultsPerPage = (int)$this->property('resultsPerPage')
238
            ?: $this->defineProperties()['resultsPerPage']['default'];
239
    }
240
241
    /**
242
     * @return void
243
     */
244
    private function populateLinks()
245
    {
246
        $this->postPage = $this->property('postPage');
247
        $this->categoryPage = $this->property('categoryPage');
248
    }
249
250
    /**
251
     * @param $query
252
     */
253
    private function handleOrder(Builder $query)
254
    {
255
        if (array_key_exists($this->orderBy, self::$postAllowedSortingOptions)) {
256
            if ($this->orderBy === 'random') {
257
                $query->inRandomOrder();
258
            } else {
259
                list($sortField, $sortDirection) = explode(' ', $this->orderBy);
260
261
                $query->orderBy($sortField, $sortDirection);
262
            }
263
        }
264
    }
265
}
266