Completed
Push — master ( b81254...51291a )
by Gino
02:08
created

ModelAbstract::queryGroupBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
    const CONNECTION_SQLITE = 'sqlite';
19
20
    /**
21
     * @var array
22
     */
23
    public static $sortingOptions = [];
24
25
    /**
26
     * Sets the URL attribute with a URL to this object
27
     *
28
     * @param string $pageName
29
     * @param Controller $controller
30
     * @param array $params
31
     *
32
     * @return void
33
     */
34
    public function setUrl($pageName, Controller $controller, array $params = array())
35
    {
36
        $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...
37
38
        $this->url = $controller->pageUrl($pageName, $params);
39
    }
40
41
    /**
42
     * @param array $params
43
     *
44
     * @return array
45
     */
46
    abstract protected function getModelUrlParams(array $params): array;
47
48
    /**
49
     * Gets a list of items related to Posts for frontend use
50
     *
51
     * @param       $query
52
     * @param array $options Available options are "sort", "displayEmpty", "limit", "post"
53
     *
54
     * @return mixed
55
     */
56
    public function scopeListFrontend(Builder $query, array $options = [])
57
    {
58
        $this->withRelation($query);
59
60
        $this->queryOrderBy($query, $options);
61
62
        $this->queryDisplayEmpty($query, $options);
63
64
        $this->queryPostSlug($query, $options);
65
66
        $this->queryLimit($query, $options);
67
68
        return $query->get();
69
    }
70
71
    /**
72
     * @param Builder   $query
73
     * @param string    $property
74
     * @param mixed     $value
75
     */
76
    public function scopeWhereTranslatable(Builder $query, string $property, $value)
77
    {
78
        self::whereTranslatableProperty($query, $property, $value);
79
    }
80
81
    /**
82
     * @param Builder $query
83
     * @param string $property
84
     * @param $value
85
     */
86
    public static function whereTranslatableProperty(Builder $query, string $property, $value)
87
    {
88
        $query->getModel()->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')
89
            ? $query->transWhere($property, $value)
90
            : $query->where($property, $value);
91
    }
92
93
    /**
94
     * @param Builder $query
95
     * @param array   $options
96
     *
97
     * @return void
98
     */
99
    private function queryDisplayEmpty(Builder $query, array $options)
100
    {
101
        if (empty($options['displayEmpty'])) {
102
            $query->withCount(
103
                [
104
                    'posts' => function ($query) {
105
                        $query->isPublished();
106
                    }
107
                ]
108
            )->having('posts_count', '>', 0);
109
110
            $connectionConfig = $query->getConnection()->getConfig();
111
112
            // GROUP BY is required for SQLite-based installations
113
            if (!empty($connectionConfig['name']) &&
114
                strtolower($connectionConfig['name']) === self::CONNECTION_SQLITE
115
            ) {
116
                $query->groupBy('id');
117
            }
118
        }
119
    }
120
121
    /**
122
     * @param Builder $query
123
     * @param array   $options
124
     *
125
     * @return void
126
     */
127
    private function queryPostSlug(Builder $query, array $options)
128
    {
129
        if (!empty($options['post'])) {
130
            $query->whereHas(
131
                'posts',
132
                function ($query) use ($options) {
133
                    ModelAbstract::whereTranslatableProperty($query, 'slug', $options['post']);
134
                }
135
            );
136
        }
137
    }
138
139
    /**
140
     * @param Builder $query
141
     * @param array   $options
142
     *
143
     * @return void
144
     */
145
    private function queryLimit(Builder $query, array $options)
146
    {
147
        if (!empty($options['limit'])) {
148
            $query->take($options['limit']);
149
        }
150
    }
151
152
    /**
153
     * @param Builder $query
154
     * @param array   $options
155
     *
156
     * @return void
157
     */
158
    private function queryOrderBy(Builder $query, array $options)
159
    {
160
        if (\array_key_exists($options['sort'], static::$sortingOptions)) {
161
            if ($options['sort'] === 'random') {
162
                $query->inRandomOrder();
163
            } else {
164
                list($sortField, $sortDirection) = explode(' ', $options['sort']);
165
166
                if ($sortField === 'posts_count') {
167
                    $query->withCount(
168
                        [
169
                            'posts' => function ($query) {
170
                                $query->isPublished();
171
                            }
172
                        ]
173
                    );
174
                }
175
176
                $query->orderBy($sortField, $sortDirection);
177
            }
178
        }
179
    }
180
181
    /**
182
     * @param Builder $query
183
     *
184
     * @return void
185
     */
186
    private function withRelation(Builder $query)
187
    {
188
        $query->with(
189
            [
190
                'posts' => function ($query) {
191
                    $query->isPublished();
192
                }
193
            ]
194
        );
195
    }
196
197
    /**
198
     * @param Builder $query
199
     */
200
    private function queryGroupBy(Builder $query)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
201
    {
202
        $query->groupBy('id');
203
    }
204
}
205