Passed
Push — master ( 083ff7...c84473 )
by William
03:02
created

CleanOldNotifsShell::buildOptionParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Clean Old Notifications Shell.
5
 *
6
 * phpMyAdmin Error reporting server
7
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
14
 * @license   https://opensource.org/licenses/mit-license.php MIT License
15
 *
16
 * @see      https://www.phpmyadmin.net/
17
 */
18
19
namespace App\Shell;
20
21
use Cake\Command\Command;
22
use Cake\Console\Arguments;
23
use Cake\Console\ConsoleIo;
24
use Cake\Console\ConsoleOptionParser;
25
use Cake\Log\Log;
26
use function date;
27
use function time;
28
use App\Model\Table\NotificationsTable;
29
30
/**
31
 * Clean old Notifications shell.
32
 *
33
 * @property NotificationsTable $Notifications
34
 */
35
class CleanOldNotifsShell extends Command
36
{
37
    protected const NAME = 'clean_old_notifs';
38
39
    /**
40
     * The name of this command.
41
     *
42
     * @var string
43
     */
44
    protected $name = self::NAME;
45
46 21
    public static function defaultName(): string
47
    {
48 21
        return self::NAME;
49
    }
50
51 7
    protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
52
    {
53
        return $parser
54 7
            ->setCommand($this->name)
55 7
            ->setDescription('Clean old notifications');
56
    }
57
58 7
    public function initialize(): void
59
    {
60 7
        parent::initialize();
61 7
        $this->loadModel('Notifications');
62 7
    }
63
64 7
    public function execute(Arguments $args, ConsoleIo $io)
65
    {
66 7
        $XTime = time() - 60 * 24 * 3600;
67 7
        $conditions = ['Notifications.created <' => date('Y-m-d H:i:s', $XTime)];
68
69 7
        if ($this->Notifications->find('all', ['conditions' => $conditions])->count() === 0) {
70
            // Check if there are any notifications to delete
71 7
            Log::write(
72 7
                'info',
73 7
                'No notifications found for deleting!',
74 7
                'cron_jobs'
75
            );
76 7
        } elseif ($this->Notifications->deleteAll($conditions)) {
77
            // Try deleting the matched records
78 7
            Log::write(
79 7
                'info',
80 7
                'Old notifications deleted successfully!',
81 7
                'cron_jobs'
82
            );
83
        } else {
84
            // If NOT successful, print out an error message
85
            Log::write(
86
                'error',
87
                'Deleting old notifications failed!',
88
                'cron_jobs'
89
            );
90
        }
91 7
    }
92
}
93