Passed
Push — master ( ab2678...bf154d )
by webdevetc
14:17
created

HasCategoriesTrait::categories()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 5
dl 0
loc 14
rs 10
c 4
b 1
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace WebDevEtc\BlogEtc\Requests\Traits;
4
5
use WebDevEtc\BlogEtc\Models\Category;
6
7
/**
8
 * Class HasCategoriesTrait.
9
 */
10
trait HasCategoriesTrait
11
{
12
    /**
13
     * If $_GET['category'] slugs were submitted, then it should return an array of the IDs.
14
     *
15
     * @return array
16
     */
17
    public function categories()
18
    {
19
        if (!$this->get('category') || !is_array($this->get('category'))) {
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

19
        if (!$this->/** @scrutinizer ignore-call */ get('category') || !is_array($this->get('category'))) {
Loading history...
20
            return [];
21
        }
22
23
        //$this->get("category") is an array of category SLUGs and we need IDs
24
25
        // check they are valid, return the IDs
26
        // limit to 1000 ... just in case someone submits with too many for the web server. No error is given if they submit more than 1k.
27
        $vals = Category::whereIn('id', array_keys($this->get('category')))->select('id')->limit(1000)->get();
28
        $vals = array_values($vals->pluck('id')->toArray());
29
30
        return $vals;
31
    }
32
}
33