Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Notifications;
4
 
5
use Illuminate\Contracts\Queue\ShouldQueue;
6
use Illuminate\Contracts\Translation\HasLocalePreference;
7
use Illuminate\Database\Eloquent\Collection as ModelCollection;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Notifications\Events\NotificationSending;
10
use Illuminate\Notifications\Events\NotificationSent;
11
use Illuminate\Support\Collection;
12
use Illuminate\Support\Str;
13
use Illuminate\Support\Traits\Localizable;
14
 
15
class NotificationSender
16
{
17
    use Localizable;
18
 
19
    /**
20
     * The notification manager instance.
21
     *
22
     * @var \Illuminate\Notifications\ChannelManager
23
     */
24
    protected $manager;
25
 
26
    /**
27
     * The Bus dispatcher instance.
28
     *
29
     * @var \Illuminate\Contracts\Bus\Dispatcher
30
     */
31
    protected $bus;
32
 
33
    /**
34
     * The event dispatcher.
35
     *
36
     * @var \Illuminate\Contracts\Events\Dispatcher
37
     */
38
    protected $events;
39
 
40
    /**
41
     * The locale to be used when sending notifications.
42
     *
43
     * @var string|null
44
     */
45
    protected $locale;
46
 
47
    /**
48
     * Create a new notification sender instance.
49
     *
50
     * @param  \Illuminate\Notifications\ChannelManager  $manager
51
     * @param  \Illuminate\Contracts\Bus\Dispatcher  $bus
52
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
53
     * @param  string|null  $locale
54
     * @return void
55
     */
56
    public function __construct($manager, $bus, $events, $locale = null)
57
    {
58
        $this->bus = $bus;
59
        $this->events = $events;
60
        $this->locale = $locale;
61
        $this->manager = $manager;
62
    }
63
 
64
    /**
65
     * Send the given notification to the given notifiable entities.
66
     *
67
     * @param  \Illuminate\Support\Collection|array|mixed  $notifiables
68
     * @param  mixed  $notification
69
     * @return void
70
     */
71
    public function send($notifiables, $notification)
72
    {
73
        $notifiables = $this->formatNotifiables($notifiables);
74
 
75
        if ($notification instanceof ShouldQueue) {
76
            return $this->queueNotification($notifiables, $notification);
77
        }
78
 
79
        $this->sendNow($notifiables, $notification);
80
    }
81
 
82
    /**
83
     * Send the given notification immediately.
84
     *
85
     * @param  \Illuminate\Support\Collection|array|mixed  $notifiables
86
     * @param  mixed  $notification
87
     * @param  array|null  $channels
88
     * @return void
89
     */
90
    public function sendNow($notifiables, $notification, array $channels = null)
91
    {
92
        $notifiables = $this->formatNotifiables($notifiables);
93
 
94
        $original = clone $notification;
95
 
96
        foreach ($notifiables as $notifiable) {
97
            if (empty($viaChannels = $channels ?: $notification->via($notifiable))) {
98
                continue;
99
            }
100
 
101
            $this->withLocale($this->preferredLocale($notifiable, $notification), function () use ($viaChannels, $notifiable, $original) {
102
                $notificationId = Str::uuid()->toString();
103
 
104
                foreach ((array) $viaChannels as $channel) {
105
                    if (! ($notifiable instanceof AnonymousNotifiable && $channel === 'database')) {
106
                        $this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel);
107
                    }
108
                }
109
            });
110
        }
111
    }
112
 
113
    /**
114
     * Get the notifiable's preferred locale for the notification.
115
     *
116
     * @param  mixed  $notifiable
117
     * @param  mixed  $notification
118
     * @return string|null
119
     */
120
    protected function preferredLocale($notifiable, $notification)
121
    {
122
        return $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
123
            if ($notifiable instanceof HasLocalePreference) {
124
                return $notifiable->preferredLocale();
125
            }
126
        });
127
    }
128
 
