CleanOldNotifsShell   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 84.62%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 54
ccs 22
cts 26
cp 0.8462
rs 10
c 2
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 4 1
A defaultName() 0 3 1
A buildOptionParser() 0 5 1
A execute() 0 25 3
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 App\Model\Table\NotificationsTable;
22
use Cake\Command\Command;
23
use Cake\Console\Arguments;
24
use Cake\Console\ConsoleIo;
25
use Cake\Console\ConsoleOptionParser;
26
use Cake\Log\Log;
27
28
use function date;
29
use function time;
30
31
/**
32
 * Clean old Notifications shell.
33
 *
34
 * @property NotificationsTable $Notifications
35
 */
36
class CleanOldNotifsShell extends Command
37
{
38
    protected const NAME = 'clean_old_notifs';
39
40
    /**
41
     * The name of this command.
42
     *
43
     * @var string
44
     */
45
    protected $name = self::NAME;
46
47 21
    public static function defaultName(): string
48
    {
49 21
        return self::NAME;
50
    }
51
52 7
    protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
53
    {
54
        return $parser
55 7
            ->setCommand($this->name)
56 7
            ->setDescription('Clean old notifications');
57
    }
58
59 7
    public function initialize(): void
60
    {
61 7
        parent::initialize();
62 7
        $this->loadModel('Notifications');
63 7
    }
64
65 7
    public function execute(Arguments $args, ConsoleIo $io)
66
    {
67 7
        $XTime = time() - 60 * 24 * 3600;
68 7
        $conditions = ['Notifications.created <' => date('Y-m-d H:i:s', $XTime)];
69
70 7
        if ($this->Notifications->find('all', ['conditions' => $conditions])->count() === 0) {
71
            // Check if there are any notifications to delete
72 7
            Log::write(
73 7
                'info',
74 7
                'No notifications found for deleting!',
75 7
                'cron_jobs'
76
            );
77 7
        } elseif ($this->Notifications->deleteAll($conditions)) {
78
            // Try deleting the matched records
79 7
            Log::write(
80 7
                'info',
81 7
                'Old notifications deleted successfully!',
82 7
                'cron_jobs'
83
            );
84
        } else {
85
            // If NOT successful, print out an error message
86
            Log::write(
87
                'error',
88
                'Deleting old notifications failed!',
89
                'cron_jobs'
90
            );
91
        }
92 7
    }
93
}
94