Completed
Push — master ( 2ef37c...52cad6 )
by Oleg
02:13
created

DeleteConfigAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
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\RequestHandlerInterface;
11
use Psr\Log\LoggerInterface;
12
use Psr\Http\Server\MiddlewareInterface;
13
use SlayerBirden\DataFlowServer\Db\Entities\DbConfiguration;
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 2
    public function __construct(
35
        EntityManagerInterface $entityManager,
36
        LoggerInterface $logger,
37
        ExtractionInterface $extraction
38
    ) {
39 2
        $this->entityManager = $entityManager;
40 2
        $this->logger = $logger;
41 2
        $this->extraction = $extraction;
42 2
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 2
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
48
    {
49 2
        $id = $request->getAttribute('id');
50 2
        $deleted = false;
51 2
        $status = 200;
52
53
        try {
54 2
            $config = $this->entityManager->find(DbConfiguration::class, $id);
55
            //todo check owner
56 1
            if ($config) {
57
                $this->entityManager->remove($config);
58
                $this->entityManager->flush();
59
                $msg = new SuccessMessage('Configuration removed.');
60
                $deleted = true;
61
            } else {
62 1
                $msg = new DangerMessage('Could not find configuration to delete.');
63 1
                $status = 404;
64
            }
65
        } catch (ORMException $exception) {
66
            $this->logger->error((string)$exception);
67
            $msg = new DangerMessage('There was an error while removing configuration.');
68
            $status = 400;
69
        }
70
71 1
        return new JsonResponse([
72 1
            'msg' => $msg,
73 1
            'success' => $deleted,
74
            'data' => [
75
                'configuration' => !empty($config) ? $this->extraction->extract($config) : null
76
            ],
77 1
        ], $status);
78
    }
79
}
80