1 | <?php |
||
17 | class JobConsume extends Command |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | protected $signature = 'job:consume'; |
||
23 | |||
24 | /** |
||
25 | * @var PsrContext |
||
26 | */ |
||
27 | private $context; |
||
28 | |||
29 | /** |
||
30 | * @var Serializer |
||
31 | */ |
||
32 | private $serializer; |
||
33 | |||
34 | /** |
||
35 | * @var EntityManager |
||
36 | */ |
||
37 | private $entityManager; |
||
38 | |||
39 | /** |
||
40 | * @var Container |
||
41 | */ |
||
42 | private $container; |
||
43 | |||
44 | /** |
||
45 | * @param PsrContext $context |
||
46 | * @param Serializer $serializer |
||
47 | * @param EntityManager $entityManager |
||
48 | * @param Container $container |
||
49 | */ |
||
50 | 3 | public function __construct( |
|
62 | |||
63 | /** |
||
64 | * @throws \Doctrine\Common\Persistence\Mapping\MappingException |
||
65 | * @throws \Doctrine\ORM\ORMException |
||
66 | * @throws \Doctrine\ORM\OptimisticLockException |
||
67 | */ |
||
68 | public function handle() |
||
69 | { |
||
70 | $consumer = $this->createConsumer(); |
||
71 | while (true) { |
||
72 | $this->consume($consumer); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @return PsrConsumer |
||
78 | */ |
||
79 | protected function createConsumer(): PsrConsumer |
||
86 | |||
87 | /** |
||
88 | * @param PsrConsumer $consumer |
||
89 | * @throws \Doctrine\Common\Persistence\Mapping\MappingException |
||
90 | * @throws \Doctrine\ORM\ORMException |
||
91 | * @throws \Doctrine\ORM\OptimisticLockException |
||
92 | */ |
||
93 | 3 | protected function consume(PsrConsumer $consumer) |
|
124 | |||
125 | /** |
||
126 | * @param PsrMessage $message |
||
127 | * @param int $delay |
||
128 | * @return PsrMessage |
||
129 | */ |
||
130 | protected function delayMessage(PsrMessage $message, int $delay) |
||
138 | } |
||
139 |
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:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during 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.