NotificationsTable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 69
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 5 1
A addNotifications() 0 19 3
1
<?php
2
3
/**
4
 * Notification model representing a notification for new report.
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\Model\Table;
20
21
use Cake\ORM\Table;
22
use Cake\ORM\TableRegistry;
23
use InvalidArgumentException;
24
25
use function date;
26
use function is_int;
27
use function time;
28
29
/**
30
 * Notification Model.
31
 *
32
 * @property Developer $Developer
33
 * @property Report    $Report
34
 */
35
class NotificationsTable extends Table
36
{
37
    /**
38
     * Validation rules.
39
     *
40
     * @var array
41
     */
42
    public $validate = [
43
        'developer_id' => [
44
            'numeric' => [
45
                'rule' => ['numeric'],
46
                'allowEmpty' => false,
47
                'required' => true,
48
            ],
49
        ],
50
        'report_id' => [
51
            'numeric' => [
52
                'rule' => ['numeric'],
53
                'allowEmpty' => false,
54
                'required' => true,
55
            ],
56
        ],
57
    ];
58
59
    /**
60
     * The Associations below have been created with all possible keys,
61
     * those that are not needed can be removed.
62
     */
63
64
    /**
65
     * belongsTo associations.
66
     *
67
     * @param array $config Config array
68
     * @return void Nothing
69
     */
70 175
    public function initialize(array $config): void
71
    {
72 175
        $this->belongsTo('Reports', [
73 175
            'className' => 'Reports',
74
            'foreignKey' => 'report_id',
75
        ]);
76 175
    }
77
78
    /**
79
     * To Add Multiple Notifications for New report.
80
     *
81
     * @param int $report_id id of the new Report
82
     *
83
     * @return bool|object value. True on success. False on any type of failure.
84
     */
85 21
    public static function addNotifications(int $report_id)
86
    {
87 21
        if (! is_int($report_id)) {
88
            throw new InvalidArgumentException('Invalid Argument "$report_id"! Integer Expected.');
89
        }
90
91 21
        $devs = TableRegistry::getTableLocator()->get('Developers')->find('all');
92 21
        $notoficationTable = TableRegistry::getTableLocator()->get('Notifications');
93 21
        $res = true;
94 21
        foreach ($devs as $dev) {
95 21
            $notification = $notoficationTable->newEmptyEntity();
96 21
            $notification->developer_id = $dev['id'];
97 21
            $notification->report_id = $report_id;
98 21
            $notification->created = date('Y-m-d H:i:s', time());
99 21
            $notification->modified = date('Y-m-d H:i:s', time());
100 21
            $res = $notoficationTable->save($notification);
101
        }
102
103 21
        return $res;
104
    }
105
}
106