Completed
Pull Request — master (#27)
by Matthieu
06:29
created

DoctrineChannel   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 80.77%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 64
ccs 21
cts 26
cp 0.8077
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 8 3
A configure() 0 6 1
A handle() 0 23 2
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 6
    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 6
        $this->manager = $manager;
29 6
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 4
    public function supports($recipient)
35
    {
36 4
        if (is_object($recipient) && $recipient instanceof DoctrineRecipientInterface) {
37 1
            return true;
38
        }
39
40 3
        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