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\Channels;
4
 
5
use Illuminate\Contracts\Mail\Factory as MailFactory;
6
use Illuminate\Contracts\Mail\Mailable;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Mail\Markdown;
9
use Illuminate\Notifications\Notification;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\Str;
12
use Symfony\Component\Mailer\Header\MetadataHeader;
13
use Symfony\Component\Mailer\Header\TagHeader;
14
 
15
class MailChannel
16
{
17
    /**
18
     * The mailer implementation.
19
     *
20
     * @var \Illuminate\Contracts\Mail\Factory
21
     */
22
    protected $mailer;
23
 
24
    /**
25
     * The markdown implementation.
26
     *
27
     * @var \Illuminate\Mail\Markdown
28
     */
29
    protected $markdown;
30
 
31
    /**
32
     * Create a new mail channel instance.
33
     *
34
     * @param  \Illuminate\Contracts\Mail\Factory  $mailer
35
     * @param  \Illuminate\Mail\Markdown  $markdown
36
     * @return void
37
     */
38
    public function __construct(MailFactory $mailer, Markdown $markdown)
39
    {
40
        $this->mailer = $mailer;
41
        $this->markdown = $markdown;
42
    }
43
 
44
    /**
45
     * Send the given notification.
46
     *
47
     * @param  mixed  $notifiable
48
     * @param  \Illuminate\Notifications\Notification  $notification
49
     * @return void
50
     */
51
    public function send($notifiable, Notification $notification)
52
    {
53
        $message = $notification->toMail($notifiable);
54
 
55
        if (! $notifiable->routeNotificationFor('mail', $notification) &&
56
            ! $message instanceof Mailable) {
57
            return;
58
        }
59
 
60
        if ($message instanceof Mailable) {
61
            return $message->send($this->mailer);
62
        }
63
 
64
        $this->mailer->mailer($message->mailer ?? null)->send(
65
            $this->buildView($message),
66
            array_merge($message->data(), $this->additionalMessageData($notification)),
67
            $this->messageBuilder($notifiable, $notification, $message)
68
        );
69
    }
70
 
71
    /**
72
     * Get the mailer Closure for the message.
73
     *
74
     * @param  mixed  $notifiable
75
     * @param  \Illuminate\Notifications\Notification  $notification
76
     * @param  \Illuminate\Notifications\Messages\MailMessage  $message
77
     * @return \Closure
78
     */
79
    protected function messageBuilder($notifiable, $notification, $message)
80
    {
81
        return function ($mailMessage) use ($notifiable, $notification, $message) {
82
            $this->buildMessage($mailMessage, $notifiable, $notification, $message);
83
        };
84
    }
85
 
86
    /**
87
     * Build the notification's view.
88
     *
89
     * @param  \Illuminate\Notifications\Messages\MailMessage  $message
90
     * @return string|array
91
     */
92
    protected function buildView($message)
93
    {
94
        if ($message->view) {
95
            return $message->view;
96
        }
97
 
98
        if (property_exists($message, 'theme') && ! is_null($message->theme)) {
99
            $this->markdown->theme($message->theme);
100
        }
101
 
102
        return [
103
            'html' => $this->markdown->render($message->markdown, $message->data()),
104
            'text' => $this->markdown->renderText($message->markdown, $message->data()),
105
        ];
106
    }
107
 
108
    /**
109
     * Get additional meta-data to pass along with the view data.
110
     *
111
     * @param  \Illuminate\Notifications\Notification  $notification
112
     * @return array
113
     */
114
    protected function additionalMessageData($notification)
115
    {
116
        return [
117
            '__laravel_notification_id' => $notification->id,
118
            '__laravel_notification' => get_class($notification),
119
            '__laravel_notification_queued' => in_array(
120
                ShouldQueue::class,
121
                class_implements($notification)
122
            ),
123
        ];
124
    }
125
 
126
    /**
127
     * Build the mail message.
128
     *
129
     * @param  \Illuminate\Mail\Message  $mailMessage
130
     * @param  mixed  $notifiable
131
     * @param  \Illuminate\Notifications\Notification  $notification
132
     * @param  \Illuminate\Notifications\Messages\MailMessage  $message
133
     * @return void
134
     */
135
    protected function buildMessage($mailMessage, $notifiable, $notification, $message)
136
    {
137
        $this->addressMessage($mailMessage, $notifiable, $notification, $message);
138
 
139
        $mailMessage->subject($message->subject ?: Str::title(
140
            Str::snake(class_basename($notification), ' ')
141
        ));
142
 
143
        $this->addAttachments($mailMessage, $message);
144
 
145
        if (! is_null($message->priority)) {
146
            $mailMessage->priority($message->priority);
147
        }
148
 
149
        if ($message->tags) {
150
            foreach ($message->tags as $tag) {
151
                $mailMessage->getHeaders()->add(new TagHeader($tag));
152
            }
153
        }
154
 
155
        if ($message->metadata) {
156
            foreach ($message->metadata as $key => $value) {
157
                $mailMessage->getHeaders()->add(new MetadataHeader($key, $value));
158
            }
159
        }
160
 
161
        $this->runCallbacks($mailMessage, $message);
162
    }
163
 
164
    /**
165
     * Address the mail message.
166
     *
167
     * @param  \Illuminate\Mail\Message  $mailMessage
168
     * @param  mixed  $notifiable
169
     * @param  \Illuminate\Notifications\Notification  $notification
170
     * @param  \Illuminate\Notifications\Messages\MailMessage  $message
171
     * @return void
172
     */
173
    protected function addressMessage($mailMessage, $notifiable, $notification, $message)
174
    {
175
        $this->addSender($mailMessage, $message);
176
 
177
        $mailMessage->to($this->getRecipients($notifiable, $notification, $message));
178
 
179
        if (! empty($message->cc)) {
180
            foreach ($message->cc as $cc) {
181
                $mailMessage->cc($cc[0], Arr::get($cc, 1));
182
            }
183
        }
184
 
185
        if (! empty($message->bcc)) {
186
            foreach ($message->bcc as $bcc) {
187
                $mailMessage->bcc($bcc[0], Arr::get($bcc, 1));
188
            }
189
        }
190
    }
191
 
192
    /**
193
     * Add the "from" and "reply to" addresses to the message.
194
     *
195
     * @param  \Illuminate\Mail\Message  $mailMessage
196
     * @param  \Illuminate\Notifications\Messages\MailMessage  $message
197
     * @return void
198
     */
199
    protected function addSender($mailMessage, $message)
200
    {
201
        if (! empty($message->from)) {
202
            $mailMessage->from($message->from[0], Arr::get($message->from, 1));
203
        }
204
 
205
        if (! empty($message->replyTo)) {
206
            foreach ($message->replyTo as $replyTo) {
207
                $mailMessage->replyTo($replyTo[0], Arr::get($replyTo, 1));
208
            }
209
        }
210
    }
211
 
212
    /**
213
     * Get the recipients of the given message.
214
     *
215
     * @param  mixed  $notifiable
216
     * @param  \Illuminate\Notifications\Notification  $notification
217
     * @param  \Illuminate\Notifications\Messages\MailMessage  $message
218
     * @return mixed
219
     */
220
    protected function getRecipients($notifiable, $notification, $message)
221
    {
222
        if (is_string($recipients = $notifiable->routeNotificationFor('mail', $notification))) {
223
            $recipients = [$recipients];
224
        }
225
 
226
        return collect($recipients)->mapWithKeys(function ($recipient, $email) {
227
            return is_numeric($email)
228
                    ? [$email => (is_string($recipient) ? $recipient : $recipient->email)]
229
                    : [$email => $recipient];
230
        })->all();
231
    }
232
 
233
    /**
234
     * Add the attachments to the message.
235
     *
236
     * @param  \Illuminate\Mail\Message  $mailMessage
237
     * @param  \Illuminate\Notifications\Messages\MailMessage  $message
238
     * @return void
239
     */
240
    protected function addAttachments($mailMessage, $message)
241
    {
242
        foreach ($message->attachments as $attachment) {
243
            $mailMessage->attach($attachment['file'], $attachment['options']);
244
        }
245
 
246
        foreach ($message->rawAttachments as $attachment) {
247
            $mailMessage->attachData($attachment['data'], $attachment['name'], $attachment['options']);
248
        }
249
    }
250
 
251
    /**
252
     * Run the callbacks for the message.
253
     *
254
     * @param  \Illuminate\Mail\Message  $mailMessage
255
     * @param  \Illuminate\Notifications\Messages\MailMessage  $message
256
     * @return $this
257
     */
258
    protected function runCallbacks($mailMessage, $message)
259
    {
260
        foreach ($message->callbacks as $callback) {
261
            $callback($mailMessage->getSymfonyMessage());
262
        }
263
 
264
        return $this;
265
    }
266
}