Completed
Push — master ( 17c7d3...95752d )
by Abdelrahman
02:30 queued 01:24
created

Pageable::scopeWithAllPages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Pages\Traits;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Relations\MorphToMany;
9
10
trait Pageable
11
{
12
    /**
13
     * Register a saved model event with the dispatcher.
14
     *
15
     * @param \Closure|string $callback
16
     *
17
     * @return void
18
     */
19
    abstract public static function saved($callback);
20
21
    /**
22
     * Register a deleted model event with the dispatcher.
23
     *
24
     * @param \Closure|string $callback
25
     *
26
     * @return void
27
     */
28
    abstract public static function deleted($callback);
29
30
    /**
31
     * Define a polymorphic many-to-many relationship.
32
     *
33
     * @param string $related
34
     * @param string $name
35
     * @param string $table
0 ignored issues
show
Documentation introduced by
Should the type for parameter $table not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
36
     * @param string $foreignPivotKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $foreignPivotKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
37
     * @param string $relatedPivotKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $relatedPivotKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     * @param string $parentKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $parentKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
     * @param string $relatedKey
0 ignored issues
show
Documentation introduced by
Should the type for parameter $relatedKey not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     * @param bool   $inverse
41
     *
42
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
43
     */
44
    abstract public function morphToMany(
45
        $related,
46
        $name,
47
        $table = null,
48
        $foreignPivotKey = null,
49
        $relatedPivotKey = null,
50
        $parentKey = null,
51
        $relatedKey = null,
52
        $inverse = false
53
    );
54
55
    /**
56
     * Get all attached pages to the model.
57
     *
58
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
59
     */
60
    public function pages(): MorphToMany
61
    {
62
        return $this->morphToMany(config('rinvex.pages.models.page'), 'pageable', config('rinvex.pages.tables.pageables'), 'pageable_id', 'page_id')
0 ignored issues
show
Bug introduced by
The method orderBy() does not exist on Illuminate\Database\Eloquent\Relations\MorphToMany. Did you maybe mean orderByPivot()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
63
                    ->orderBy('sort_order')
64
                    ->withTimestamps();
65
    }
66
67
    /**
68
     * Boot the pageable trait for the model.
69
     *
70
     * @return void
71
     */
72
    public static function bootPageable()
73
    {
74
        static::deleted(function (self $model) {
75
            $model->pages()->detach();
76
        });
77
    }
78
79
    /**
80
     * Attach the given page(s) to the model.
81
     *
82
     * @param mixed $pages
83
     *
84
     * @return void
85
     */
86
    public function setPagesAttribute($pages): void
87
    {
88
        static::saved(function (self $model) use ($pages) {
89
            $model->syncPages($pages);
90
        });
91
    }
92
93
    /**
94
     * Scope query with all the given pages.
95
     *
96
     * @param \Illuminate\Database\Eloquent\Builder $builder
97
     * @param mixed                                 $pages
98
     *
99
     * @return \Illuminate\Database\Eloquent\Builder
100
     */
101
    public function scopeWithAllPages(Builder $builder, $pages): Builder
102
    {
103
        collect($pages)->each(function ($page) use ($builder) {
104
            $builder->whereHas('pages', function (Builder $builder) use ($page) {
105
                return $builder->where('id', $page);
106
            });
107
        });
108
109
        return $builder;
110
    }
111
112
    /**
113
     * Scope query with any of the given pages.
114
     *
115
     * @param \Illuminate\Database\Eloquent\Builder $builder
116
     * @param mixed                                 $pages
117
     *
118
     * @return \Illuminate\Database\Eloquent\Builder
119
     */
120
    public function scopeWithAnyPages(Builder $builder, $pages): Builder
121
    {
122
        return $builder->whereHas('pages', function (Builder $builder) use ($pages) {
123
            $builder->whereIn('id', $pages);
124
        });
125
    }
126
127
    /**
128
     * Scope query without any of the given pages.
129
     *
130
     * @param \Illuminate\Database\Eloquent\Builder $builder
131
     * @param mixed                                 $pages
132
     *
133
     * @return \Illuminate\Database\Eloquent\Builder
134
     */
135
    public function scopeWithoutPages(Builder $builder, $pages): Builder
136
    {
137
        return $builder->whereDoesntHave('pages', function (Builder $builder) use ($pages) {
138
            $builder->whereIn('id', $pages);
139
        });
140
    }
141
142
    /**
143
     * Scope query without any pages.
144
     *
145
     * @param \Illuminate\Database\Eloquent\Builder $builder
146
     *
147
     * @return \Illuminate\Database\Eloquent\Builder
148
     */
149
    public function scopeWithoutAnyPages(Builder $builder): Builder
150
    {
151
        return $builder->doesntHave('pages');
152
    }
153
154
    /**
155
     * Determine if the model has any of the given pages.
156
     *
157
     * @param mixed $pages
158
     *
159
     * @return bool
160
     */
161
    public function hasAnyPages($pages): bool
162
    {
163
        return ! $this->pages->pluck('id')->intersect($pages)->isEmpty();
0 ignored issues
show
Bug introduced by
The property pages does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
164
    }
165
166
    /**
167
     * Determine if the model has all of the given pages.
168
     *
169
     * @param mixed $pages
170
     *
171
     * @return bool
172
     */
173
    public function hasAllPages($pages): bool
174
    {
175
        return collect($pages)->diff($this->pages->pluck('id'))->isEmpty();
176
    }
177
178
    /**
179
     * Attach model pages.
180
     *
181
     * @param mixed $pages
182
     *
183
     * @return $this
184
     */
185
    public function attachPages($pages)
186
    {
187
        // Use 'sync' not 'attach' to avoid Integrity constraint violation
188
        $this->pages()->sync($pages, false);
189
190
        return $this;
191
    }
192
193
    /**
194
     * Sync model pages.
195
     *
196
     * @param mixed $pages
197
     * @param bool  $detaching
198
     *
199
     * @return $this
200
     */
201
    public function syncPages($pages, bool $detaching = true)
202
    {
203
        $this->pages()->sync($pages, $detaching);
204
205
        return $this;
206
    }
207
208
    /**
209
     * Detach model pages.
210
     *
211
     * @param mixed $pages
212
     *
213
     * @return $this
214
     */
215
    public function detachPages($pages = null)
216
    {
217
        $this->pages()->detach($pages);
218
219
        return $this;
220
    }
221
}
222