129
    /**
130
     * Send the given notification to the given notifiable via a channel.
131
     *
132
     * @param  mixed  $notifiable
133
     * @param  string  $id
134
     * @param  mixed  $notification
135
     * @param  string  $channel
136
     * @return void
137
     */
138
    protected function sendToNotifiable($notifiable, $id, $notification, $channel)
139
    {
140
        if (! $notification->id) {
141
            $notification->id = $id;
142
        }
143
 
144
        if (! $this->shouldSendNotification($notifiable, $notification, $channel)) {
145
            return;
146
        }
147
 
148
        $response = $this->manager->driver($channel)->send($notifiable, $notification);
149
 
150
        $this->events->dispatch(
151
            new NotificationSent($notifiable, $notification, $channel, $response)
152
        );
153
    }
154
 
155
    /**
156
     * Determines if the notification can be sent.
157
     *
158
     * @param  mixed  $notifiable
159
     * @param  mixed  $notification
160
     * @param  string  $channel
161
     * @return bool
162
     */
163
    protected function shouldSendNotification($notifiable, $notification, $channel)
164
    {
165
        if (method_exists($notification, 'shouldSend') &&
166
            $notification->shouldSend($notifiable, $channel) === false) {
167
            return false;
168
        }
169
 
170
        return $this->events->until(
171
            new NotificationSending($notifiable, $notification, $channel)
172
        ) !== false;
173
    }
174
 
175
    /**
176
     * Queue the given notification instances.
177
     *
178
     * @param  mixed  $notifiables
179
     * @param  \Illuminate\Notifications\Notification  $notification
180
     * @return void
181
     */
182
    protected function queueNotification($notifiables, $notification)
183
    {
184
        $notifiables = $this->formatNotifiables($notifiables);
185
 
186
        $original = clone $notification;
187
 
188
        foreach ($notifiables as $notifiable) {
189
            $notificationId = Str::uuid()->toString();
190
 
191
            foreach ((array) $original->via($notifiable) as $channel) {
192
                $notification = clone $original;
193
 
194
                if (! $notification->id) {
195
                    $notification->id = $notificationId;
196
                }
197
 
198
                if (! is_null($this->locale)) {
199
                    $notification->locale = $this->locale;
200
                }
201
 
202
                $connection = $notification->connection;
203
 
204
                if (method_exists($notification, 'viaConnections')) {
205
                    $connection = $notification->viaConnections()[$channel] ?? null;
206
                }
207
 
208
                $queue = $notification->queue;
209
 
210
                if (method_exists($notification, 'viaQueues')) {
211
                    $queue = $notification->viaQueues()[$channel] ?? null;
212
                }
213
 
214
                $delay = $notification->delay;
215
 
216
                if (method_exists($notification, 'withDelay')) {
217
                    $delay = $notification->withDelay($notifiable, $channel) ?? null;
218
                }
219
 
220
                $middleware = $notification->middleware ?? [];
221
 
222
                if (method_exists($notification, 'middleware')) {
223
                    $middleware = array_merge(
224
                        $notification->middleware($notifiable, $channel),
225
                        $middleware
226
                    );
227
                }
228
 
229
                $this->bus->dispatch(
230
                    (new SendQueuedNotifications($notifiable, $notification, [$channel]))
231
                            ->onConnection($connection)
232
                            ->onQueue($queue)
233
                            ->delay(is_array($delay) ? ($delay[$channel] ?? null) : $delay)
234
                            ->through($middleware)
235
                );
236
            }
237
        }
238
    }
239
 
240
    /**
241
     * Format the notifiables into a Collection / array if necessary.
242
     *
243
     * @param  mixed  $notifiables
244
     * @return \Illuminate\Database\Eloquent\Collection|array
245
     */
246
    protected function formatNotifiables($notifiables)
247
    {
248
        if (! $notifiables instanceof Collection && ! is_array($notifiables)) {
249
            return $notifiables instanceof Model
250
                            ? new ModelCollection([$notifiables]) : [$notifiables];
251
        }
252
 
253
        return $notifiables;
254
    }
255
}