Passed
Push — master ( 3a97d5...2b44a8 )
by Adam
21:57 queued 12s
created

Notification::notificationUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Coyote\Services\Notification;
4
5
use Coyote\User;
6
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
7
use Illuminate\Notifications\Notification as BaseNotification;
8
use Coyote\Notification as Model;
9
use NotificationChannels\WebPush\WebPushChannel;
10
11
abstract class Notification extends BaseNotification implements NotificationInterface
12
{
13
    /**
14
     * @var string|null
15
     */
16
    public $broadcastChannel;
17
18
    /**
19
     * Get the notification's delivery channels.
20
     *
21
     * @param  User  $user
22
     * @return array
23
     */
24
    public function via(User $user)
25
    {
26
        return $this->channels($user);
27
    }
28
29
    /**
30
     * Get the channels the event should be broadcast on.
31
     *
32
     * @return array
33
     */
34
    public function broadcastOn()
35
    {
36
        return [$this->broadcastChannel];
37
    }
38
39
    /**
40
     * @param User $user
41
     * @return array
42
     */
43
    protected function channels(User $user): array
44
    {
45
        $this->broadcastChannel = $user->receivesBroadcastNotificationsOn();
46
47
        /** @var \Illuminate\Support\Collection $channels */
48
        $channels = $user->notificationSettings()->select('channel')->where('type_id', static::ID)->where('is_enabled', true)->pluck('channel');
0 ignored issues
show
Bug introduced by
The constant Coyote\Services\Notification\Notification::ID was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
49
50
        if (empty($channels)) {
51
            return [];
52
        }
53
54
        if ($channels->contains(Model::DB) && $this instanceof ShouldBroadcast) {
55
            $channels->push('broadcast');
56
        }
57
58
        if ($channels->contains(Model::MAIL) && !$user->canReceiveEmail()) {
59
            $channels->forget(Model::MAIL);
60
        }
61
62
        // do not send another notification if previous was not yet read
63
        if (!empty($user->getUnreadNotification($this->objectId()))) {
64
            $channels->forget([Model::MAIL, Model::PUSH]);
65
        }
66
67
        return $this->resolveChannels($channels);
68
    }
69
70
    /**
71
     * @param \Illuminate\Support\Collection $channels
72
     * @return array
73
     */
74
    private function resolveChannels($channels): array
75
    {
76
        $replacement = [Model::DB => DatabaseChannel::class, Model::PUSH => WebPushChannel::class];
77
78
        foreach ($replacement as $channel => $class) {
79
            if ($channels->contains($channel)) {
80
                $channels = $channels->reject(fn ($value) => $value === $channel)->push($class);
81
            }
82
        }
83
84
        return $channels->toArray();
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    protected function notificationUrl()
91
    {
92
        return route('user.notifications.url', [$this->id]);
93
    }
94
}
95