1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Authentication\Middleware; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\ORMException; |
8
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Token; |
14
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Middleware\ResourceMiddlewareInterface; |
15
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
16
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
17
|
|
|
|
18
|
|
|
class TokenResourceMiddleware implements ResourceMiddlewareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var EntityManager |
22
|
|
|
*/ |
23
|
|
|
private $entityManager; |
24
|
|
|
/** |
25
|
|
|
* @var LoggerInterface |
26
|
|
|
*/ |
27
|
|
|
private $logger; |
28
|
|
|
|
29
|
|
|
public function __construct(EntityManager $entityManager, LoggerInterface $logger) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->entityManager = $entityManager; |
32
|
|
|
$this->logger = $logger; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritdoc |
37
|
|
|
*/ |
38
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
39
|
|
|
{ |
40
|
|
|
$id = $request->getAttribute('id'); |
41
|
|
|
|
42
|
|
|
if ($id !== null) { |
43
|
|
|
try { |
44
|
|
|
$token = $this->entityManager->find(Token::class, $id); |
45
|
|
|
if ($token) { |
46
|
|
|
return $handler->handle( |
47
|
|
|
$request->withAttribute(self::DATA_RESOURCE, $token) |
48
|
|
|
); |
49
|
|
|
} else { |
50
|
|
|
return new JsonResponse([ |
51
|
|
|
'data' => [ |
52
|
|
|
'token' => null, |
53
|
|
|
], |
54
|
|
|
'success' => false, |
55
|
|
|
'msg' => new DangerMessage('Could not load Token by provided ID.'), |
56
|
|
|
], 404); |
57
|
|
|
} |
58
|
|
|
} catch (ORMInvalidArgumentException | ORMException $exception) { |
59
|
|
|
$this->logger->error((string)$exception); |
60
|
|
|
return new JsonResponse([ |
61
|
|
|
'data' => [ |
62
|
|
|
'token' => null, |
63
|
|
|
], |
64
|
|
|
'success' => false, |
65
|
|
|
'msg' => new DangerMessage('Error during loading Token.'), |
66
|
|
|
], 404); |
67
|
|
|
} |
68
|
|
|
} else { |
69
|
|
|
return new JsonResponse([ |
70
|
|
|
'data' => [ |
71
|
|
|
'token' => null, |
72
|
|
|
], |
73
|
|
|
'success' => false, |
74
|
|
|
'msg' => new DangerMessage('No id provided.'), |
75
|
|
|
], 404); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
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.