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

Search   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 4
Bugs 3 Features 0
Metric Value
eloc 33
c 4
b 3
f 0
dl 0
loc 99
ccs 32
cts 34
cp 0.9412
rs 10
wmc 20

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getId() 0 5 2
A filterSlugs() 0 4 1
A toArray() 0 11 5
A by() 0 19 5
A filterNum() 0 4 1
A isPermission() 0 3 1
A map() 0 5 1
A isArray() 0 3 1
A isArrayable() 0 3 2
A isRole() 0 3 1
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