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\Doctrine\Middleware\ResourceMiddlewareInterface; |
14
|
|
|
use SlayerBirden\DataFlowServer\Notification\DangerMessage; |
15
|
|
|
use SlayerBirden\DataFlowServer\Notification\SuccessMessage; |
16
|
|
|
use Zend\Diactoros\Response\JsonResponse; |
17
|
|
|
use Zend\Hydrator\ExtractionInterface; |
18
|
|
|
|
19
|
|
|
class DeleteConfigAction implements MiddlewareInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var EntityManagerInterface |
23
|
|
|
*/ |
24
|
|
|
private $entityManager; |
25
|
|
|
/** |
26
|
|
|
* @var LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
/** |
30
|
|
|
* @var ExtractionInterface |
31
|
|
|
*/ |
32
|
|
|
private $extraction; |
33
|
|
|
|
34
|
1 |
|
public function __construct( |
35
|
|
|
EntityManagerInterface $entityManager, |
36
|
|
|
LoggerInterface $logger, |
37
|
|
|
ExtractionInterface $extraction |
38
|
|
|
) { |
39
|
1 |
|
$this->entityManager = $entityManager; |
40
|
1 |
|
$this->logger = $logger; |
41
|
1 |
|
$this->extraction = $extraction; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
1 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
48
|
|
|
{ |
49
|
1 |
|
$dbConfig = $request->getAttribute(ResourceMiddlewareInterface::DATA_RESOURCE); |
50
|
1 |
|
$deleted = false; |
51
|
1 |
|
$status = 200; |
52
|
|
|
|
53
|
|
|
try { |
54
|
1 |
|
$this->entityManager->remove($dbConfig); |
55
|
1 |
|
$this->entityManager->flush(); |
56
|
1 |
|
$msg = new SuccessMessage('Configuration removed.'); |
57
|
1 |
|
$deleted = true; |
58
|
|
|
} catch (ORMException $exception) { |
59
|
|
|
$this->logger->error((string)$exception); |
60
|
|
|
$msg = new DangerMessage('There was an error while removing configuration.'); |
61
|
|
|
$status = 500; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return new JsonResponse([ |
65
|
1 |
|
'msg' => $msg, |
66
|
1 |
|
'success' => $deleted, |
67
|
|
|
'data' => [ |
68
|
1 |
|
'configuration' => !empty($dbConfig) ? $this->extraction->extract($dbConfig) : null |
69
|
|
|
], |
70
|
1 |
|
], $status); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|