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\Doctrine\Collection\CriteriaBuilder; |
14
|
|
|
use SlayerBirden\DataFlowServer\Doctrine\Hydrator\ListExtractor; |
15
|
|
|
use SlayerBirden\DataFlowServer\Stdlib\ResponseFactory; |
16
|
|
|
use Zend\Hydrator\HydratorInterface; |
17
|
|
|
|
18
|
|
|
final class GetConfigsAction implements MiddlewareInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var LoggerInterface |
22
|
|
|
*/ |
23
|
|
|
private $logger; |
24
|
|
|
/** |
25
|
|
|
* @var HydratorInterface |
26
|
|
|
*/ |
27
|
|
|
private $hydrator; |
28
|
|
|
/** |
29
|
|
|
* @var Selectable |
30
|
|
|
*/ |
31
|
|
|
private $dbConfigRepository; |
32
|
|
|
|
33
|
12 |
|
public function __construct( |
34
|
|
|
Selectable $dbConfigRepository, |
35
|
|
|
LoggerInterface $logger, |
36
|
|
|
HydratorInterface $hydrator |
37
|
|
|
) { |
38
|
12 |
|
$this->logger = $logger; |
39
|
12 |
|
$this->hydrator = $hydrator; |
40
|
12 |
|
$this->dbConfigRepository = $dbConfigRepository; |
41
|
12 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
12 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
47
|
|
|
{ |
48
|
12 |
|
$data = $request->getQueryParams(); |
49
|
|
|
|
50
|
|
|
try { |
51
|
12 |
|
$configs = $this->dbConfigRepository->matching((new CriteriaBuilder())($data)); |
52
|
|
|
// before collection load to count all records without pagination |
53
|
12 |
|
$count = $configs->count(); |
54
|
10 |
|
if ($count > 0) { |
55
|
8 |
|
$arrayConfigs = (new ListExtractor())($this->hydrator, $configs->toArray()); |
56
|
8 |
|
return (new ResponseFactory())('Success', 200, 'configurations', $arrayConfigs, $count); |
57
|
|
|
} else { |
58
|
2 |
|
$msg = 'Could not find configurations using given conditions.'; |
59
|
2 |
|
return (new ResponseFactory())($msg, 404, 'configurations', [], 0); |
60
|
|
|
} |
61
|
2 |
|
} catch (ORMException $exception) { |
62
|
2 |
|
$this->logger->error((string)$exception); |
63
|
2 |
|
$msg = 'There was an error while fetching configurations.'; |
64
|
2 |
|
return (new ResponseFactory())($msg, 400, 'configurations', [], 0); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|