Passed
Push — develop ( f99f9b...4e8497 )
by Enea
02:07
created

Repository::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author enea dhack <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Enea\Authorization\Repositories;
13
14
use Illuminate\Database\Eloquent\Builder;
15
use Illuminate\Database\Eloquent\Model;
16
use Illuminate\Support\Collection;
17
18
abstract class Repository
19
{
20
    abstract protected function contract(): string;
21
22 2
    public function delete(string $secretName): bool
23
    {
24 2
        return $this->query()->where('secret_name', $secretName)->delete() > 0;
25
    }
26
27 2
    public function createMultiple(array $structs): Collection
28
    {
29 2
        return $this->register($structs);
30
    }
31
32
    protected function register(array $authorizations): Collection
33
    {
34 6
        return $this->transform($authorizations)->map(function (array $attributes): Model {
35 6
            return $this->query()->create($attributes);
36 6
        });
37
    }
38
39
    private function transform(array $authorization): Collection
40
    {
41 6
        return collect($authorization)->map(function (Struct $struct) {
42
            return [
43 6
                'display_name' => $struct->getName(),
44 6
                'description' => $struct->getDescription(),
45
            ];
46 6
        });
47
    }
48
49 6
    private function query(): Builder
50
    {
51 6
        return $this->model()->newQuery();
52
    }
53
54 6
    private function model(): Model
55
    {
56 6
        return app()->make($this->contract());
57
    }
58
}
59