Test Failed
Push — master ( 7bbbf4...34ff55 )
by Julien
04:57
created

DataLifeCycleTask   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
c 1
b 0
f 0
dl 0
loc 110
ccs 0
cts 47
cp 0
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mainAction() 0 8 1
A initialize() 0 9 1
A addModelsPermissions() 0 14 3
B modelsAction() 0 53 7
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Modules\Cli\Tasks;
13
14
use Phalcon\Mvc\Model\ResultsetInterface;
15
use Phalcon\Mvc\ModelInterface;
16
use Zemit\Exception\CliException;
17
use Zemit\Models\Lang;
18
use Zemit\Models\Site;
19
use Zemit\Modules\Cli\Task;
20
use Zemit\Models\Role;
21
use Zemit\Models\Setting;
22
use Zemit\Models\Template;
23
use Zemit\Mvc\Model;
24
use Zemit\Utils;
25
26
class DataLifeCycleTask extends Task
27
{
28
    public string $cliDoc = <<<DOC
29
Usage:
30
  php zemit cli data-life-cycle <action> [<params> ...]
31
32
Options:
33
  task: cache
34
  action: clear
35
36
37
DOC;
38
    
39
    public ?array $models = null;
40
    
41
    public ?array $policies = null;
42
    
43
    public function initialize(): void
44
    {
45
        Utils::setUnlimitedRuntime();
46
        
47
        $dataLifeCycleConfig = $this->config->pathToArray('dataLifeCycle');
48
        $this->models ??= $dataLifeCycleConfig['models'] ?? [];
49
        $this->policies ??= $dataLifeCycleConfig['policies'] ?? [];
50
        
51
        $this->addModelsPermissions();
52
    }
53
    
54
    /**
55
     * Default action
56
     */
57
    public function mainAction(string ...$tables): ?array
58
    {
59
        $response = [];
60
61
//        $response ['backup'] = $this->backupAction();
62
        $response ['models'] = $this->modelsAction(...$tables);
63
        
64
        return $response;
65
    }
66
    
67
    public function modelsAction(string ...$tables): array
68
    {
69
        $response = [];
70
        
71
        foreach ($tables as &$table) {
72
            $table = array_map('trim', explode(',', $table));
73
        }
74
        $tables = array_flip(array_merge_recursive(...$tables));
0 ignored issues
show
Bug introduced by
$tables of type string is incompatible with the type array expected by parameter $arrays of array_merge_recursive(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $tables = array_flip(array_merge_recursive(/** @scrutinizer ignore-type */ ...$tables));
Loading history...
75
        
76
        foreach ($this->models as $modelClass => $policyName) {
77
            
78
            // retrieve configured model the policy
79
            $policy = $this->policies[$policyName] ?? null;
80
            
81
            // load an instance of the model class
82
            $model = $this->modelsManager->load($modelClass);
83
            assert($model instanceof Model);
84
            $source = $model->getSource();
85
            
86
            // whitelisted tables
87
            if (!empty($tables) && !isset($tables[$source])) {
88
                continue;
89
            }
90
            
91
            if (!isset($response[$source])) {
92
                $response[$source] = [];
93
            }
94
            
95
            // find all record matching the defined retention policy
96
            $records = $model::findLifeCycle($policy['query'] ?? null);
97
            assert($records instanceof ResultsetInterface);
98
            
99
            // temporarily disable soft-delete
100
            $model->disableSoftDelete();
101
            
102
            foreach ($records as $record) {
103
                assert($record instanceof Model);
104
                
105
                // disable soft delete
106
                $record->disableSoftDelete();
107
                
108
                // delete record
109
                $response[$source] []= [
110
                    'primaryKeys' => $record->getPrimaryKeysValues(),
111
                    'deleted' => $record->delete(),
112
                ];
113
            }
114
            
115
            // re-enable soft-delete
116
            $model->enableSoftDelete();
117
        }
118
        
119
        return $response;
120
    }
121
    
122
    public function addModelsPermissions(?array $models = null): void
123
    {
124
        $permissions = [];
125
        $models ??= $this->models;
126
        if ($models) {
127
            foreach ($models as $model => $entity) {
128
                $permissions[$model] = ['*'];
129
            }
130
        }
131
        $this->config->merge([
132
            'permissions' => [
133
                'roles' => [
134
                    'cli' => [
135
                        'models' => $permissions,
136
                    ],
137
                ],
138
            ],
139
        ]);
140
    }
141
}
142