1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/lav45/yii2-activity-logger |
4
|
|
|
* @copyright Copyright (c) 2017 LAV45 |
5
|
|
|
* @author Aleksey Loban <[email protected]> |
6
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace lav45\activityLogger\console; |
10
|
|
|
|
11
|
|
|
use lav45\activityLogger\ManagerInterface; |
12
|
|
|
use lav45\activityLogger\storage\DeleteCommand; |
13
|
|
|
use yii\base\Module; |
14
|
|
|
use yii\console\Controller; |
15
|
|
|
use yii\helpers\Console; |
16
|
|
|
|
17
|
|
|
class DefaultController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** Target entity name */ |
20
|
|
|
public ?string $entityName = null; |
21
|
|
|
/** Entity target id */ |
22
|
|
|
public ?string $entityId = null; |
23
|
|
|
/** User id who performed the action */ |
24
|
|
|
public ?string $userId = null; |
25
|
|
|
/** Action performed on the object */ |
26
|
|
|
public ?string $logAction = null; |
27
|
|
|
/** Environment, which produced the effect */ |
28
|
|
|
public ?string $env = null; |
29
|
|
|
/** |
30
|
|
|
* Delete older than |
31
|
|
|
* Valid values: |
32
|
|
|
* 1h - 1 hour |
33
|
|
|
* 2d - 2 days |
34
|
|
|
* 3m - 3 month |
35
|
|
|
* 1y - 1 year |
36
|
|
|
*/ |
37
|
|
|
public ?string $oldThan = '1y'; |
38
|
|
|
|
39
|
|
|
private ManagerInterface $logger; |
40
|
|
|
|
41
|
15 |
|
public function __construct( |
42
|
|
|
string $id, |
43
|
|
|
Module $module, |
44
|
|
|
ManagerInterface $logger, |
45
|
|
|
array $config = [] |
46
|
|
|
) |
47
|
|
|
{ |
48
|
15 |
|
parent::__construct($id, $module, $config); |
49
|
15 |
|
$this->logger = $logger; |
50
|
|
|
} |
51
|
|
|
|
52
|
15 |
|
public function options($actionID): array |
53
|
|
|
{ |
54
|
15 |
|
return array_merge(parent::options($actionID), [ |
55
|
15 |
|
'entityName', |
56
|
15 |
|
'entityId', |
57
|
15 |
|
'userId', |
58
|
15 |
|
'logAction', |
59
|
15 |
|
'env', |
60
|
15 |
|
'oldThan', |
61
|
15 |
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
8 |
|
public function optionAliases(): array |
65
|
|
|
{ |
66
|
8 |
|
return array_merge(parent::optionAliases(), [ |
67
|
8 |
|
'o' => 'old-than', |
68
|
8 |
|
'a' => 'log-action', |
69
|
8 |
|
'eid' => 'entity-id', |
70
|
8 |
|
'e' => 'entity-name', |
71
|
8 |
|
'uid' => 'user-id', |
72
|
8 |
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Clean storage activity log |
77
|
|
|
*/ |
78
|
15 |
|
public function actionClean(): void |
79
|
|
|
{ |
80
|
15 |
|
$oldThan = $this->parseDate($this->oldThan); |
|
|
|
|
81
|
15 |
|
if (null === $oldThan) { |
82
|
1 |
|
$this->stderr("Invalid date format\n", Console::FG_RED, Console::UNDERLINE); |
83
|
1 |
|
return; |
84
|
|
|
} |
85
|
|
|
|
86
|
15 |
|
$command = new DeleteCommand([ |
87
|
15 |
|
'entityName' => $this->entityName, |
88
|
15 |
|
'entityId' => $this->entityId, |
89
|
15 |
|
'userId' => $this->userId, |
90
|
15 |
|
'action' => $this->logAction, |
91
|
15 |
|
'env' => $this->env, |
92
|
15 |
|
'oldThan' => $oldThan, |
93
|
15 |
|
]); |
94
|
|
|
|
95
|
15 |
|
if ($this->logger->delete($command)) { |
96
|
15 |
|
$this->stdout("Successful clearing the logs.\n"); |
97
|
|
|
} else { |
98
|
1 |
|
$this->stdout("Error while cleaning the logs.\n"); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
15 |
|
private function parseDate(string $str): ?int |
103
|
|
|
{ |
104
|
15 |
|
if (preg_match("/^(\d+)([hdmy]+)$/", $str, $matches)) { |
105
|
15 |
|
[$_, $count, $alias] = $matches; |
106
|
15 |
|
$aliases = [ |
107
|
15 |
|
'h' => 'hour', |
108
|
15 |
|
'd' => 'day', |
109
|
15 |
|
'm' => 'month', |
110
|
15 |
|
'y' => 'year', |
111
|
15 |
|
]; |
112
|
15 |
|
if (isset($aliases[$alias])) { |
113
|
15 |
|
return strtotime("-{$count} {$aliases[$alias]} 0:00:00 UTC"); |
114
|
|
|
} |
115
|
|
|
throw new \InvalidArgumentException("Invalid date alias: {$alias}. You can use one of the 1h, 2d, 3m or 4y"); |
116
|
|
|
} |
117
|
1 |
|
return null; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|