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

DoctrineChannel::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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