Completed
Push — master ( 1494d2...398a9b )
by dan
02:07
created

NotificationManager.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace IrishDan\NotificationBundle;
4
5
use IrishDan\NotificationBundle\Notification\NotifiableInterface;
6
use IrishDan\NotificationBundle\Notification\NotificationInterface;
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
9
/**
10
 * Class NotificationManager
11
 * This the primary public service.
12
 * From here all notifications can be dispatched.
13
 * Essentially is just a wrapper around the ContainerManager and DatabaseNotificationManager
14
 *
15
 * @package NotificationBundle
16
 */
17
class NotificationManager
18
{
19
    /**
20
     * @var ChannelManager
21
     */
22
    private $channelManager;
23
    /**
24
     * @var DatabaseNotificationManager
25
     */
26
    private $databaseNotificationManager;
27
    protected $propertyAccessor;
28
29
    public function __construct(ChannelManager $channelManager)
30
    {
31
        $this->channelManager = $channelManager;
32
    }
33
34
    public function setDatabaseNotificationManager(DatabaseNotificationManager $databaseNotificationManager)
35
    {
36
        $this->databaseNotificationManager = $databaseNotificationManager;
37
    }
38
39
    public function broadcast(NotificationInterface $notification, array $broadcasters = null)
0 ignored issues
show
The parameter $notification is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41
42
    }
43
44
    public function send(NotificationInterface $notification, $recipients, array $data = [])
45
    {
46
        if (!is_array($recipients)) {
47
            $recipients = [$recipients];
48
        }
49
50
        if (!empty($data)) {
51
            if (empty($this->propertyAccessor)) {
52
                $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
53
            }
54
55
            $dataArray = $notification->getDataArray();
56
            foreach ($data as $key => $value) {
57
                $this->propertyAccessor->setValue($dataArray, '[' . $key . ']', $value);
58
            }
59
60
            $notification->setDataArray($dataArray);
61
        }
62
63
        $this->channelManager->send($recipients, $notification);
64
    }
65
66
    public function markAsRead(NotificationInterface $notification)
67
    {
68
        $now = new \DateTime();
69
        try {
70
            $this->databaseNotificationManager->setReadAtDate($notification, $now);
71
72
            return true;
73
        } catch (\Exception $exception) {
74
            // @TODO:
75
        }
76
    }
77
78
    public function markAllAsRead(NotifiableInterface $user)
79
    {
80
        $now = new \DateTime();
81
        try {
82
            $this->databaseNotificationManager->setUsersNotificationsAsRead($user, $now);
83
84
            return true;
85
        } catch (\Exception $exception) {
86
            // @TODO:
87
        }
88
    }
89
90
    public function allNotificationCount(NotifiableInterface $user)
91
    {
92
        return $this->notificationCount($user);
93
    }
94
95
    public function unreadNotificationCount(NotifiableInterface $user)
96
    {
97
        return $this->notificationCount($user, 'unread');
98
    }
99
100
    public function readNotificationCount(NotifiableInterface $user)
101
    {
102
        return $this->notificationCount($user, 'read');
103
    }
104
105
    public function notificationCount(NotifiableInterface $user, $status = '')
106
    {
107
        // try {
108
109
        return $this->databaseNotificationManager->getUsersNotificationCount($user, $status);
110
        // } catch (\Exception $exception) {
111
        //     // @TODO:
112
        // }
113
    }
114
}
115