Completed
Pull Request — master (#4)
by dan
14:39
created

DatabaseNotificationManager.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\DatabaseNotificationInterface;
6
use IrishDan\NotificationBundle\Notification\NotifiableInterface;
7
use Symfony\Bridge\Doctrine\ManagerRegistry;
8
use Symfony\Component\PropertyAccess\PropertyAccess;
9
use Symfony\Component\PropertyAccess\PropertyAccessor;
10
11
/**
12
 * Class DatabaseNotificationManager
13
 *
14
 * @package IrishDan\NotificationBundle
15
 */
16
class DatabaseNotificationManager
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $databaseConfiguration;
22
    /**
23
     * @var ManagerRegistry
24
     */
25
    protected $managerRegistry;
26
    /**
27
     * @var PropertyAccessor
28
     */
29
    protected $propertyAccessor;
30
31
    /**
32
     * DatabaseNotificationManager constructor.
33
     *
34
     * @param ManagerRegistry $managerRegistry
35
     * @param array           $databaseConfiguration
36
     */
37
    public function __construct(ManagerRegistry $managerRegistry, array $databaseConfiguration = [])
38
    {
39
        $this->managerRegistry = $managerRegistry;
40
        $this->databaseConfiguration = $databaseConfiguration;
41
    }
42
43
    /**
44
     * @return bool|\Doctrine\Common\Persistence\ObjectManager|null|object
45
     */
46
    protected function getEntityManager()
47
    {
48
        $entity = $this->notificationEntityName();
49
        if ($entity) {
50
            return $this->managerRegistry->getManagerForClass($entity);
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * @param array $data
58
     * @return bool
59
     */
60
    public function createDatabaseNotification(array $data)
61
    {
62
        if ($this->propertyAccessor === null) {
63
            $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
64
        }
65
66
        $entityManager = $this->getEntityManager();
67
        if ($entityManager) {
68
            $entity = $this->notificationEntityName();
69
            $class = $entityManager->getRepository($entity)->getClassName();
70
            $databaseNotification = new $class();
71
72
            // Transfer values from message to databaseNotification.
73
            $properties = ['notifiable', 'uuid', 'type', 'body', 'title'];
74
            foreach ($properties as $property) {
75
                $value = $this->propertyAccessor->getValue($data, '[' . $property . ']');
76
                $this->propertyAccessor->setValue($databaseNotification, $property, $value);
77
            }
78
79
            // Save the notification to the database
80
            $entityManager->persist($databaseNotification);
81
            $entityManager->flush();
82
83
            return $databaseNotification;
84
        }
85
86
        return false;
87
    }
88
89
    /**
90
     * @param DatabaseNotificationInterface $notification
91
     * @param null                          $now
92
     * @param bool                          $flush
93
     */
94
    public function setReadAtDate(DatabaseNotificationInterface $notification, $now = null, $flush = true)
95
    {
96
        if (empty($now)) {
97
            $now = new \DateTime();
98
        }
99
100
        $notification->setReadAt($now);
101
102
        $entityManager = $this->getEntityManager();
103
        $entityManager->persist($notification);
104
        if ($flush) {
105
            $entityManager->flush();
106
        }
107
    }
108
109
    /**
110
     * @param NotifiableInterface $notifiable
111
     * @param null                $now
112
     */
113
    public function setUsersNotificationsAsRead(NotifiableInterface $notifiable, $now = null)
0 ignored issues
show
The parameter $now 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...
114
    {
115
        $entity = $this->notificationEntityName();
116
        if (!empty($entity)) {
117
            $options = [
118
                'notifiable' => $notifiable,
119
                'readAt' => null,
120
            ];
121
122
            $entityManager = $this->getEntityManager();
123
            $usersNotifications = $entityManager->getRepository($entity)->findBy($options);
124
125
            if (!empty($usersNotifications)) {
126
                $this->setNotificationsReadat($usersNotifications);
127
            }
128
            $entityManager->flush();
129
        }
130
    }
131
132
    /**
133
     * @param array $notifications
134
     */
135
    public function setNotificationsReadAt(array $notifications)
136
    {
137
        $entityManager = $this->getEntityManager();
138
        foreach ($notifications as $notification) {
139
            $this->setReadAtDate($notification, null, false);
140
        }
141
142
        $entityManager->flush();
143
    }
144
145
    /**
146
     * @param DatabaseNotifiableInterface $user
147
     * @return array
148
     */
149
    public function getUsersUnreadNotifications(DatabaseNotifiableInterface $user)
150
    {
151
        $entityManager = $this->getEntityManager();
152
153
        if ($entityManager) {
154
            $entity = $this->notificationEntityName();
155
            $notifications = $entityManager->getRepository($entity)->getUnreadNotifications($user);
156
157
            return $notifications;
158
        }
159
160
        return [];
161
    }
162
163
    /**
164
     * @param NotifiableInterface $user
165
     * @param string              $status
166
     * @return int
167
     */
168
    public function getUsersNotificationCount(NotifiableInterface $user, $status = '')
169
    {
170
        $entityManager = $this->getEntityManager();
171
        if ($entityManager) {
172
            $entity = $this->notificationEntityName();
173
            $count = $entityManager->getRepository($entity)->getNotificationsCount($user, $status);
174
175
            return $count;
176
        }
177
178
        return 0;
179
    }
180
181
    /**
182
     * @param array $options
183
     * @throws \Exception
184
     */
185
    public function findAndSetAsRead(array $options)
186
    {
187
        $entityManager = $this->getEntityManager();
188
        $entity = $this->notificationEntityName();
189
        $notifications = $entityManager->getRepository($entity)->findBy($options);
190
191
        try {
192
            $this->setNotificationsReadAt($notifications);
193
        } catch (\Exception $e) {
194
            throw new \Exception(
195
                $e->getMessage()
196
            );
197
        }
198
    }
199
200
    /**
201
     * @return bool|mixed
202
     */
203
    protected function notificationEntityName()
204
    {
205
        $config = $this->databaseConfiguration;
206
        if (!empty($config['entity'])) {
207
            return $config['entity'];
208
        }
209
210
        return false;
211
    }
212
}
213