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