AsgardNotification   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A push() 0 14 3
A triggerEventFor() 0 4 1
A to() 0 6 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