DoctrineChannel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 79.17%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 60
ccs 19
cts 24
cp 0.7917
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 4 1
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\IdentifierRecipientInterface;
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
        return $recipient instanceof IdentifierRecipientInterface;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42 2
    public function configure(OptionsResolver $resolver)
43
    {
44
        $resolver
45 2
            ->setDefined(['attachments_path'])
46
        ;
47 2
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 1
    public function handle(Delivery $delivery)
53
    {
54 1
        $options = $delivery->getOptions();
55
56 1
        $notification = new Notification(
57 1
            $delivery->getSubject(),
58 1
            $delivery->getBody(),
59 1
            $delivery->getRecipient()
60
        );
61
62 1
        $fs = new Filesystem();
63 1
        foreach ($delivery->getAttachments() as $attachment) {
64
            $fs->copy(
65
                $attachment->getPathname(),
66
                sprintf('%s/%s', $options['attachments_path'], $attachment->getBasename())
67
            );
68
            $notificationAttachment = new NotificationAttachment($notification, $attachment->getBasename());
69
            $notification->addNotificationAttachment($notificationAttachment);
70
        }
71
72 1
        $this->manager->persist($notification);
73 1
        $this->manager->flush($notification);
74 1
    }
75
}
76