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

Repository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A transform() 0 6 1
A createMultiple() 0 3 1
A register() 0 4 1
A delete() 0 3 1
A query() 0 3 1
A model() 0 3 1
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