Completed
Pull Request — master (#26)
by Yann
01:49
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 Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\OptionsResolver\OptionsResolver;
8
use Yokai\MessengerBundle\Delivery;
9
use Yokai\MessengerBundle\Entity\Notification;
10
use Yokai\MessengerBundle\Entity\NotificationAttachment;
11
use Yokai\MessengerBundle\Recipient\DoctrineRecipientInterface;
12
13
/**
14
 * @author Yann Eugoné <[email protected]>
15
 */
16
class DoctrineChannel implements ChannelInterface
17
{
18
    /**
19
     * @var EntityManager
20
     */
21
    private $manager;
22
23
    /**
24
     * @param EntityManager $manager
25
     */
26 5
    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...
27
    {
28 5
        $this->manager = $manager;
29 5
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 3
    public function supports($recipient)
35
    {
36 3
        if (is_object($recipient) && $recipient instanceof DoctrineRecipientInterface) {
37 1
            return true;
38
        }
39
40 2
        return false;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46 2
    public function configure(OptionsResolver $resolver)
47
    {
48
        $resolver
49 2
            ->setDefined(['attachments_path'])
50
        ;
51 2
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 1
    public function handle(Delivery $delivery)
57
    {
58 1
        $options = $delivery->getOptions();
59
60 1
        $notification = new Notification(
61 1
            $delivery->getSubject(),
62 1
            $delivery->getBody(),
63 1
            $delivery->getRecipient()
64
        );
65
66 1
        $fs = new Filesystem();
67 1
        foreach ($delivery->getAttachments() as $attachment) {
68
            $fs->copy(
69
                $attachment->getPathname(),
70
                sprintf('%s/%s', $options['attachments_path'], $attachment->getBasename())
71
            );
72
            $notificationAttachment = new NotificationAttachment($notification, $attachment->getBasename());
73
            $notification->addNotificationAttachment($notificationAttachment);
74
        }
75
76 1
        $this->manager->persist($notification);
77 1
        $this->manager->flush($notification);
78 1
    }
79
}
80