|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ConferenceTools\Tickets\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Carnage\Cqrs\MessageBus\MessageBusInterface; |
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
|
7
|
|
|
use ConferenceTools\Tickets\Domain\Service\Configuration; |
|
8
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
|
9
|
|
|
use ZfrStripe\Client\StripeClient; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class AbstractController |
|
13
|
|
|
* @package HirePower\Common\Mvc\Controller |
|
14
|
|
|
* @method \Zend\Form\Form commandForm(string $commandClass, array $additionalData = [], array $defaults = []) |
|
15
|
|
|
* @method \Carnage\Cqrs\Mvc\Controller\Plugin\Events events() |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractController extends AbstractActionController |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var MessageBusInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $commandBus; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var EntityManager |
|
26
|
|
|
*/ |
|
27
|
|
|
private $entityManager; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var StripeClient |
|
31
|
|
|
*/ |
|
32
|
|
|
private $stripeClient; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Configuration |
|
36
|
|
|
*/ |
|
37
|
|
|
private $configuration; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* AbstractController constructor. |
|
41
|
|
|
* @param MessageBusInterface $commandBus |
|
42
|
|
|
* @param EntityManager $entityManager |
|
43
|
|
|
* @param StripeClient $stripeClient |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( |
|
46
|
|
|
MessageBusInterface $commandBus, |
|
47
|
|
|
EntityManager $entityManager, |
|
|
|
|
|
|
48
|
|
|
StripeClient $stripeClient, |
|
49
|
|
|
Configuration $configuration |
|
50
|
|
|
) { |
|
51
|
|
|
$this->commandBus = $commandBus; |
|
52
|
|
|
$this->entityManager = $entityManager; |
|
53
|
|
|
$this->stripeClient = $stripeClient; |
|
54
|
|
|
$this->configuration = $configuration; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return MessageBusInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getCommandBus() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->commandBus; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return EntityManager |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getEntityManager() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->entityManager; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return StripeClient |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getStripeClient(): StripeClient |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->stripeClient; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return Configuration |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getConfiguration(): Configuration |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->configuration; |
|
87
|
|
|
} |
|
88
|
|
|
} |
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.