Completed
Push — master ( 28a6d6...6b5086 )
by Yann
03:15
created

DoctrineChannel   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 82.76%

Importance

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