Passed
Push — master ( 652acd...8b321d )
by Mostafa
13:28 queued 10:29
created

UpdateDeleteCache   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 23
c 1
b 0
f 0
dl 0
loc 57
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A group() 0 20 4
A make() 0 3 1
A title() 0 6 1
A makeCommandDataFromGroupItem() 0 10 4
1
<?php
2
3
namespace Mostafaznv\LaraCache\Actions\Support;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\Command;
7
use Mostafaznv\LaraCache\DTOs\CommandData;
8
use Mostafaznv\LaraCache\Exceptions\CacheGroupNotExist;
9
use Mostafaznv\LaraCache\Exceptions\CacheGroupValueIsNotValid;
10
11
abstract class UpdateDeleteCache
12
{
13
    public function __construct(protected ?Command $console) {}
14
15
    public static function make(?Command $console = null): self
16
    {
17
        return new static($console);
18
    }
19
20
21
    protected function title(string $string): string
22
    {
23
        return Str::title(
24
            Str::slug(
25
                title: Str::replace(['.', '-', '_'], ' ', $string),
26
                separator: ' '
27
            )
28
        );
29
    }
30
31
    /**
32
     * @param string $group
33
     * @return CommandData[]
34
     */
35
    protected function group(string $group): array
36
    {
37
        $groupName = $group;
38
        $group = config("laracache.groups.$group");
39
40
        if (is_null($group)) {
41
            throw CacheGroupNotExist::make($groupName);
42
        }
43
44
        if (is_array($group)) {
45
            $data = [];
46
47
            foreach ($group as $item) {
48
                $data[] = $this->makeCommandDataFromGroupItem($item, $groupName);
49
            }
50
51
            return $data;
52
        }
53
        else {
54
            throw CacheGroupValueIsNotValid::make($groupName);
55
        }
56
    }
57
58
    private function makeCommandDataFromGroupItem(mixed $item, string $groupName): CommandData
59
    {
60
        if (isset($item['model']) and is_string($item['model']) and isset($item['entities'])) {
61
            return CommandData::make(
62
                models: [$item['model']],
63
                entities: $item['entities']
64
            );
65
        }
66
        else {
67
            throw CacheGroupValueIsNotValid::make($groupName);
68
        }
69
    }
70
}
71