Passed
Push — master ( 03e013...bd5966 )
by Anatoly
01:18 queued 14s
created

ReadController   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 22
c 1
b 0
f 0
ccs 6
cts 6
cp 1
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 11 2
1
<?php declare(strict_types=1);
2
3
namespace App\Controller\Entry;
4
5
/**
6
 * Import classes
7
 */
8
use App\ContainerAwareTrait;
9
use App\Http\ResponseFactory;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Server\RequestHandlerInterface;
13
14
/**
15
 * @Route(
16
 *   name="api.entry.read",
17
 *   path="/api/v1/entry/{id<\d+>}",
18
 *   methods={"GET"},
19
 * )
20
 *
21
 * @OpenApi\Operation(
22
 *   tags={"Entry"},
23
 *   summary="Read an entry",
24
 *   responses={
25
 *     200: @OpenApi\Response(
26
 *       description="OK",
27
 *       content={
28
 *         "application/json": @OpenApi\MediaType(
29
 *           schema=@OpenApi\Schema(
30
 *             type="object",
31
 *             required={"status", "data"},
32
 *             properties={
33
 *               "status": @OpenApi\SchemaReference(
34
 *                 class="App\Http\ResponseFactory",
35
 *                 method="ok",
36
 *               ),
37
 *               "data": @OpenApi\SchemaReference(
38
 *                 class="App\Entity\Entry",
39
 *               ),
40
 *             },
41
 *           ),
42
 *         ),
43
 *       },
44
 *     ),
45
 *     "default": @OpenApi\ResponseReference(
46
 *       class="App\Http\ResponseFactory",
47
 *       method="error",
48
 *     ),
49
 *   },
50
 * )
51
 */
52
final class ReadController implements RequestHandlerInterface
53
{
54
    use ContainerAwareTrait;
55
56
    /**
57
     * {@inheritDoc}
58
     *
59
     * @param ServerRequestInterface $request
60
     *
61
     * @return ResponseInterface
62
     */
63 2
    public function handle(ServerRequestInterface $request) : ResponseInterface
64
    {
65 2
        $id = (int) $request->getAttribute('id');
66
67 2
        $service = $this->container->get('service.entry');
68
69 2
        if (!$service->existsById($id)) {
70 1
            return (new ResponseFactory)->error('The requested entry was not found.', [], 404);
71
        }
72
73 1
        return (new ResponseFactory)->ok($service->readById($id), 200);
74
    }
75
}
76