Completed
Push — master ( ebba66...366e1c )
by Yann
27:01 queued 22:24
created

DoctrineChannel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 49
ccs 18
cts 18
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 8 3
A configure() 0 3 1
A handle() 0 11 1
1
<?php
2
3
namespace Yokai\MessengerBundle\Channel;
4
5
use Doctrine\ORM\EntityManager;
6
use Yokai\MessengerBundle\Delivery;
7
use Yokai\MessengerBundle\Entity\Notification;
8
use Yokai\MessengerBundle\Recipient\DoctrineRecipientInterface;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
/**
12
 * @author Yann Eugoné <[email protected]>
13
 */
14
class DoctrineChannel implements ChannelInterface
15
{
16
    /**
17
     * @var EntityManager
18
     */
19
    private $manager;
20
21
    /**
22
     * @param EntityManager $manager
23
     */
24 4
    public function __construct(EntityManager $manager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $manager. 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...
25
    {
26 4
        $this->manager = $manager;
27 4
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 2
    public function supports($recipient)
33
    {
34 2
        if (is_object($recipient) && $recipient instanceof DoctrineRecipientInterface) {
35 1
            return true;
36
        }
37
38 1
        return false;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 1
    public function configure(OptionsResolver $resolver)
45
    {
46 1
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 1
    public function handle(Delivery $delivery)
52
    {
53 1
        $notification = new Notification(
54 1
            $delivery->getSubject(),
55 1
            $delivery->getBody(),
56 1
            $delivery->getRecipient()
57 1
        );
58
59 1
        $this->manager->persist($notification);
60 1
        $this->manager->flush($notification);
61 1
    }
62
}
63