Passed
Push — master ( 1877a1...37d1bb )
by Gabriel
10:54
created

Dispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ByTIC\Notifications\Dispatcher;
4
5
use ByTIC\Notifications\ChannelManager;
6
use Nip\Collections\Collection;
7
use Nip\Records\AbstractModels\Record;
8
9
/**
10
 * Class NotificationSender
11
 * Used to send a notification
12
 *
13
 * @package ByTIC\Notifications\Dispatcher
14
 */
15
class Dispatcher implements DispatcherInterface
16
{
17
18
    /**
19
     * The notification manager instance.
20
     *
21
     * @var ChannelManager
22
     */
23
    protected $manager;
24
25
    /**
26
     * Create a new notification sender instance.
27
     *
28
     * @param ChannelManager $manager
29
     */
30
    public function __construct($manager)
31
    {
32
        $this->manager = $manager;
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function send($notifiables, $notification)
39
    {
40
        $this->sendNow($notifiables, $notification);
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function sendNow($notifiables, $notification, array $channels = null)
47
    {
48
        $notifiables = $this->formatNotifiables($notifiables);
49
        $original = clone $notification;
50
        foreach ($notifiables as $notifiable) {
51
            $notificationId = microtime();
52
53
            if (empty($viaChannels = $channels ?: $notification->via($notifiable))) {
54
                continue;
55
            }
56
57
            foreach ($viaChannels as $channel) {
58
                $this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel);
59
            }
60
        }
61
    }
62
63
    /**
64
     * Format the notifiables into a Collection / array if necessary.
65
     *
66
     * @param  mixed $notifiables
67
     *
68
     * @return Collection|array
69
     */
70
    public function formatNotifiables($notifiables)
71
    {
72
        if (is_array($notifiables) || $notifiables instanceof Collection) {
73
            return $notifiables;
74
        }
75
76
        if ($notifiables instanceof Record) {
77
            return new Collection([$notifiables]);
78
        }
79
80
        return [$notifiables];
81
    }
82
83
    /**
84
     * Send the given notification to the given notifiable via a channel.
85
     *
86
     * @param  mixed $notifiable
87
     * @param  string $id
88
     * @param  mixed $notification
89
     * @param  string $channel
90
     *
91
     * @return int
92
     */
93
    protected function sendToNotifiable($notifiable, $id, $notification, $channel)
94
    {
95
        if (!$notification->id) {
96
            $notification->id = $id;
97
        }
98
//        if (! $this->shouldSendNotification($notifiable, $notification, $channel)) {
99
//            return;
100
//        }
101
        $response = $this->manager->channel($channel)->send($notifiable, $notification);
102
103
//        $this->events->dispatch(
104
//            new Events\NotificationSent($notifiable, $notification, $channel, $response)
105
//        );
106
        return $response;
107
    }
108
}
109