Completed
Push — master ( 7cc6a0...b81254 )
by Gino
01:48
created

ModelAbstract::queryDisplayEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace GinoPane\BlogTaxonomy\Models;
4
5
use Model;
6
use Cms\Classes\Controller;
7
use October\Rain\Database\Builder;
8
9
/**
10
 * Class ModelAbstract
11
 *
12
 * @property string $url
13
 *
14
 * @package GinoPane\BlogTaxonomy\Models
15
 */
16
abstract class ModelAbstract extends Model
0 ignored issues
show
Coding Style introduced by
ModelAbstract 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
    /**
19
     * @var array
20
     */
21
    public static $sortingOptions = [];
22
23
    /**
24
     * Sets the URL attribute with a URL to this object
25
     *
26
     * @param string $pageName
27
     * @param Controller $controller
28
     * @param array $params
29
     *
30
     * @return void
31
     */
32
    public function setUrl($pageName, Controller $controller, array $params = array())
33
    {
34
        $params = $this->getModelUrlParams($params);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $params. This often makes code more readable.
Loading history...
35
36
        $this->url = $controller->pageUrl($pageName, $params);
37
    }
38
39
    /**
40
     * @param array $params
41
     *
42
     * @return array
43
     */
44
    abstract protected function getModelUrlParams(array $params): array;
45
46
    /**
47
     * Gets a list of items related to Posts for frontend use
48
     *
49
     * @param       $query
50
     * @param array $options Available options are "sort", "displayEmpty", "limit", "post"
51
     *
52
     * @return mixed
53
     */
54
    public function scopeListFrontend(Builder $query, array $options = [])
55
    {
56
        $this->withRelation($query);
57
58
        $this->queryOrderBy($query, $options);
59
60
        $this->queryDisplayEmpty($query, $options);
61
62
        $this->queryPostSlug($query, $options);
63
64
        $this->queryLimit($query, $options);
65
66
        return $query->get();
67
    }
68
69
    /**
70
     * @param Builder   $query
71
     * @param string    $property
72
     * @param mixed     $value
73
     */
74
    public function scopeWhereTranslatable(Builder $query, string $property, $value)
75
    {
76
        self::whereTranslatableProperty($query, $property, $value);
77
    }
78
79
    /**
80
     * @param Builder $query
81
     * @param string $property
82
     * @param $value
83
     */
84
    public static function whereTranslatableProperty(Builder $query, string $property, $value)
85
    {
86
        $query->getModel()->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')
87
            ? $query->transWhere($property, $value)
88
            : $query->where($property, $value);
89
    }
90
91
    /**
92
     * @param Builder $query
93
     * @param array   $options
94
     *
95
     * @return void
96
     */
97
    private function queryDisplayEmpty(Builder $query, array $options)
98
    {
99
        if (empty($options['displayEmpty'])) {
100
            $query->withCount(
101
                [
102
                    'posts' => function ($query) {
103
                        $query->isPublished();
104
                    }
105
                ]
106
            )->where('posts_count', '>', 0);
107
        }
108
    }
109
110
    /**
111
     * @param Builder $query
112
     * @param array   $options
113
     *
114
     * @return void
115
     */
116
    private function queryPostSlug(Builder $query, array $options)
117
    {
118
        if (!empty($options['post'])) {
119
            $query->whereHas(
120
                'posts',
121
                function ($query) use ($options) {
122
                    ModelAbstract::whereTranslatableProperty($query, 'slug', $options['post']);
123
                }
124
            );
125
        }
126
    }
127
128
    /**
129
     * @param Builder $query
130
     * @param array   $options
131
     *
132
     * @return void
133
     */
134
    private function queryLimit(Builder $query, array $options)
135
    {
136
        if (!empty($options['limit'])) {
137
            $query->take($options['limit']);
138
        }
139
    }
140
141
    /**
142
     * @param Builder $query
143
     * @param array   $options
144
     *
145
     * @return void
146
     */
147
    private function queryOrderBy(Builder $query, array $options)
148
    {
149
        if (array_key_exists($options['sort'], self::$sortingOptions)) {
150
            if ($options['sort'] === 'random') {
151
                $query->inRandomOrder();
152
            } else {
153
                list($sortField, $sortDirection) = explode(' ', $options['sort']);
154
155
                if ($sortField === 'posts_count') {
156
                    $query->withCount(
157
                        [
158
                            'posts' => function ($query) {
159
                                $query->isPublished();
160
                            }
161
                        ]
162
                    );
163
                }
164
165
                $query->orderBy($sortField, $sortDirection);
166
            }
167
        }
168
    }
169
170
    /**
171
     * @param Builder $query
172
     *
173
     * @return void
174
     */
175
    private function withRelation(Builder $query)
176
    {
177
        $query->with(
178
            [
179
                'posts' => function ($query) {
180
                    $query->isPublished();
181
                }
182
            ]
183
        );
184
    }
185
}
186