|
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
|
5 |
|
/** |
|
25
|
|
|
* @param EntityManager $manager |
|
26
|
5 |
|
*/ |
|
27
|
5 |
|
public function __construct(EntityManager $manager) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$this->manager = $manager; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
/** |
|
33
|
|
|
* @inheritdoc |
|
34
|
3 |
|
*/ |
|
35
|
1 |
|
public function supports($recipient) |
|
36
|
|
|
{ |
|
37
|
|
|
if (is_object($recipient) && $recipient instanceof DoctrineRecipientInterface) { |
|
38
|
2 |
|
return true; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
1 |
|
*/ |
|
47
|
|
|
public function configure(OptionsResolver $resolver) |
|
48
|
|
|
{ |
|
49
|
|
|
$resolver |
|
50
|
|
|
->setDefined(['attachments_path']) |
|
51
|
1 |
|
; |
|
52
|
|
|
} |
|
53
|
1 |
|
|
|
54
|
1 |
|
/** |
|
55
|
1 |
|
* @inheritdoc |
|
56
|
1 |
|
*/ |
|
57
|
1 |
|
public function handle(Delivery $delivery) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$options = $delivery->getOptions(); |
|
60
|
1 |
|
|
|
61
|
1 |
|
$notification = new Notification( |
|
62
|
|
|
$delivery->getSubject(), |
|
63
|
|
|
$delivery->getBody(), |
|
64
|
|
|
$delivery->getRecipient() |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($delivery->getAttachments() as $attachment) { |
|
68
|
|
|
/** @var $attachment File */ |
|
69
|
|
|
$fs = new Filesystem(); |
|
70
|
|
|
$fs->copy( |
|
71
|
|
|
$attachment->getPathname(), |
|
72
|
|
|
sprintf('%s/%s', $options['attachments_path'], $attachment->getBasename()) |
|
73
|
|
|
); |
|
74
|
|
|
$notificationAttachment = new NotificationAttachment($notification, $attachment->getBasename()); |
|
75
|
|
|
$notification->addNotificationAttachment($notificationAttachment); |
|
76
|
|
|
|
|
77
|
|
|
$this->manager->persist($notificationAttachment); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->manager->persist($notification); |
|
81
|
|
|
$this->manager->flush(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
The
EntityManagermight 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:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.