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\Dispatcher; |
||
| 4 | |||
| 5 | use Doctrine\ORM\EntityManager; |
||
| 6 | use IrishDan\NotificationBundle\Message\BaseMessage; |
||
| 7 | use IrishDan\NotificationBundle\Message\MessageInterface; |
||
| 8 | use Symfony\Component\PropertyAccess\PropertyAccess; |
||
| 9 | |||
| 10 | class DatabaseMessageDispatcher implements MessageDispatcherInterface |
||
| 11 | { |
||
| 12 | protected $entityManager; |
||
| 13 | protected $configuration; |
||
| 14 | protected $accessor; |
||
| 15 | |||
| 16 | public function __construct(EntityManager $entityManager, $configuration = []) |
||
|
0 ignored issues
–
show
|
|||
| 17 | { |
||
| 18 | $this->entityManager = $entityManager; |
||
| 19 | $this->configuration = $configuration; |
||
| 20 | } |
||
| 21 | |||
| 22 | public function dispatch(MessageInterface $message) |
||
| 23 | { |
||
| 24 | // Get the dispatch and message data from the message. |
||
| 25 | $dispatchData = $message->getDispatchData(); |
||
| 26 | $messageData = $message->getMessageData(); |
||
| 27 | |||
| 28 | // Create the database notification entity |
||
| 29 | $databaseNotification = $this->createDatabaseNotificationEntity($message); |
||
| 30 | if ($databaseNotification) { |
||
| 31 | // Save the notification to the database |
||
| 32 | $this->entityManager->persist($databaseNotification); |
||
| 33 | $this->entityManager->flush(); |
||
| 34 | |||
| 35 | return true; |
||
| 36 | } |
||
| 37 | |||
| 38 | return false; |
||
| 39 | } |
||
| 40 | |||
| 41 | protected function createDatabaseNotificationEntity($data) |
||
| 42 | { |
||
| 43 | if ($this->accessor === null) { |
||
| 44 | $this->accessor = PropertyAccess::createPropertyAccessor(); |
||
| 45 | } |
||
| 46 | |||
| 47 | $entity = $this->configuration['entity']; |
||
| 48 | if (!empty($entity)) { |
||
| 49 | $em = $this->entityManager; |
||
| 50 | $class = $em->getRepository($entity)->getClassName(); |
||
| 51 | $object = new $class(); |
||
| 52 | |||
| 53 | // Transfer values from message to databaseNotification. |
||
| 54 | $properties = ['notifiable', 'uuid', 'type', 'data']; |
||
| 55 | foreach ($properties as $property) { |
||
| 56 | $value = $this->accessor->getValue($data, $property); |
||
| 57 | $this->accessor->setValue($object, $property, $value); |
||
| 58 | } |
||
| 59 | |||
| 60 | return $object; |
||
| 61 | } |
||
| 62 | |||
| 63 | return false; |
||
| 64 | } |
||
| 65 | } |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.