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, DatabaseNotificationManager $databaseNotificationManager = null, $broadcaster = null) |
||
30 | { |
||
31 | $this->channelManager = $channelManager; |
||
32 | $this->databaseNotificationManager = $databaseNotificationManager; |
||
33 | $this->broadcaster = $broadcaster; |
||
0 ignored issues
–
show
|
|||
34 | } |
||
35 | |||
36 | public function setDatabaseNotificationManager(DatabaseNotificationManager $databaseNotificationManager) |
||
37 | { |
||
38 | $this->databaseNotificationManager = $databaseNotificationManager; |
||
39 | } |
||
40 | |||
41 | public function broadcast(NotificationInterface $notification, array $broadcasters = null) |
||
42 | { |
||
43 | try { |
||
44 | $this->broadcaster->broadcast($notification); |
||
45 | } catch (\Exception $exception) { |
||
46 | // @TODO: |
||
47 | } |
||
48 | } |
||
49 | |||
50 | public function send(NotificationInterface $notification, $recipients, array $data = []) |
||
51 | { |
||
52 | if (!is_array($recipients)) { |
||
53 | $recipients = [$recipients]; |
||
54 | } |
||
55 | |||
56 | if (!empty($data)) { |
||
57 | if (empty($this->propertyAccessor)) { |
||
58 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
59 | } |
||
60 | |||
61 | $dataArray = $notification->getDataArray(); |
||
62 | foreach ($data as $key => $value) { |
||
63 | $this->propertyAccessor->setValue($dataArray, '[' . $key . ']', $value); |
||
64 | } |
||
65 | |||
66 | $notification->setDataArray($dataArray); |
||
67 | } |
||
68 | |||
69 | $this->channelManager->send($recipients, $notification); |
||
70 | } |
||
71 | |||
72 | public function markAsRead(NotificationInterface $notification) |
||
73 | { |
||
74 | $now = new \DateTime(); |
||
75 | try { |
||
76 | $this->databaseNotificationManager->setReadAtDate($notification, $now); |
||
77 | |||
78 | return true; |
||
79 | } catch (\Exception $exception) { |
||
80 | // @TODO: |
||
81 | } |
||
82 | } |
||
83 | |||
84 | public function markAllAsRead(NotifiableInterface $user) |
||
85 | { |
||
86 | $now = new \DateTime(); |
||
87 | try { |
||
88 | $this->databaseNotificationManager->setUsersNotificationsAsRead($user, $now); |
||
89 | |||
90 | return true; |
||
91 | } catch (\Exception $exception) { |
||
92 | // @TODO: |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function allNotificationCount(NotifiableInterface $user) |
||
97 | { |
||
98 | return $this->notificationCount($user); |
||
99 | } |
||
100 | |||
101 | public function unreadNotificationCount(NotifiableInterface $user) |
||
102 | { |
||
103 | return $this->notificationCount($user, 'unread'); |
||
104 | } |
||
105 | |||
106 | public function readNotificationCount(NotifiableInterface $user) |
||
107 | { |
||
108 | return $this->notificationCount($user, 'read'); |
||
109 | } |
||
110 | |||
111 | public function notificationCount(NotifiableInterface $user, $status = '') |
||
112 | { |
||
113 | // try { |
||
114 | |||
115 | return $this->databaseNotificationManager->getUsersNotificationCount($user, $status); |
||
116 | // } catch (\Exception $exception) { |
||
117 | // // @TODO: |
||
118 | // } |
||
119 | } |
||
120 | } |
||
121 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: