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
|
|
|
|