Completed
Push — master ( 5c0b6f...5c71f2 )
by Oleg
10:16
created

BaseResourceMiddleware   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 62.5%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 82
ccs 20
cts 32
cp 0.625
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A process() 0 41 4
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlowServer\Doctrine\Middleware;
5
6
use Doctrine\Common\Persistence\ManagerRegistry;
7
use Doctrine\ORM\ORMInvalidArgumentException;
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 SlayerBirden\DataFlowServer\Notification\DangerMessage;
13
use Zend\Diactoros\Response\JsonResponse;
14
15
final class BaseResourceMiddleware implements ResourceMiddlewareInterface
16
{
17
    /**
18
     * @var LoggerInterface
19
     */
20
    private $logger;
21
    /**
22
     * @var string
23
     */
24
    private $entityName;
25
    /**
26
     * @var string
27
     */
28
    private $dataObjectName;
29
    /**
30
     * @var string
31
     */
32
    private $idAttributeName;
33
    /**
34
     * @var ManagerRegistry
35
     */
36
    private $managerRegistry;
37
38 28
    public function __construct(
39
        ManagerRegistry $managerRegistry,
40
        LoggerInterface $logger,
41
        string $entityName,
42
        string $dataObjectName,
43
        string $idAttributeName = 'id'
44
    ) {
45 28
        $this->managerRegistry = $managerRegistry;
46 28
        $this->logger = $logger;
47 28
        $this->entityName = $entityName;
48 28
        $this->dataObjectName = $dataObjectName;
49 28
        $this->idAttributeName = $idAttributeName;
50 28
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 28
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
56
    {
57 28
        $id = $request->getAttribute($this->idAttributeName);
58
59 28
        if ($id !== null) {
60
            try {
61 28
                $em = $this->managerRegistry->getManagerForClass($this->entityName);
62 28
                $entity = $em->find($this->entityName, $id);
63 28
                if ($entity) {
64 20
                    return $handler->handle(
65 20
                        $request->withAttribute(self::DATA_RESOURCE, $entity)
66
                    );
67
                } else {
68 8
                    return new JsonResponse([
69 8
                        'data' => [
70 8
                            $this->dataObjectName => null,
71
                        ],
72
                        'success' => false,
73 8
                        'msg' => new DangerMessage(sprintf('Could not load %s by provided ID.', $this->dataObjectName)),
74 8
                    ], 404);
75
                }
76
            } catch (ORMInvalidArgumentException $exception) {
77
                $this->logger->error((string)$exception);
78
                return new JsonResponse([
79
                    'data' => [
80
                        $this->dataObjectName => null,
81
                    ],
82
                    'success' => false,
83
                    'msg' => new DangerMessage(sprintf('Error during loading %s.', $this->dataObjectName)),
84
                ], 500);
85
            }
86
        } else {
87
            return new JsonResponse([
88
                'data' => [
89
                    $this->dataObjectName => null,
90
                ],
91
                'success' => false,
92
                'msg' => new DangerMessage(sprintf('No %s provided.', $this->idAttributeName)),
93
            ], 400);
94
        }
95
    }
96
}
97