irishdan /
NotificationBundle
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
|
|||
| 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 | foreach ($usersNotifications as $notification) { |
||
| 127 | $this->setNotificationsReadat($usersNotifications); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | $entityManager->flush(); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $notifications |
||
| 136 | */ |
||
| 137 | public function setNotificationsReadAt(array $notifications) |
||
| 138 | { |
||
| 139 | $entityManager = $this->getEntityManager(); |
||
| 140 | foreach ($notifications as $notification) { |
||
| 141 | $this->setReadAtDate($notification, null, false); |
||
| 142 | } |
||
| 143 | |||
| 144 | $entityManager->flush(); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param DatabaseNotifiableInterface $user |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function getUsersUnreadNotifications(DatabaseNotifiableInterface $user) |
||
| 152 | { |
||
| 153 | $entityManager = $this->getEntityManager(); |
||
| 154 | |||
| 155 | if ($entityManager) { |
||
| 156 | $entity = $this->notificationEntityName(); |
||
| 157 | $notifications = $entityManager->getRepository($entity)->getUnreadNotifications($user); |
||
| 158 | |||
| 159 | return $notifications; |
||
| 160 | } |
||
| 161 | |||
| 162 | return []; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param NotifiableInterface $user |
||
| 167 | * @param string $status |
||
| 168 | * @return int |
||
| 169 | */ |
||
| 170 | public function getUsersNotificationCount(NotifiableInterface $user, $status = '') |
||
| 171 | { |
||
| 172 | $entityManager = $this->getEntityManager(); |
||
| 173 | if ($entityManager) { |
||
| 174 | $entity = $this->notificationEntityName(); |
||
| 175 | $count = $entityManager->getRepository($entity)->getNotificationsCount($user, $status); |
||
| 176 | |||
| 177 | return $count; |
||
| 178 | } |
||
| 179 | |||
| 180 | return 0; |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param array $options |
||
| 185 | * @throws \Exception |
||
| 186 | */ |
||
| 187 | public function findAndSetAsRead(array $options) |
||
| 188 | { |
||
| 189 | $entityManager = $this->getEntityManager(); |
||
| 190 | $entity = $this->notificationEntityName(); |
||
| 191 | $notifications = $entityManager->getRepository($entity)->findBy($options); |
||
| 192 | |||
| 193 | try { |
||
| 194 | $this->setNotificationsReadAt($notifications); |
||
| 195 | } catch (\Exception $e) { |
||
| 196 | throw new \Exception( |
||
| 197 | $e->getMessage() |
||
| 198 | ); |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return bool|mixed |
||
| 204 | */ |
||
| 205 | protected function notificationEntityName() |
||
| 206 | { |
||
| 207 | $config = $this->databaseConfiguration; |
||
| 208 | if (!empty($config['entity'])) { |
||
| 209 | return $config['entity']; |
||
| 210 | } |
||
| 211 | |||
| 212 | return false; |
||
| 213 | } |
||
| 214 | } |
||
| 215 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.