• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

valkyrjaio / valkyrja / 20156277091

12 Dec 2025 04:26AM UTC coverage: 73.416% (-0.6%) from 74.002%
20156277091

push

github

MelechMizrachi
CI: Trying to fix coveralls.

8125 of 11067 relevant lines covered (73.42%)

17.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

1.94
/src/Valkyrja/Notification/Notification.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <melechmizrachi@gmail.com>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Valkyrja\Notification;
15

16
use Override;
17
use Valkyrja\Broadcast\Contract\Broadcaster;
18
use Valkyrja\Broadcast\Data\Message as BroadcastMessage;
19
use Valkyrja\Exception\InvalidArgumentException;
20
use Valkyrja\Mail\Contract\Mailer;
21
use Valkyrja\Mail\Data\Message as MailMessage;
22
use Valkyrja\Notification\Contract\Notification as Contract;
23
use Valkyrja\Notification\Data\Contract\Notify;
24
use Valkyrja\Notification\Entity\Contract\NotifiableUser;
25
use Valkyrja\Notification\Factory\Contract\Factory;
26
use Valkyrja\Sms\Contract\Sms;
27
use Valkyrja\Sms\Data\Message as SmsMessage;
28

29
use function is_string;
30

31
/**
32
 * Class Notification.
33
 *
34
 * @author Melech Mizrachi
35
 */
