Passed
Push — master ( 1b5d5a...c5b742 )
by Andrey
12:01
created

Search::isRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Helldar\Roles\Support\Database;
4
5
use Helldar\Roles\Models\BaseModel;
6
use Helldar\Roles\Models\Permission;
7
use Helldar\Roles\Models\Role;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Collection;
12
13
class Search
14
{
15
    /**
16
     * @param  \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation  $builder
17
     * @param  \Helldar\Roles\Models\BaseModel|int|string  $values
18
     *
19 66
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation
20
     */
21 66
    public function by($builder, $values)
22
    {
23 66
        $value = $this->map($values);
24 66
25
        $nums  = $this->filterNum($value);
26 66
        $slugs = $this->filterSlugs($value);
27 54
28
        if ($nums && ! $slugs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $slugs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $nums of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
29
            return $builder->whereIn('id', $nums);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $builder->whereIn('id', $nums) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloq...uent\Relations\Relation.
Loading history...
30 54
        }
31 54
32
        if (! $nums && $slugs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $nums of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $slugs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
33
            return $builder->whereIn('slug', $slugs);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $builder->whereIn('slug', $slugs) also could return the type Illuminate\Database\Query\Builder which is incompatible with the documented return type Illuminate\Database\Eloq...uent\Relations\Relation.
Loading history...
34
        }
35
36 36
        return $builder->where(function (Builder $builder) use ($nums, $slugs) {
37 36
            $builder
38 36
                ->whereIn('id', $nums)
39
                ->orWhereIn('slug', $slugs);
40
        });
41
    }
42
43
    /**
44
     * @param  \Helldar\Roles\Models\BaseModel|int|string  $value
45
     *
46 66
     * @return int|string
47
     */
48 66
    public function getId($value)
49
    {
50 66
        return $value instanceof BaseModel
51
            ? $value->id
52
            : $value;
53
    }
54
55
    /**
56
     * @param  array|Arrayable|Collection|int|string  $values
57
     *
58 66
     * @return array
59
     */
60
    protected function map($values): array
61 66
    {
62 66
        return array_map(function ($value) {
63
            return $this->getId($value);
64
        }, $this->toArray($values));
65 66
    }
66
67
    protected function filterNum(array $values): array
68 66
    {
69 66
        return array_filter($values, function ($value) {
70
            return is_numeric($value);
71
        });
72 66
    }
73
74
    protected function filterSlugs(array $values): array
75 66
    {
76 66
        return array_filter($values, function ($value) {
77
            return ! is_numeric($value);
78
        });
79 66
    }
80
81 66
    protected function toArray($values): array
82
    {
83 66
        if ($this->isRole($values) || $this->isPermission($values)) {
84 54
            return [$values];
85
        } elseif ($this->isArrayable($values)) {
86
            $values = $values->toArray();
87 66
        } elseif (! $this->isArray($values)) {
88
            $values = Arr::wrap($values);
89
        }
90 66
91
        return Arr::flatten($values);
92 66
    }
93
94
    protected function isArray($values): bool
95 66
    {
96
        return is_array($values);
97 66
    }
98
99
    protected function isArrayable($values): bool
100
    {
101
        return $values instanceof Arrayable || $values instanceof Collection;
102
    }
103
104
    protected function isRole($value): bool
105
    {
106
        return $value instanceof Role;
107
    }
108
109
    protected function isPermission($value): bool
110
    {
111
        return $value instanceof Permission;
112
    }
113
}
114