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\DatabaseNotificationInterface; |
||
6 | use IrishDan\NotificationBundle\Notification\NotifiableInterface; |
||
7 | use Symfony\Bridge\Doctrine\ManagerRegistry; |
||
8 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
9 | |||
10 | class DatabaseNotificationManager |
||
11 | { |
||
12 | protected $databaseConfiguration; |
||
13 | protected $managerRegistry; |
||
14 | protected $propertyAccessor; |
||
15 | |||
16 | public function __construct(ManagerRegistry $managerRegistry, array $databaseConfiguration = []) |
||
17 | { |
||
18 | $this->managerRegistry = $managerRegistry; |
||
19 | $this->databaseConfiguration = $databaseConfiguration; |
||
20 | } |
||
21 | |||
22 | public function createDatabaseNotification(array $data) |
||
23 | { |
||
24 | if ($this->propertyAccessor === null) { |
||
25 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
||
26 | } |
||
27 | |||
28 | $entity = $this->notificationEntityName(); |
||
29 | if ($entity) { |
||
30 | $entityManager = $this->managerRegistry->getManagerForClass($entity); |
||
31 | $class = $entityManager->getRepository($entity)->getClassName(); |
||
32 | $databaseNotification = new $class(); |
||
33 | |||
34 | // Transfer values from message to databaseNotification. |
||
35 | $properties = ['notifiable', 'uuid', 'type', 'body', 'title']; |
||
36 | foreach ($properties as $property) { |
||
37 | $value = $this->propertyAccessor->getValue($data, '[' . $property . ']'); |
||
38 | $this->propertyAccessor->setValue($databaseNotification, $property, $value); |
||
39 | } |
||
40 | |||
41 | // Save the notification to the database |
||
42 | $entityManager->persist($databaseNotification); |
||
43 | $entityManager->flush(); |
||
44 | |||
45 | return $databaseNotification; |
||
46 | } |
||
47 | |||
48 | return false; |
||
49 | } |
||
50 | |||
51 | public function setReadAtDate(DatabaseNotificationInterface $notification, $now = null, $flush = true) |
||
52 | { |
||
53 | if (empty($now)) { |
||
54 | $now = new \DateTime(); |
||
55 | } |
||
56 | |||
57 | $notification->setReadAt($now); |
||
58 | |||
59 | $entityManager = $this->managerRegistry->getManagerForClass(get_class($notification)); |
||
60 | $entityManager->persist($notification); |
||
61 | if ($flush) { |
||
62 | $entityManager->flush(); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | public function setUsersNotificationsAsRead(NotifiableInterface $notifiable, $now = null) |
||
0 ignored issues
–
show
|
|||
67 | { |
||
68 | $entity = $this->notificationEntityName(); |
||
69 | if (!empty($entity)) { |
||
70 | $options = [ |
||
71 | 'notifiable' => $notifiable, |
||
72 | 'readAt' => null, |
||
73 | ]; |
||
74 | |||
75 | $entityManager = $this->managerRegistry->getManagerForClass($entity); |
||
76 | $usersNotifications = $entityManager->getRepository($entity)->findBy($options); |
||
77 | |||
78 | if (!empty($usersNotifications)) { |
||
79 | foreach ($usersNotifications as $notification) { |
||
80 | $this->setReadAtDate($notification, null, false); |
||
81 | } |
||
82 | } |
||
83 | $entityManager->flush(); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | public function getUsersNotificationCount(NotifiableInterface $user, $status = '') |
||
88 | { |
||
89 | $entity = $this->notificationEntityName(); |
||
90 | if (!empty($entity)) { |
||
91 | $entityManager = $this->managerRegistry->getManagerForClass($entity); |
||
92 | // @TODO: Have a look at this. |
||
93 | $count = $entityManager->getRepository($entity)->getNotificationsCount($user, $status); |
||
94 | |||
95 | return $count; |
||
96 | } |
||
97 | |||
98 | return 0; |
||
99 | } |
||
100 | |||
101 | protected function notificationEntityName() |
||
102 | { |
||
103 | $config = $this->databaseConfiguration; |
||
104 | if (!empty($config['entity'])) { |
||
105 | return $config['entity']; |
||
106 | } |
||
107 | |||
108 | return false; |
||
109 | } |
||
110 | } |
||
111 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.