Completed
Pull Request — master (#168)
by Deven
02:30
created

CleanOldNotifsShell::main()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0987

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 28
ccs 14
cts 18
cp 0.7778
rs 8.8571
cc 3
eloc 18
nc 3
nop 0
crap 3.0987
1
<?php
2
3
/* vim: set expandtab sw=4 ts=4 sts=4: */
4
/**
5
 * Clean Old Notifications Shell.
6
 *
7
 * phpMyAdmin Error reporting server
8
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
15
 * @license   https://opensource.org/licenses/mit-license.php MIT License
16
 *
17
 * @see      https://www.phpmyadmin.net/
18
 */
19
20
namespace App\Shell;
21
22
use Cake\Console\Shell;
23
use Cake\Log\Log;
24
25
/**
26
 * Clean old Notifications shell.
27
 */
28
class CleanOldNotifsShell extends Shell
29
{
30 1
    public function initialize()
31
    {
32 1
        parent::initialize();
33 1
        $this->loadModel('Notifications');
34 1
    }
35
36 1
    public function main()
37
    {
38 1
        $XTime = time() - 60 * 24 * 3600;
39 1
        $conditions = array('Notifications.created <' => date('Y-m-d H:i:s', $XTime));
40
41 1
        if ($this->Notifications->find('all', array('conditions' => $conditions))->count() === 0) {
42
            // Check if there are any notifications to delete
43 1
            Log::write(
44 1
                'info',
45 1
                'No notifications found for deleting!',
46 1
                'cron_jobs'
47
            );
48 1
        } elseif (!$this->Notifications->deleteAll($conditions)) {
49
            // Try deleting the matched records
50
            Log::write(
51
                'info',
52
                'Old notifications deleted successfully!',
53
                'cron_jobs'
54
            );
55
        } else {
56
            // If NOT successful, print out an error message
57 1
            Log::write(
58 1
                'error',
59 1
                'Deleting old notifications failed!',
60 1
                'cron_jobs'
61
            );
62
        }
63 1
    }
64
}
65