Completed
Push — master ( 07e3a8...9561cd )
by William
16:58 queued 14:23
created

NotificationsTable::addNotifications()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 18
ccs 13
cts 14
cp 0.9286
crap 3.0032
rs 9.8333
c 0
b 0
f 0
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
use function date;
25
use function is_int;
26
use function time;
27
28
/**
29
 * Notification Model.
30
 *
31
 * @property Developer $Developer
32
 * @property Report    $Report
33
 */
34
class NotificationsTable extends Table
35
{
36
    /**
37
     * Validation rules.
38
     *
39
     * @var array
40
     */
41
    public $validate = [
42
        'developer_id' => [
43
            'numeric' => [
44
                'rule' => ['numeric'],
45
                'allowEmpty' => false,
46
                'required' => true,
47
            ],
48
        ],
49
        'report_id' => [
50
            'numeric' => [
51
                'rule' => ['numeric'],
52
                'allowEmpty' => false,
53
                'required' => true,
54
            ],
55
        ],
56
    ];
57
58
    /**
59
     * The Associations below have been created with all possible keys,
60
     * those that are not needed can be removed.
61
     */
62
63
    /**
64
     * belongsTo associations.
65
     *
66
     * @param array $config Config array
67
     * @return void Nothing
68
     */
69 84
    public function initialize(array $config): void
70
    {
71 84
        $this->belongsTo('Reports', [
72 84
            'className' => 'Reports',
73
            'foreignKey' => 'report_id',
74
        ]);
75 84
    }
76
77
    /**
78
     * To Add Multiple Notifications for New report.
79
     *
80
     * @param int $report_id id of the new Report
81
     *
82
     * @return bool|object value. True on success. False on any type of failure.
83
     */
84 12
    public static function addNotifications(int $report_id)
85
    {
86 12
        if (! is_int($report_id)) {
0 ignored issues
show
introduced by
The condition is_int($report_id) is always true.
Loading history...
87
            throw new InvalidArgumentException('Invalid Argument "$report_id"! Integer Expected.');
88
        }
89 12
        $devs = TableRegistry::getTableLocator()->get('Developers')->find('all');
90 12
        $notoficationTable = TableRegistry::getTableLocator()->get('Notifications');
91 12
        $res = true;
92 12
        foreach ($devs as $dev) {
93 12
            $notification = $notoficationTable->newEntity();
94 12
            $notification->developer_id = $dev['id'];
95 12
            $notification->report_id = $report_id;
96 12
            $notification->created = date('Y-m-d H:i:s', time());
97 12
            $notification->modified = date('Y-m-d H:i:s', time());
98 12
            $res = $notoficationTable->save($notification);
99
        }
100
101 12
        return $res;
102
    }
103
}
104