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

PostListAbstract::prepareVars()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Components;
4
5
use Cms\Classes\Page;
6
use Illuminate\Http\Response;
7
use Rainlab\Blog\Models\Post;
8
use Cms\Classes\ComponentBase;
9
use Illuminate\Database\Eloquent\Collection;
10
11
/**
12
 * Class PostListAbstract
13
 *
14
 * @package GinoPane\BlogTaxonomy\Components
15
 */
16
abstract class PostListAbstract extends ComponentBase
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...
17
{
18
    use UrlHelperTrait;
19
20
    /**
21
     * @var Collection | array
22
     */
23
    public $posts = [];
24
25
    /**
26
     * @var integer             The current page
27
     */
28
    public $currentPage;
29
30
    /**
31
     * @var integer             The number of results per page
32
     */
33
    public $resultsPerPage;
34
35
    /**
36
     * Message to display when there are no posts
37
     *
38
     * @var string
39
     */
40
    public $noPostsMessage;
41
42
    /**
43
     * If the post list should be ordered by another attribute
44
     *
45
     * @var string
46
     */
47
    public $orderBy;
48
49
    /**
50
     * The attributes on which the post list can be ordered
51
     * @var array
52
     */
53
    public static $postAllowedSortingOptions = [
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $postAllowedSortingOptions exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
54
        'title asc' => 'Title (ascending)',
55
        'title desc' => 'Title (descending)',
56
        'created_at asc' => 'Created (ascending)',
57
        'created_at desc' => 'Created (descending)',
58
        'updated_at asc' => 'Updated (ascending)',
59
        'updated_at desc' => 'Updated (descending)',
60
        'published_at asc' => 'Published (ascending)',
61
        'published_at desc' => 'Published (descending)',
62
        'random' => 'Random'
63
    ];
64
65
    /**
66
     * Component Properties
67
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

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...
68
     */
69
    public function defineProperties()
70
    {
71
        return [
72
            'noPostsMessage' => [
73
                'title'        => 'rainlab.blog::lang.settings.posts_no_posts',
74
                'description'  => 'rainlab.blog::lang.settings.posts_no_posts_description',
75
                'type'         => 'string',
76
                'showExternalParam' => false
77
            ],
78
            'orderBy' => [
79
                'title'       => 'rainlab.blog::lang.settings.posts_order',
80
                'description' => 'rainlab.blog::lang.settings.posts_order_description',
81
                'type'        => 'dropdown',
82
                'default'     => 'published_at asc',
83
                'showExternalParam' => false
84
            ],
85
86
            'page' => [
87
                'title'         => 'Page',
88
                'description'   => 'The URL parameter defining the page number.',
89
                'default'       => '{{ :page }}',
90
                'type'          => 'string',
91
                'group'         => 'Pagination',
92
            ],
93
            'resultsPerPage' => [
94
                'title'         => 'Results',
95
                'description'   => 'The number of posts to display per page.',
96
                'default'       => 10,
97
                'type'          => 'string',
98
                'validationPattern' => '^(0+)?[1-9]\d*$',
99
                'validationMessage' => 'Results per page must be a positive whole number.',
100
                'showExternalParam' => false,
101
                'group'         => 'Pagination',
102
            ],
103
104
            'postPage' => [
105
                'title'       => 'Post page',
106
                'description' => 'Page to show linked posts',
107
                'type'        => 'dropdown',
108
                'default'     => 'blog/post',
109
                'group'       => 'Links',
110
            ],
111
            'categoryPage' => [
112
                'title'       => 'rainlab.blog::lang.settings.posts_category',
113
                'description' => 'rainlab.blog::lang.settings.posts_category_description',
114
                'type'        => 'dropdown',
115
                'default'     => 'blog/category',
116
                'group'       => 'Links',
117
            ]
118
        ];
119
    }
120
121
    /**
122
     * @see Post::$allowedSortingOptions
123
     *
124
     * @return mixed
125
     */
126
    public function getOrderByOptions()
127
    {
128
        return static::$postAllowedSortingOptions;
129
    }
130
131
    /**
132
     * Query the tag and posts belonging to it
133
     */
134
    public function onRun()
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...
135
    {
136
        if (is_null($this->prepareContextItem())) {
137
            return $this->controller->run(Response::HTTP_NOT_FOUND);
138
        }
139
140
        $this->prepareVars();
141
142
        $this->listPosts();
143
    }
144
145
    /**
146
     * Prepare variables
147
     */
148
    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...
149
150
    /**
151
     * Prepare variables
152
     */
153
    protected function prepareVars()
154
    {
155
        $this->currentPage = intval($this->property('page', 1)) ?: intval(post('page'));
156
        $this->resultsPerPage = intval($this->property('resultsPerPage'))
157
            ?: $this->defineProperties()['resultsPerPage']['default'];
158
159
        $this->noPostsMessage = $this->page['noPostsMessage'] = $this->property('noPostsMessage');
160
        $this->orderBy = $this->page['orderBy'] = $this->property('orderBy');
161
162
        // Page links
163
        $this->postPage = $this->page['postPage' ] = $this->property('postPage');
164
        $this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage');
165
    }
166
167
    /**
168
     * Load a list of posts
169
     */
170
    public function listPosts()
171
    {
172
        $query = $this->getPostsQuery();
173
174
        if (in_array($this->orderBy, array_keys(self::$postAllowedSortingOptions))) {
175
            if ($this->orderBy == 'random') {
176
                $query->inRandomOrder();
177
            } else {
178
                list($sortField, $sortDirection) = explode(' ', $this->orderBy);
179
180
                $query->orderBy($sortField, $sortDirection);
181
            }
182
        }
183
184
        $posts = $query->paginate($this->resultsPerPage, $this->currentPage);
185
186
        // Add a "url" helper attribute for linking to each post and category
187
        if ($posts && $posts->count()) {
188
            $posts->each([$this, 'setPostUrls']);
189
        }
190
191
        $this->posts = $posts;
192
    }
193
194
    /**
195
     * @return mixed
196
     */
197
    abstract protected function getPostsQuery();
198
199
    /**
200
     * @return mixed
201
     */
202
    public function getPostPageOptions()
203
    {
204
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
205
    }
206
207
    /**
208
     * @return mixed
209
     */
210
    public function getCategoryPageOptions()
211
    {
212
        return Page::sortBy('baseFileName')->lists('baseFileName', 'baseFileName');
213
    }
214
}
215