36
class Notification implements Contract
37
{
38
    /**
39
     * The mail recipients.
40
     *
41
     * @var array<int, array{email: string, name: string, user?: NotifiableUser}>
42
     */
43
    protected array $mailRecipients = [];
44

45
    /**
46
     * The SMS recipients.
47
     *
48
     * @var array<int, array{to: non-empty-string, from: non-empty-string, text: non-empty-string, user?: NotifiableUser}>
49
     */
50
    protected array $smsRecipients = [];
51

52
    /**
53
     * The broadcast events.
54
     *
55
     * @var array<int, array{event: string, user?: NotifiableUser}>
56
     */
57
    protected array $broadcastEvents = [];
58

59
    /**
60
     * Notification constructor.
61
     */
62
    public function __construct(
2✔
63
        protected Factory $factory,
64
        protected Broadcaster $broadcaster,
65
        protected Mailer $mailer,
66
        protected Sms $sms,
67
    ) {
68
    }
2✔
69

70
    /**
71
     * @inheritDoc
72
     */
73
    #[Override]
×
74
    public function createNotification(string $name, array $data = []): Notify
75
    {
76
        return $this->factory->createNotification($name, $data);
×
77
    }
78

79
    /**
80
     * @inheritDoc
81
     */
82
    #[Override]
×
83
    public function addMailRecipient(string $email, string $name = ''): static
84
    {
85
        $this->mailRecipients[] = [
×
86
            'email' => $email,
×
87
            'name'  => $name,
×
88
        ];
×
89

90
        return $this;
×
91
    }
92

93
    /**
94
     * @inheritDoc
95
     */
96
    #[Override]
×
97
    public function addSmsRecipient(string $phoneNumber): static
98
    {
99
        // TODO: Figure this out
100
        $this->smsRecipients[] = [
×
101
            'to'   => $phoneNumber,
×
102
            'from' => 'us',
×
103
            'text' => 'text',
×
104
        ];
×
105

106
        return $this;
×
107
    }
108

109
    /**
110
     * @inheritDoc
111
     */
112
    #[Override]
×
113
    public function addBroadcastEvent(string $event): static
114
    {
115
        $this->broadcastEvents[] = [
×
116
            'event' => $event,
×
117
        ];
×
118

119
        return $this;
×
120
    }
121

122
    /**
123
     * @inheritDoc
124
     */
125
    #[Override]
×
126
    public function addUserRecipient(NotifiableUser $user): static
127
    {
128
        $this->addSmsUserRecipient($user);
×
129
        $this->addMailUserRecipient($user);
×
130
        $this->addBroadcastUserRecipient($user);
×
131

132
        return $this;
×
133
    }
134

135
    /**
136
     * @inheritDoc
137
     */
138
    #[Override]
×
139
    public function notify(Notify $notify): void
140
    {
141
        // $notification = $this->getNotification($notificationName, $data);
142

143
        if ($notify->shouldSendBroadcastMessage()) {
×
144
            $this->notifyByBroadcast($notify);
×
145
        }
146

147
        if ($notify->shouldSendMailMessage()) {
×
148
            $this->notifyByMail($notify);
×
149
        }
150

151
        if ($notify->shouldSendSmsMessage()) {
×
152
            $this->notifyBySms($notify);
×
153
        }
154

155
        $this->resetRecipients();
×
156
    }
157

158
    /**
159
     * @inheritDoc
160
     */
161
    #[Override]
×
162
    public function notifyUser(Notify $notify, NotifiableUser $user): void
163
    {
164
        $this->addUserRecipient($user);
×
165
        $this->notify($notify);
×
166
    }
167

168
    /**
169
     * @inheritDoc
170
     */
171
    #[Override]
×
172
    public function notifyUsers(Notify $notify, NotifiableUser ...$users): void
173
    {
174
        foreach ($users as $user) {
×
175
            $this->addUserRecipient($user);
×
176
        }
177

178
        $this->notify($notify);
×
179
    }
180

181
    /**
182
     * Reset all the recipient arrays.
183
     *
184
     * @return void
185
     */
186
    protected function resetRecipients(): void
×
187
    {
188
        $this->broadcastEvents = [];
×
189
        $this->mailRecipients  = [];
×
190
        $this->smsRecipients   = [];
×
191
    }
192

193
    /**
194
     * Add a user as an broadcast recipient.
195
     *
196
     * @param NotifiableUser $user The user
197
     *
198
     * @return void
199
     */
200
    protected function addBroadcastUserRecipient(NotifiableUser $user): void
×
201
    {
202
        /** @var mixed $secretId */
203
        $secretId = $user::hasSecretIdField()
×
204
            ? $user->__get($user::getSecretIdField())
×
205
            : null;
×
206

207
        if (is_string($secretId)) {
×
208
            $this->broadcastEvents[] = [
×
209
                'event' => $secretId,
×
210
                'user'  => $user,
×
211
            ];
×
212
        }
213
    }
214

215
    /**
216
     * Add a user as a mail recipient.
217
     *
218
     * @param NotifiableUser $user The user
219
     *
220
     * @return void
221
     */
222
    protected function addMailUserRecipient(NotifiableUser $user): void
×
223
    {
224
        $email = $user->__get($user::getEmailField());
×
225
        $name  = $user::hasNameField()
×
226
            ? $user->__get($user::getNameField())
×
227
            : '';
×
228

229
        if (! is_string($email)) {
×
230
            throw new InvalidArgumentException('Invalid email provided');
×
231
        }
232

233
        if (! is_string($name)) {
×
234
            throw new InvalidArgumentException('Invalid name provided');
×
235
        }
236

237
        $this->mailRecipients[] = [
×
238
            'email' => $email,
×
239
            'name'  => $name,
×
240
            'user'  => $user,
×
241
        ];
×
242
    }
243

244
    /**
245
     * Add a user as an SMS recipient.
246
     *
247
     * @param NotifiableUser $user The user
248
     *
249
     * @return void
250
     */
251
    protected function addSmsUserRecipient(NotifiableUser $user): void
×
252
    {
253
        /** @var mixed $phoneNumber */
254
        $phoneNumber = $user::hasPhoneNumberField()
×
255
            ? $user->__get($user::getPhoneNumberField())
×
256
            : null;
×
257

258
        if (is_string($phoneNumber) && $phoneNumber !== '') {
×
259
            $this->smsRecipients[] = [
×
260
                'to'   => $phoneNumber,
×
261
                'from' => 'us',
×
262
                'text' => 'test',
×
263
                'user' => $user,
×
264
            ];
×
265
        }
266
    }
267

268
    /**
269
     * Send a notification by broadcast.
270
     *
271
     * @param Notify $notify The notification
272
     *
273
     * @return void
274
     */
275
    protected function notifyByBroadcast(Notify $notify): void
×
276
    {
277
        foreach ($this->broadcastEvents as $broadcastEvent) {
×
278
            $message = new BroadcastMessage();
×
279

280
            $message->setEvent($broadcastEvent['event']);
×
281
            $notify->broadcast($message);
×
282

283
            $this->broadcaster->send($message);
×
284
        }
285
    }
286

287
    /**
288
     * Send a notification by mail.
289
     *
290
     * @param Notify $notify The notification
291
     *
292
     * @return void
293
     */
294
    protected function notifyByMail(Notify $notify): void
×
295
    {
296
        foreach ($this->mailRecipients as $mailRecipient) {
×
297
            $message = new MailMessage(
×
298
                $mailRecipient['email'],
×
299
                $mailRecipient['name']
×
300
            );
×
301

302
            $notify->mail($message);
×
303

304
            $this->mailer->send($message);
×
305
        }
306
    }
307

308
    /**
309
     * Send a notification by SMS.
310
     *
311
     * @param Notify $notify The notification
312
     *
313
     * @return void
314
     */
315
    protected function notifyBySms(Notify $notify): void
×
316
    {
317
        foreach ($this->smsRecipients as $smsRecipient) {
×
318
            $message = new SmsMessage(
×
319
                $smsRecipient['to'],
×
320
                $smsRecipient['from'],
×
321
                $smsRecipient['text']
×
322
            );
×
323

324
            $notify->sms($message);
×
325

326
            $this->sms->send($message);
×
327
        }
328
    }
329
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc