Passed
Push — master ( c5b742...46b7b3 )
by Andrey
05:36 queued 02:56
created

Search   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 5
Bugs 3 Features 0
Metric Value
eloc 29
c 5
b 3
f 0
dl 0
loc 91
ccs 33
cts 36
cp 0.9167
rs 10
wmc 16

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 11 1
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
     * @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
25 66
        $nums  = $this->filterNum($value);
26 66
        $slugs = $this->filterSlugs($value);
27
28
        return $builder->where(function (Builder $builder) use ($nums, $slugs) {
29
            $builder
30 66
                ->whereIn('id', $nums)
31 66
                ->orWhereIn('slug', $slugs);
32 66
        });
33
    }
34
35
    /**
36
     * @param  \Helldar\Roles\Models\BaseModel|int|string  $value
37
     *
38
     * @return int|string
39
     */
40 66
    public function getId($value)
41
    {
42 66
        return $value instanceof BaseModel
43
            ? $value->id
44 66
            : $value;
45
    }
46
47
    /**
48
     * @param  array|Arrayable|Collection|int|string  $values
49
     *
50
     * @return array
51
     */
52 66
    protected function map($values): array
53
    {
54
        return array_map(function ($value) {
55 66
            return $this->getId($value);
56 66
        }, $this->toArray($values));
57
    }
58
59 66
    protected function filterNum(array $values): array
60
    {
61
        return array_filter($values, function ($value) {
62 66
            return is_numeric($value);
63 66
        });
64
    }
65
66 66
    protected function filterSlugs(array $values): array
67
    {
68
        return array_filter($values, function ($value) {
69 66
            return ! is_numeric($value);
70 66
        });
71
    }
72
73 66
    protected function toArray($values): array
74
    {
75 66
        if ($this->isRole($values) || $this->isPermission($values)) {
76
            return [$values];
77 66
        } elseif ($this->isArrayable($values)) {
78
            $values = $values->toArray();
79 66
        } elseif (! $this->isArray($values)) {
80 54
            $values = Arr::wrap($values);
81
        }
82
83 66
        return Arr::flatten($values);
84
    }
85
86 66
    protected function isArray($values): bool
87
    {
88 66
        return is_array($values);
89
    }
90
91 66
    protected function isArrayable($values): bool
92
    {
93 66
        return $values instanceof Arrayable || $values instanceof Collection;
94
    }
95
96 66
    protected function isRole($value): bool
97
    {
98 66
        return $value instanceof Role;
99
    }
100
101 66
    protected function isPermission($value): bool
102
    {
103 66
        return $value instanceof Permission;
104
    }
105
}
106