UpdateCacheAction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 19
c 2
b 0
f 0
dl 0
loc 50
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A updateAll() 0 9 2
A run() 0 13 4
A update() 0 12 2
1
<?php
2
3
namespace Mostafaznv\LaraCache\Actions;
4
5
use Mostafaznv\LaraCache\Actions\Support\UpdateDeleteCache;
6
use Mostafaznv\LaraCache\DTOs\CommandData;
7
8
class UpdateCacheAction extends UpdateDeleteCache
9
{
10
    public function run(CommandData $data): void
11
    {
12
        if (count($data->models) > 1) {
13
            foreach ($data->models as $model) {
14
                $this->updateAll($model);
15
            }
16
        }
17
        else {
18
            $model = $data->models[0];
19
20
            empty($data->entities)
21
                ? $this->updateAll($model)
22
                : $this->update($model, $data->entities);
23
        }
24
    }
25
26
    /**
27
     * @param \Mostafaznv\LaraCache\Traits\LaraCache $model
28
     * @return void
29
     */
30
    private function updateAll(string $model): void
31
    {
32
        $entities = [];
33
34
        foreach ($model::cacheEntities() as $entity) {
35
            $entities[] = $entity->name;
36
        }
37
38
        $this->update($model, $entities);
39
    }
40
41
    /**
42
     * @param \Mostafaznv\LaraCache\Traits\LaraCache $model
43
     * @param array $entities
44
     * @return void
45
     */
46
    private function update(string $model, array $entities): void
47
    {
48
        $this->console?->warn(
49
            sprintf('>> Updating cache entities in [%s] model', class_basename($model))
50
        );
51
52
        foreach ($entities as $entity) {
53
            $this->console?->line('— ' . $this->title($entity));
54
55
            $model::cache()->update($entity);
56
57
            $this->console?->info('Updated');
58
        }
59
    }
60
}
61