MongoCollection   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 38
eloc 93
c 3
b 0
f 1
dl 0
loc 237
rs 9.36

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getBySlugAndStatus() 0 17 5
A exist() 0 6 2
A getNotDeleted() 0 7 2
A getActive() 0 4 1
A getPublic() 0 7 2
A hasItem() 0 22 5
A hasRole() 0 16 4
A getBySlug() 0 14 3
A findByAID() 0 5 1
A checkPermission() 0 16 4
A hasPermission() 0 16 4
A getPublished() 0 7 2
A moveFirst() 0 7 3
1
<?php
2
3
namespace OfflineAgency\MongoAutoSync\Extensions;
4
5
use Illuminate\Database\Eloquent\Collection;
6
7
class MongoCollection extends Collection
8
{
9
    //Method to retrieve a single element of a collection by slug, very useful for frontend
10
    public function getBySlugAndStatus($category = null, $myslug = null)
11
    {
12
        $cl = cl();
13
        //TODO: move primarycategory, status, slug on config file
14
        $out = $this->filter(function ($col) use ($category, $myslug, $cl) {
15
            if ($col->slug[$cl] === $myslug && $col->status === 'published' && $col->primarycategory->slug[$cl] === $category) {
16
                return true;
17
            } else {
18
                return false;
19
            }
20
        })->first();
21
        if (! $out) {//Handler 404 Object Not Found
22
            $obj_name = get_class($this->first());
23
            $message = __('error.'.$obj_name);
24
            abort(404, $message);
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type array and array; however, parameter $message of abort() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
            abort(404, /** @scrutinizer ignore-type */ $message);
Loading history...
25
        } else {
26
            return $out;
27
        }
28
    }
29
30
    /**
31
     * @param  null  $myslug
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $myslug is correct as it would always require null to be passed?
Loading history...
32
     * @return mixed
33
     */
34
    public function getBySlug($myslug = null)
35
    {
36
        $cl = cl();
37
        $out = $this->filter(function ($col) use ($myslug, $cl) {
38
            if ($col->slug[$cl] === $myslug) {
39
                return true;
40
            }
41
        })->first();
42
        if (! $out) {//Handler 404 Object Not Found
43
            $obj_name = get_class($this->first());
44
            $message = __('error.'.$obj_name);
45
            abort(404, $message);
0 ignored issues
show
Bug introduced by
It seems like $message can also be of type array and array; however, parameter $message of abort() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            abort(404, /** @scrutinizer ignore-type */ $message);
Loading history...
46
        } else {
47
            return $out;
48
        }
49
    }
50
51
    //Method to retrieve only not deleted item of a collection - Check on is_deleted custom property added on MDMODEL ovverriding init, delete
52
53
    /**
54
     * @return MongoCollection
55
     */
56
    public function getNotDeleted()
57
    {
58
        return $this->filter(function ($col) {
59
            if ($col->is_deleted) {
60
                return false;
61
            } else {
62
                return true;
63
            }
64
        });
65
    }
66
67
    //Method to retrieve only published item of a collection - Check on status entry
68
69
    /**
70
     * @return MongoCollection
71
     */
72
    public function getPublished()
73
    {
74
        return $this->filter(function ($col) {
75
            if ($col->status === 'published') {
76
                return true;
77
            } else {
78
                return false;
79
            }
80
        });
81
    }
82
83
    //Method to retrieve only public item of a collection - Check on status entry
84
85
    /**
86
     * @return MongoCollection
87
     */
88
    public function getPublic()
89
    {
90
        return $this->filter(function ($col) {
91
            if ($col->visibility === 'public') {
92
                return true;
93
            } else {
94
                return false;
95
            }
96
        });
97
    }
98
99
    //Check if the collection has an item with ref_id equal to id of the obj pass in to the parameter, useful to mark a category already selected in edit
100
101
    /**
102
     * @param  $obj
103
     * @return bool
104
     */
105
    public function hasItem($obj)
106
    {
107
        if (is_null($obj)) {
108
            return false;
109
        } elseif (is_null($obj->id)) {
110
            return false;
111
        }
112
113
        $id = $obj->id;
114
115
        $out = $this->filter(function ($col) use ($id) {
116
            if ($col->ref_id === $id) {
117
                return true;
118
            } else {
119
                return false;
120
            }
121
        });
122
123
        if ($out->count() > 0) {
124
            return true;
125
        } else {
126
            return false;
127
        }
128
    }
129
130
    //Move the item with ref_id equal to the parameter, useful for edit primary category
131
132
    /**
133
     * @param  $id
134
     * @return $this
135
     */
136
    public function moveFirst($id)
137
    {
138
        for ($i = 0; $i <= ($this->count() - 1); $i++) {
139
            $this[$i]->ref_id == $id ? $this->prepend($this->splice($i, 1)[0]) : 0;
140
        }
141
142
        return $this;
143
    }
144
145
    /**
146
     * @return MongoCollection
147
     */
148
    public function getActive()
149
    {
150
        return $this->filter(function ($col) {
151
            return $col->is_active;
152
        });
153
    }
154
155
    /**
156
     * @return bool
157
     */
158
    public function exist()
159
    {
160
        if ($this->count() > 0) {
161
            return true;
162
        } else {
163
            return false;
164
        }
165
    }
166
167
    /**
168
     * @param  string  $aid
169
     * @return mixed
170
     */
171
    public function findByAID(string $aid)
172
    {
173
        return $this->filter(function ($col) use ($aid) {
174
            return $col->autoincrement_id == $aid;
175
        })->first();
176
    }
177
178
    /**
179
     * @param  $id
180
     * @return bool
181
     */
182
    public function hasPermission($id)
183
    {
184
        if (is_null($id)) {
185
            return false;
186
        }
187
188
        $out = $this->filter(function ($col) use ($id) {
189
            if ($col->ref_id == $id) {
190
                return true;
191
            }
192
        });
193
194
        if ($out->count() > 0) {
195
            return true;
196
        } else {
197
            return false;
198
        }
199
    }
200
201
    /**
202
     * @param  $name
203
     * @return bool
204
     */
205
    public function hasRole($name)
206
    {
207
        if (is_null($name)) {
208
            return false;
209
        }
210
211
        $out = $this->filter(function ($col) use ($name) {
212
            if ($col->name == $name) {
213
                return true;
214
            }
215
        });
216
217
        if ($out->count() > 0) {
218
            return true;
219
        } else {
220
            return false;
221
        }
222
    }
223
224
    /**
225
     * @param  $name
226
     * @return bool
227
     */
228
    public function checkPermission($name)
229
    {
230
        if (is_null($name)) {
231
            return false;
232
        }
233
234
        $out = $this->filter(function ($col) use ($name) {
235
            if ($col->name == $name) {
236
                return true;
237
            }
238
        });
239
240
        if ($out->count() > 0) {
241
            return true;
242
        } else {
243
            return false;
244
        }
245
    }
246
}
247