1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Db\Controller; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Collections\Selectable; |
7
|
|
|
use Doctrine\ORM\ORMException; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Middleware\TokenMiddleware; |
14
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Collection\CriteriaBuilder; |
15
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Hydrator\ListExtractor; |
16
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralErrorResponseFactory; |
17
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\Validation\GeneralSuccessResponseFactory; |
18
|
|
|
use Zend\Hydrator\HydratorInterface; |
19
|
|
|
|
20
|
|
|
final class GetConfigsAction implements MiddlewareInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var LoggerInterface |
24
|
|
|
*/ |
25
|
|
|
private $logger; |
26
|
|
|
/** |
27
|
|
|
* @var HydratorInterface |
28
|
|
|
*/ |
29
|
|
|
private $hydrator; |
30
|
|
|
/** |
31
|
|
|
* @var Selectable |
32
|
|
|
*/ |
33
|
|
|
private $dbConfigRepository; |
34
|
|
|
|
35
|
12 |
|
public function __construct( |
36
|
|
|
Selectable $dbConfigRepository, |
37
|
|
|
LoggerInterface $logger, |
38
|
|
|
HydratorInterface $hydrator |
39
|
|
|
) { |
40
|
12 |
|
$this->logger = $logger; |
41
|
12 |
|
$this->hydrator = $hydrator; |
42
|
12 |
|
$this->dbConfigRepository = $dbConfigRepository; |
43
|
12 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
12 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
49
|
|
|
{ |
50
|
12 |
|
$data = $request->getQueryParams(); |
51
|
12 |
|
$currentOwner = $request->getAttribute(TokenMiddleware::USER_PARAM); |
52
|
12 |
|
$data[CriteriaBuilder::FILTERS]['owner'] = $currentOwner; |
53
|
|
|
|
54
|
|
|
try { |
55
|
12 |
|
$configs = $this->dbConfigRepository->matching((new CriteriaBuilder())($data)); |
56
|
|
|
// before collection load to count all records without pagination |
57
|
12 |
|
$count = $configs->count(); |
58
|
10 |
|
if ($count > 0) { |
59
|
8 |
|
$arrayConfigs = (new ListExtractor())($this->hydrator, $configs->toArray()); |
60
|
8 |
|
return (new GeneralSuccessResponseFactory())('Success', 'configurations', $arrayConfigs, 200, $count); |
61
|
|
|
} else { |
62
|
2 |
|
$msg = 'Could not find configurations using given conditions.'; |
63
|
2 |
|
return (new GeneralErrorResponseFactory())($msg, 'configurations', 404, [], 0); |
64
|
|
|
} |
65
|
2 |
|
} catch (ORMException $exception) { |
66
|
2 |
|
$this->logger->error((string)$exception); |
67
|
2 |
|
$msg = 'There was an error while fetching configurations.'; |
68
|
2 |
|
return (new GeneralErrorResponseFactory())($msg, 'configurations', 400, [], 0); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|