Passed
Push — master ( 06f5a7...6013ae )
by Loban
02:50
created

DefaultController::optionAliases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 0
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
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 16
    public function __construct(
42
        string           $id,
43
        Module           $module,
44
        ManagerInterface $logger,
45
        array            $config = []
46
    )
47
    {
48 16
        parent::__construct($id, $module, $config);
49 16
        $this->logger = $logger;
50
    }
51
52 16
    public function options($actionID): array
53
    {
54 16
        return array_merge(parent::options($actionID), [
55 16
            'entityName',
56 16
            'entityId',
57 16
            'userId',
58 16
            'logAction',
59 16
            'env',
60 16
            'oldThan',
61 16
        ]);
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 16
    public function actionClean(): void
79
    {
80 16
        $oldThan = $this->parseDate($this->oldThan);
0 ignored issues
show
Bug introduced by
It seems like $this->oldThan can also be of type null; however, parameter $str of lav45\activityLogger\con...Controller::parseDate() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

80
        $oldThan = $this->parseDate(/** @scrutinizer ignore-type */ $this->oldThan);
Loading history...
81
82 16
        $command = new DeleteCommand([
83 16
            'entityName' => $this->entityName,
84 16
            'entityId' => $this->entityId,
85 16
            'userId' => $this->userId,
86 16
            'action' => $this->logAction,
87 16
            'env' => $this->env,
88 16
            'oldThan' => $oldThan,
89 16
        ]);
90
91 16
        if ($this->logger->delete($command)) {
92 16
            $this->stdout("Successful clearing the logs.\n");
93
        } else {
94 1
            $this->stdout("Error while cleaning the logs.\n");
95
        }
96
    }
97
98 16
    private function parseDate(string $str): int
99
    {
100 16
        if (preg_match("/^(\d+)([hdmy]+)$/", $str, $matches)) {
101 16
            [$_, $count, $alias] = $matches;
102 16
            $aliases = [
103 16
                'h' => 'hour',
104 16
                'd' => 'day',
105 16
                'm' => 'month',
106 16
                'y' => 'year',
107 16
            ];
108 16
            return strtotime("-{$count} {$aliases[$alias]} 0:00:00 UTC");
109
        }
110 2
        throw new \InvalidArgumentException("Invalid old-than value: '{$str}'. You can use one of the 1h, 2d, 3m or 4y");
111
    }
112
}
113