DefaultController::options()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 7
c 3
b 0
f 0
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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
16
class DefaultController extends Controller
17
{
18
    /** Target entity name */
19
    public ?string $entityName = null;
20
    /** Entity target id */
21
    public ?string $entityId = null;
22
    /** User id who performed the action */
23
    public ?string $userId = null;
24
    /** Action performed on the object */
25
    public ?string $logAction = null;
26
    /** Environment, which produced the effect */
27
    public ?string $env = null;
28
    /**
29
     * Delete older than
30
     * Valid values:
31
     * 1h - 1 hour
32
     * 2d - 2 days
33
     * 3m - 3 month
34
     * 1y - 1 year
35
     */
36
    public ?string $oldThan = null;
37
38
    private ManagerInterface $logger;
39
40 16
    public function __construct(
41
        string           $id,
42
        Module           $module,
43
        ManagerInterface $logger,
44
        array            $config = []
45
    )
46
    {
47 16
        parent::__construct($id, $module, $config);
48 16
        $this->logger = $logger;
49
    }
50
51 16
    public function options($actionID): array
52
    {
53 16
        return array_merge(parent::options($actionID), [
54 16
            'entityName',
55 16
            'entityId',
56 16
            'userId',
57 16
            'logAction',
58 16
            'env',
59 16
            'oldThan',
60 16
        ]);
61
    }
62
63 8
    public function optionAliases(): array
64
    {
65 8
        return array_merge(parent::optionAliases(), [
66 8
            'o' => 'old-than',
67 8
            'a' => 'log-action',
68 8
            'eid' => 'entity-id',
69 8
            'e' => 'entity-name',
70 8
            'uid' => 'user-id',
71 8
        ]);
72
    }
73
74
    /**
75
     * Clean storage activity log
76
     */
77 16
    public function actionClean(): void
78
    {
79 16
        $oldThan = $this->parseDate($this->oldThan);
80
81 16
        $command = new DeleteCommand([
82 16
            'entityName' => $this->entityName,
83 16
            'entityId' => $this->entityId,
84 16
            'userId' => $this->userId,
85 16
            'action' => $this->logAction,
86 16
            'env' => $this->env,
87 16
            'oldThan' => $oldThan,
88 16
        ]);
89
90 16
        if ($this->logger->delete($command)) {
91 16
            $this->stdout("Successful clearing the logs.\n");
92
        } else {
93 1
            $this->stdout("Error while cleaning the logs.\n");
94
        }
95
    }
96
97 16
    private function parseDate(?string $str): ?int
98
    {
99 16
        if (empty($str)) {
100 9
            return null;
101
        }
102 8
        if (preg_match("/^(\d+)([hdmy]+)$/", $str, $matches)) {
103 7
            [$_, $count, $alias] = $matches;
104 7
            $aliases = [
105 7
                'h' => 'hour',
106 7
                'd' => 'day',
107 7
                'm' => 'month',
108 7
                'y' => 'year',
109 7
            ];
110 7
            return strtotime("-{$count} {$aliases[$alias]} 0:00:00 UTC");
111
        }
112 2
        throw new \InvalidArgumentException("Invalid old-than value: '{$str}'. You can use one of the 1h, 2d, 3m or 4y");
113
    }
114
}
115