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

valkyrjaio / valkyrja / 20687263522

04 Jan 2026 03:56AM UTC coverage: 74.209%. Remained the same
20687263522

push

github

web-flow
[CI] Add dead code rector rules (#337)

# Description

Add dead code rector rules.

## Types of changes

- [ ] Bug fix _(non-breaking change which fixes an issue)_
    <!-- Target the lowest major affected branch -->
- [X] New feature _(non-breaking change which adds functionality)_
    <!-- Target master -->
- [ ] Deprecation _(breaking change which removes functionality)_
    <!-- Target master -->
- [ ] Breaking change _(fix or feature that would cause existing
functionality
  to change)_
<!-- Target master, unless this is a bug fix in which case let's chat
-->
- [ ] Documentation improvement
    <!-- Target appropriate branch -->

8419 of 11345 relevant lines covered (74.21%)

8.87 hits per line

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

1.94
/src/Valkyrja/Notification/Notifier/Notifier.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\Notifier;
15

16
use Override;
17
use Valkyrja\Broadcast\Broadcaster\Contract\BroadcasterContract;
18
use Valkyrja\Broadcast\Data\Message as BroadcastMessage;
19
use Valkyrja\Mail\Data\Message as MailMessage;
20
use Valkyrja\Mail\Mailer\Contract\MailerContract;
21
use Valkyrja\Notification\Data\Contract\NotifyContract;
22
use Valkyrja\Notification\Entity\Contract\NotifiableUserContract;
23
use Valkyrja\Notification\Factory\Contract\FactoryContract;
24
use Valkyrja\Notification\Notifier\Contract\NotifierContract as Contract;
25
use Valkyrja\Sms\Data\Message as SmsMessage;
26
use Valkyrja\Sms\Messenger\Contract\MessengerContract;
27
use Valkyrja\Throwable\Exception\InvalidArgumentException;
28

29
use function is_string;
30

31
class Notifier implements Contract
32
{
33
    /**
34
     * The mail recipients.
35
     *
36
     * @var array<int, array{email: string, name: string, user?: NotifiableUserContract}>
37
     */
38
    protected array $mailRecipients = [];
39

40
    /**
41
     * The SMS recipients.
42
     *
43
     * @var array<int, array{to: non-empty-string, from: non-empty-string, text: non-empty-string, user?: NotifiableUserContract}>
44
     */
45
    protected array $smsRecipients = [];
46

47
    /**
48
     * The broadcast events.
49
     *
50
     * @var array<int, array{event: string, user?: NotifiableUserContract}>
51
     */
52
    protected array $broadcastEvents = [];
53

54
    public function __construct(
1✔
55
        protected FactoryContract $factory,
56
        protected BroadcasterContract $broadcaster,
57
        protected MailerContract $mailer,
58
        protected MessengerContract $sms,
59
    ) {
60
    }
1✔
61

62
    /**
63
     * @inheritDoc
64
     */
65
    #[Override]
×
66
    public function createNotification(string $name, array $data = []): NotifyContract
67
    {
68
        return $this->factory->createNotification($name, $data);
×
69
    }
70

71
    /**
72
     * @inheritDoc
73
     */
74
    #[Override]
×
75
    public function addMailRecipient(string $email, string $name = ''): static
76
    {
77
        $this->mailRecipients[] = [
×
78
            'email' => $email,
×
79
            'name'  => $name,
×
80
        ];
×
81

82
        return $this;
×
83
    }
84

85
    /**
86
     * @inheritDoc
87
     */
88
    #[Override]
×
89
    public function addSmsRecipient(string $phoneNumber): static
90
    {
91
        // TODO: Figure this out
92
        $this->smsRecipients[] = [
×
93
            'to'   => $phoneNumber,
×
94
            'from' => 'us',
×
95
            'text' => 'text',
×
96
        ];
×
97

98
        return $this;
×
99
    }
100

101
    /**
102
     * @inheritDoc
103
     */
104
    #[Override]
×
105
    public function addBroadcastEvent(string $event): static
106
    {
107
        $this->broadcastEvents[] = [
×
108
            'event' => $event,
×
109
        ];
×
110

111
        return $this;
×
112
    }
113

114
    /**
115
     * @inheritDoc
116
     */
117
    #[Override]
×
118
    public function addUserRecipient(NotifiableUserContract $user): static
119
    {
120
        $this->addSmsUserRecipient($user);
×
121
        $this->addMailUserRecipient($user);
×
122
        $this->addBroadcastUserRecipient($user);
×
123

124
        return $this;
×
125
    }
126

127
    /**
128
     * @inheritDoc
129
     */
130
    #[Override]
×
131
    public function notify(NotifyContract $notify): void
132
    {
133
        // $notification = $this->getNotification($notificationName, $data);
134

135
        if ($notify->shouldSendBroadcastMessage()) {
×
136
            $this->notifyByBroadcast($notify);
×
137
        }
138

139
        if ($notify->shouldSendMailMessage()) {
×
140
            $this->notifyByMail($notify);
×
141
        }
142

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

147
        $this->resetRecipients();
×
148
    }
149

150
    /**
151
     * @inheritDoc
152
     */
153
    #[Override]
×
154
    public function notifyUser(NotifyContract $notify, NotifiableUserContract $user): void
155
    {
156
        $this->addUserRecipient($user);
×
157
        $this->notify($notify);
×
158
    }
159

160
    /**
161
     * @inheritDoc
162
     */
163
    #[Override]
×
164
    public function notifyUsers(NotifyContract $notify, NotifiableUserContract ...$users): void
165
    {
166
        foreach ($users as $user) {
×
167
            $this->addUserRecipient($user);
×
168
        }
169

170
        $this->notify($notify);
×
171
    }
172

173
    /**
174
     * Reset all the recipient arrays.
175
     */
176
    protected function resetRecipients(): void
×
177
    {
178
        $this->broadcastEvents = [];
×
179
        $this->mailRecipients  = [];
×
180
        $this->smsRecipients   = [];
×
181
    }
182

183
    /**
184
     * Add a user as an broadcast recipient.
185
     *
186
     * @param NotifiableUserContract $user The user
187
     */
188
    protected function addBroadcastUserRecipient(NotifiableUserContract $user): void
×
189
    {
190
        /** @var mixed $secretId */
191
        $secretId = $user::hasSecretIdField()
×
192
            ? $user->__get($user::getSecretIdField())
×
193
            : null;
×
194

195
        if (is_string($secretId)) {
×
196
            $this->broadcastEvents[] = [
×
197
                'event' => $secretId,
×
198
                'user'  => $user,
×
199
            ];
×
200
        }
201
    }
202

203
    /**
204
     * Add a user as a mail recipient.
205
     *
206
     * @param NotifiableUserContract $user The user
207
     */
208
    protected function addMailUserRecipient(NotifiableUserContract $user): void
×
209
    {
210
        $email = $user->__get($user::getEmailField());
×
211
        $name  = $user::hasNameField()
×
212
            ? $user->__get($user::getNameField())
×
213
            : '';
×
214

215
        if (! is_string($email)) {
×
216
            throw new InvalidArgumentException('Invalid email provided');
×
217
        }
218

219
        if (! is_string($name)) {
×
220
            throw new InvalidArgumentException('Invalid name provided');
×
221
        }
222

223
        $this->mailRecipients[] = [
×
224
            'email' => $email,
×
225
            'name'  => $name,
×
226
            'user'  => $user,
×
227
        ];
×
228
    }
229

230
    /**
231
     * Add a user as an SMS recipient.
232
     *
233
     * @param NotifiableUserContract $user The user
234
     */
235
    protected function addSmsUserRecipient(NotifiableUserContract $user): void
×
236
    {
237
        /** @var mixed $phoneNumber */
238
        $phoneNumber = $user::hasPhoneNumberField()
×
239
            ? $user->__get($user::getPhoneNumberField())
×
240
            : null;
×
241

242
        if (is_string($phoneNumber) && $phoneNumber !== '') {
×
243
            $this->smsRecipients[] = [
×
244
                'to'   => $phoneNumber,
×
245
                'from' => 'us',
×
246
                'text' => 'test',
×
247
                'user' => $user,
×
248
            ];
×
249
        }
250
    }
251

252
    /**
253
     * Send a notification by broadcast.
254
     *
255
     * @param NotifyContract $notify The notification
256
     */
257
    protected function notifyByBroadcast(NotifyContract $notify): void
×
258
    {
259
        foreach ($this->broadcastEvents as $broadcastEvent) {
×
260
            $message = new BroadcastMessage();
×
261

262
            $message->setEvent($broadcastEvent['event']);
×
263
            $notify->broadcast($message);
×
264

265
            $this->broadcaster->send($message);
×
266
        }
267
    }
268

269
    /**
270
     * Send a notification by mail.
271
     *
272
     * @param NotifyContract $notify The notification
273
     */
274
    protected function notifyByMail(NotifyContract $notify): void
×
275
    {
276
        foreach ($this->mailRecipients as $mailRecipient) {
×
277
            $message = new MailMessage(
×
278
                $mailRecipient['email'],
×
279
                $mailRecipient['name']
×
280
            );
×
281

282
            $notify->mail($message);
×
283

284
            $this->mailer->send($message);
×
285
        }
286
    }
287

288
    /**
289
     * Send a notification by SMS.
290
     *
291
     * @param NotifyContract $notify The notification
292
     */
293
    protected function notifyBySms(NotifyContract $notify): void
×
294
    {
295
        foreach ($this->smsRecipients as $smsRecipient) {
×
296
            $message = new SmsMessage(
×
297
                $smsRecipient['to'],
×
298
                $smsRecipient['from'],
×
299
                $smsRecipient['text']
×
300
            );
×
301

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

304
            $this->sms->send($message);
×
305
        }
306
    }
307
}
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