Conditions | 4 |
Paths | 5 |
Total Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 0 |
1 | <?php |
||
38 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
39 | { |
||
40 | $id = $request->getAttribute('id'); |
||
41 | $msg = new DangerMessage('Could not load Configuration by provided ID.'); |
||
42 | |||
43 | if ($id !== null) { |
||
44 | try { |
||
45 | $dbConfig = $this->entityManager->find(DbConfiguration::class, $id); |
||
46 | if ($dbConfig) { |
||
47 | return $handler->handle( |
||
48 | $request->withAttribute(self::DATA_RESOURCE, $dbConfig) |
||
49 | ); |
||
50 | } |
||
51 | } catch (ORMInvalidArgumentException | ORMException $exception) { |
||
52 | $this->logger->error((string)$exception); |
||
53 | $msg = new DangerMessage('Error during loading DB Configuration.'); |
||
54 | } |
||
55 | } |
||
56 | |||
57 | return new JsonResponse([ |
||
58 | 'data' => [ |
||
59 | 'configuration' => null, |
||
60 | ], |
||
61 | 'success' => false, |
||
62 | 'msg' => $msg, |
||
63 | ], 404); |
||
64 | } |
||
65 | } |
||
66 |
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.