AsgardNotification::to()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Modules\Notification\Services;
4
5
use Modules\Notification\Events\BroadcastNotification;
6
use Modules\Notification\Repositories\NotificationRepository;
7
use Modules\User\Contracts\Authentication;
8
9
final class AsgardNotification implements Notification
10
{
11
    /**
12
     * @var NotificationRepository
13
     */
14
    private $notification;
15
    /**
16
     * @var Authentication
17
     */
18
    private $auth;
19
    /**
20
     * @var int
21
     */
22
    private $userId;
23
24
    public function __construct(NotificationRepository $notification, Authentication $auth)
25
    {
26
        $this->notification = $notification;
27
        $this->auth = $auth;
28
    }
29
30
    /**
31
     * Push a notification on the dashboard
32
     * @param string $title
33
     * @param string $message
34
     * @param string $icon
35
     * @param string|null $link
36
     */
37
    public function push($title, $message, $icon, $link = null)
38
    {
39
        $notification = $this->notification->create([
40
            'user_id' => $this->userId ?: $this->auth->id(),
41
            'icon_class' => $icon,
42
            'link' => $link,
43
            'title' => $title,
44
            'message' => $message,
45
        ]);
46
47
        if (true === config('asgard.notification.config.real-time', false)) {
48
            $this->triggerEventFor($notification);
49
        }
50
    }
51
52
    /**
53
     * Trigger the broadcast event for the given notification
54
     * @param \Modules\Notification\Entities\Notification $notification
55
     */
56
    private function triggerEventFor(\Modules\Notification\Entities\Notification $notification)
57
    {
58
        event(new BroadcastNotification($notification));
59
    }
60
61
    /**
62
     * Set a user id to set the notification to
63
     * @param int $userId
64
     * @return $this
65
     */
66
    public function to($userId)
67
    {
68
        $this->userId = $userId;
69
70
        return $this;
71
    }
72
}
73