1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Db\Controller; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use Doctrine\ORM\EntityManager; |
9
|
|
|
use Doctrine\ORM\ORMException; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
12
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
13
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Middleware\TokenMiddleware; |
16
|
|
|
use SlayerBirden\DataFlowServer\Db\Entities\DbConfiguration; |
17
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
18
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
19
|
|
|
use Zend\Hydrator\HydratorInterface; |
20
|
|
|
|
21
|
|
|
class GetConfigsAction implements MiddlewareInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var EntityManager |
25
|
|
|
*/ |
26
|
|
|
private $entityManager; |
27
|
|
|
/** |
28
|
|
|
* @var LoggerInterface |
29
|
|
|
*/ |
30
|
|
|
private $logger; |
31
|
|
|
/** |
32
|
|
|
* @var HydratorInterface |
33
|
|
|
*/ |
34
|
|
|
private $hydrator; |
35
|
|
|
|
36
|
6 |
|
public function __construct( |
37
|
|
|
EntityManager $entityManager, |
|
|
|
|
38
|
|
|
LoggerInterface $logger, |
39
|
|
|
HydratorInterface $hydrator |
40
|
|
|
) { |
41
|
6 |
|
$this->entityManager = $entityManager; |
42
|
6 |
|
$this->logger = $logger; |
43
|
6 |
|
$this->hydrator = $hydrator; |
44
|
6 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
6 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
50
|
|
|
{ |
51
|
6 |
|
$data = $request->getQueryParams(); |
52
|
6 |
|
$page = isset($data['p']) ? abs($data['p']) : 1; |
53
|
6 |
|
$limit = isset($data['l']) ? abs($data['l']) : 10; |
54
|
6 |
|
$filters = $data['f'] ?? []; |
55
|
6 |
|
$sorting = $data['s'] ?? []; |
56
|
|
|
|
57
|
|
|
|
58
|
6 |
|
$currentOwner = $request->getAttribute(TokenMiddleware::USER_PARAM); |
59
|
|
|
|
60
|
6 |
|
if (!$currentOwner) { |
61
|
|
|
return new JsonResponse([ |
62
|
|
|
'data' => [ |
63
|
|
|
'configurations' => [], |
64
|
|
|
'count' => 0, |
65
|
|
|
], |
66
|
|
|
'msg' => new DangerMessage('Access denied. Only Logged In users can access this resource.'), |
67
|
|
|
'success' => false |
68
|
|
|
], 403); |
69
|
|
|
} |
70
|
6 |
|
$filters['owner'] = $currentOwner; |
71
|
|
|
|
72
|
|
|
try { |
73
|
6 |
|
$criteria = $this->buildCriteria($filters, $sorting, $page, $limit); |
74
|
|
|
|
75
|
|
|
/** @var Collection $configs */ |
76
|
6 |
|
$configs = $this->entityManager |
77
|
6 |
|
->getRepository(DbConfiguration::class) |
78
|
6 |
|
->matching($criteria); |
79
|
|
|
// before collection load to count all records without pagination |
80
|
6 |
|
$count = $configs->count(); |
81
|
5 |
|
if ($count > 0) { |
82
|
|
|
$arrayConfigs = array_map(function ($config) { |
83
|
4 |
|
return $this->hydrator->extract($config); |
84
|
4 |
|
}, $configs->toArray()); |
85
|
4 |
|
return new JsonResponse([ |
86
|
4 |
|
'data' => [ |
87
|
4 |
|
'configurations' => $arrayConfigs, |
88
|
4 |
|
'count' => $count, |
89
|
|
|
], |
90
|
|
|
'success' => true, |
91
|
|
|
'msg' => null, |
92
|
4 |
|
], 200); |
93
|
|
|
} else { |
94
|
1 |
|
return new JsonResponse([ |
95
|
1 |
|
'data' => [ |
96
|
|
|
'configurations' => [], |
97
|
|
|
'count' => 0, |
98
|
|
|
], |
99
|
|
|
'success' => false, |
100
|
1 |
|
'msg' => new DangerMessage('Could not find configurations using given conditions.'), |
101
|
1 |
|
], 404); |
102
|
|
|
} |
103
|
1 |
|
} catch (ORMException $exception) { |
104
|
1 |
|
$this->logger->error((string)$exception); |
105
|
1 |
|
return new JsonResponse([ |
106
|
1 |
|
'data' => [ |
107
|
|
|
'configurations' => [], |
108
|
|
|
'count' => 0, |
109
|
|
|
], |
110
|
|
|
'success' => false, |
111
|
1 |
|
'msg' => new DangerMessage('There was an error while fetching configurations.'), |
112
|
1 |
|
], 400); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
private function buildCriteria( |
117
|
|
|
array $filters = [], |
118
|
|
|
array $sorting = [], |
119
|
|
|
int $page = 1, |
120
|
|
|
int $limit = 10 |
121
|
|
|
): Criteria { |
122
|
6 |
|
$criteria = Criteria::create(); |
123
|
6 |
|
$criteria->setFirstResult(($page - 1) * $limit) |
124
|
6 |
|
->setMaxResults($limit); |
125
|
6 |
|
foreach ($filters as $key => $value) { |
126
|
6 |
|
if (is_string($value)) { |
127
|
3 |
|
$criteria->andWhere(Criteria::expr()->contains($key, $value)); |
128
|
|
|
} else { |
129
|
6 |
|
$criteria->andWhere(Criteria::expr()->eq($key, $value)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
6 |
|
if ($sorting) { |
|
|
|
|
133
|
1 |
|
foreach ($sorting as $key => $dir) { |
134
|
1 |
|
$criteria->orderBy($sorting); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
6 |
|
return $criteria; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.