1 | <?php |
||
16 | class RecordProvider extends ResourceFactoryAware |
||
17 | { |
||
18 | /** |
||
19 | * @var ProviderProvider |
||
20 | */ |
||
21 | protected $provider; |
||
22 | |||
23 | /** |
||
24 | * @var EventDispatcherInterface |
||
25 | */ |
||
26 | protected $eventDispatcher; |
||
27 | |||
28 | /** |
||
29 | * @var EntityManager |
||
30 | */ |
||
31 | protected $manager; |
||
32 | |||
33 | /** |
||
34 | * @var EntityRepository |
||
35 | */ |
||
36 | protected $repository; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | protected $dataClass; |
||
42 | |||
43 | public function __construct( |
||
44 | EventDispatcherInterface $eventDispatcher, |
||
45 | ProviderProvider $provider, |
||
46 | EntityManager $manager, |
||
|
|||
47 | $dataClass |
||
48 | ) { |
||
49 | $this->eventDispatcher = $eventDispatcher; |
||
50 | $this->provider = $provider; |
||
51 | $this->manager = $manager; |
||
52 | $this->dataClass = $dataClass; |
||
53 | $this->repository = $manager->getRepository($dataClass); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @return ProviderProvider |
||
58 | */ |
||
59 | public function getProvider() |
||
63 | |||
64 | /** |
||
65 | * @param $name |
||
66 | * |
||
67 | * @return array |
||
68 | */ |
||
69 | public function getProviderParameters($name) |
||
73 | |||
74 | /** |
||
75 | * @return RecordInterface |
||
76 | */ |
||
77 | public function createNew() |
||
81 | |||
82 | /** |
||
83 | * @param $transactionId |
||
84 | * |
||
85 | * @return null|RecordInterface |
||
86 | */ |
||
87 | public function findTransactionId($transactionId) |
||
88 | { |
||
89 | return $this->repository->findOneBy( |
||
90 | array( |
||
91 | 'transactionId' => $transactionId, |
||
92 | ) |
||
93 | ); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param ResultInterface $result |
||
98 | * |
||
99 | * @return RecordInterface |
||
100 | */ |
||
101 | public function storeResult(ResultInterface $result) |
||
102 | { |
||
103 | $number = PhoneNumberUtil::getInstance()->parse($result->getRecipient(), 'TH'); |
||
104 | $provider = $this->provider->getActivedProvider(); |
||
105 | $object = $this->createNew(); |
||
106 | |||
107 | $object->setMessage($result->getBody()); |
||
108 | $object->setTransactionId($result->getId()); |
||
109 | $object->setNumber($number); |
||
110 | $object->setState($object::STATE_SENT); |
||
111 | $object->setPrice($provider->getPrice()); |
||
112 | $object->setProvider($provider); |
||
113 | $object->setCurrency($provider->getCurrency()); |
||
114 | |||
115 | $event = new GenericEvent($object); |
||
116 | |||
117 | $this->dispatchEvent('dos_sms_record_pre_store', $event); |
||
118 | |||
119 | $this->manager->persist($object); |
||
120 | $this->manager->flush(); |
||
121 | |||
122 | $this->dispatchEvent('dos_sms_record_post_store', $event); |
||
123 | |||
124 | return $object; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param string $name |
||
129 | * @param Event $event |
||
130 | * |
||
131 | * @return Event |
||
132 | */ |
||
133 | public function dispatchEvent($name, Event $event) |
||
137 | |||
138 | /** |
||
139 | * @param ProviderInterface $provider |
||
140 | */ |
||
141 | public function visit(ProviderInterface $provider) |
||
159 | } |
||
160 |
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.