MultipleSender::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 1
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Fenos\Notifynder\Senders;
4
5
use Fenos\Notifynder\Models\Notification;
6
use Fenos\Notifynder\Contracts\SenderContract;
7
use Fenos\Notifynder\Contracts\SenderManagerContract;
8
9
/**
10
 * Class MultipleSender.
11
 */
12
class MultipleSender implements SenderContract
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $notifications;
18
19
    /**
20
     * @var \Illuminate\Database\DatabaseManager
21
     */
22
    protected $database;
23
24
    /**
25
     * MultipleSender constructor.
26
     *
27
     * @param array $notifications
28
     */
29
    public function __construct(array $notifications)
30
    {
31
        $this->notifications = $notifications;
32
        $this->database = app('db');
33
    }
34
35
    /**
36
     * Send all notifications.
37
     *
38
     * @param SenderManagerContract $sender
39
     * @return bool
40
     */
41
    public function send(SenderManagerContract $sender)
42
    {
43
        $model = app('notifynder.resolver.model')->getModel(Notification::class);
44
        $table = (new $model())->getTable();
45
46
        $this->database->beginTransaction();
47
        $stackId = $this->database
48
                ->table($table)
49
                ->max('stack_id') + 1;
50
        foreach ($this->notifications as $key => $notification) {
51
            $this->notifications[$key] = $this->notifications[$key]->toDbArray();
52
            $this->notifications[$key]['stack_id'] = $stackId;
53
        }
54
        $insert = $this->database
55
            ->table($table)
56
            ->insert($this->notifications);
57
        $this->database->commit();
58
59
        return (bool) $insert;
60
    }
61
}
62