|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SlayerBirden\DataFlowServer\Db\Controller; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
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\Db\Entities\DbConfiguration; |
|
14
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
|
15
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
|
16
|
|
|
use Zend\Hydrator\ExtractionInterface; |
|
17
|
|
|
|
|
18
|
|
|
class GetConfigAction implements MiddlewareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var EntityManagerInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $entityManager; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var LoggerInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $logger; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ExtractionInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $extraction; |
|
32
|
|
|
|
|
33
|
2 |
|
public function __construct( |
|
34
|
|
|
EntityManagerInterface $entityManager, |
|
35
|
|
|
LoggerInterface $logger, |
|
36
|
|
|
ExtractionInterface $extraction |
|
37
|
|
|
) { |
|
38
|
2 |
|
$this->entityManager = $entityManager; |
|
39
|
2 |
|
$this->logger = $logger; |
|
40
|
2 |
|
$this->extraction = $extraction; |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
47
|
|
|
{ |
|
48
|
2 |
|
$id = $request->getAttribute('id'); |
|
49
|
2 |
|
$success = false; |
|
50
|
2 |
|
$msg = ''; |
|
51
|
2 |
|
$status = 200; |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
2 |
|
$config = $this->entityManager |
|
55
|
2 |
|
->getRepository(DbConfiguration::class) |
|
56
|
2 |
|
->find($id); |
|
57
|
2 |
|
if ($config !== null) { |
|
58
|
1 |
|
$success = true; |
|
59
|
|
|
} else { |
|
60
|
1 |
|
$msg = new DangerMessage('Could not find configuration.'); |
|
61
|
1 |
|
$status = 404; |
|
62
|
|
|
} |
|
63
|
|
|
} catch (ORMException $exception) { |
|
64
|
|
|
$this->logger->error((string)$exception); |
|
65
|
|
|
$msg = new DangerMessage('Could not find configuration. An error occurred.'); |
|
66
|
|
|
$status = 400; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
return new JsonResponse([ |
|
70
|
2 |
|
'data' => [ |
|
71
|
1 |
|
'configuration' => !empty($config) ? $this->extraction->extract($config) : null, |
|
72
|
|
|
], |
|
73
|
2 |
|
'success' => $success, |
|
74
|
2 |
|
'msg' => $msg, |
|
75
|
2 |
|
], $status); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|