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\Stdlib\Validation\GeneralErrorResponseFactory; |
16
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory; |
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
|
12 |
|
public function __construct( |
35
|
|
|
Selectable $dbConfigRepository, |
36
|
|
|
LoggerInterface $logger, |
37
|
|
|
HydratorInterface $hydrator |
38
|
|
|
) { |
39
|
12 |
|
$this->logger = $logger; |
40
|
12 |
|
$this->hydrator = $hydrator; |
41
|
12 |
|
$this->dbConfigRepository = $dbConfigRepository; |
42
|
12 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
12 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
48
|
|
|
{ |
49
|
12 |
|
$data = $request->getQueryParams(); |
50
|
12 |
|
$page = isset($data['p']) ? abs($data['p']) : 1; |
51
|
12 |
|
$limit = isset($data['l']) ? abs($data['l']) : 10; |
52
|
12 |
|
$filters = $data['f'] ?? []; |
53
|
12 |
|
$sorting = $data['s'] ?? []; |
54
|
|
|
|
55
|
|
|
|
56
|
12 |
|
$currentOwner = $request->getAttribute(TokenMiddleware::USER_PARAM); |
57
|
12 |
|
$filters['owner'] = $currentOwner; |
58
|
|
|
|
59
|
|
|
try { |
60
|
12 |
|
$criteria = $this->buildCriteria($filters, $sorting, $page, $limit); |
61
|
|
|
|
62
|
12 |
|
$configs = $this->dbConfigRepository->matching($criteria); |
63
|
|
|
// before collection load to count all records without pagination |
64
|
12 |
|
$count = $configs->count(); |
65
|
10 |
|
if ($count > 0) { |
66
|
|
|
$arrayConfigs = array_map(function ($config) { |
67
|
8 |
|
return $this->hydrator->extract($config); |
68
|
8 |
|
}, $configs->toArray()); |
69
|
8 |
|
return (new GeneralSuccessResponseFactory())('Success', 'configurations', $arrayConfigs, 200, $count); |
70
|
|
|
} else { |
71
|
2 |
|
$msg = 'Could not find configurations using given conditions.'; |
72
|
2 |
|
return (new GeneralErrorResponseFactory())($msg, 'configurations', 404, [], 0); |
73
|
|
|
} |
74
|
2 |
|
} catch (ORMException $exception) { |
75
|
2 |
|
$this->logger->error((string)$exception); |
76
|
2 |
|
$msg = 'There was an error while fetching configurations.'; |
77
|
2 |
|
return (new GeneralErrorResponseFactory())($msg, 'configurations', 400, [], 0); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
12 |
|
private function buildCriteria( |
82
|
|
|
array $filters = [], |
83
|
|
|
array $sorting = [], |
84
|
|
|
int $page = 1, |
85
|
|
|
int $limit = 10 |
86
|
|
|
): Criteria { |
87
|
12 |
|
$criteria = Criteria::create(); |
88
|
12 |
|
$criteria->setFirstResult(($page - 1) * $limit) |
89
|
12 |
|
->setMaxResults($limit); |
90
|
12 |
|
foreach ($filters as $key => $value) { |
91
|
12 |
|
if (is_string($value)) { |
92
|
6 |
|
$criteria->andWhere(Criteria::expr()->contains($key, $value)); |
93
|
|
|
} else { |
94
|
12 |
|
$criteria->andWhere(Criteria::expr()->eq($key, $value)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
12 |
|
if (! empty($sorting)) { |
98
|
2 |
|
foreach ($sorting as $key => $dir) { |
99
|
2 |
|
$criteria->orderBy($sorting); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
12 |
|
return $criteria; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|