Completed
Push — master ( 2f25c9...4c0020 )
by
unknown
06:10
created

MultipleSender::send()   A

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\Contracts\SenderContract;
6
use Fenos\Notifynder\Contracts\SenderManagerContract;
7
8
/**
9
 * Class MultipleSender.
10
 */
11
class MultipleSender implements SenderContract
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $notifications;
17
18
    /**
19
     * @var \Illuminate\Database\DatabaseManager
20
     */
21
    protected $database;
22
23
    /**
24
     * MultipleSender constructor.
25
     *
26
     * @param array $notifications
27
     */
28
    public function __construct(array $notifications)
29
    {
30
        $this->notifications = $notifications;
31
        $this->database = app('db');
32
    }
33
34
    /**
35
     * Send all notifications.
36
     *
37
     * @param SenderManagerContract $sender
38
     * @return bool
39
     */
40
    public function send(SenderManagerContract $sender)
41
    {
42
        $model = notifynder_config()->getNotificationModel();
43
        $table = (new $model())->getTable();
44
45
        $this->database->beginTransaction();
46
        $stackId = $this->database
47
                ->table($table)
48
                ->max('stack_id') + 1;
49
        foreach ($this->notifications as $key => $notification) {
50
            $this->notifications[$key] = $this->notifications[$key]->toDbArray();
51
            $this->notifications[$key]['stack_id'] = $stackId;
52
        }
53
        $insert = $this->database
54
            ->table($table)
55
            ->insert($this->notifications);
56
        $this->database->commit();
57
58
        return (bool) $insert;
59
    }
60
}
61