Completed
Push — master ( 50fbec...218e95 )
by dan
08:09
created

DatabaseMessageDispatcher   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A dispatch() 0 18 2
B createDatabaseNotificationEntity() 0 24 4
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
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might 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:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
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();
0 ignored issues
show
Unused Code introduced by
$dispatchData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
        $messageData = $message->getMessageData();
0 ignored issues
show
Unused Code introduced by
$messageData is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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
}