Passed
Push — master ( 75052b...8ecf83 )
by Dmitri
01:50
created

AuthorController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Application\Dto\AuthorDto;
8
use App\Application\Exception\AuthorNotFound;
9
use App\Application\Service\AuthorService;
10
use Damax\Common\Bridge\Symfony\Bundle\Annotation\Serialize;
11
use Nelmio\ApiDocBundle\Annotation\Model;
12
use Swagger\Annotations as OpenApi;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * @Route("/authors")
18
 */
19
final class AuthorController
20
{
21
    private $service;
22
23
    public function __construct(AuthorService $service)
24
    {
25
        $this->service = $service;
26
    }
27
28
    /**
29
     * @OpenApi\Get(
30
     *     tags={"author"},
31
     *     summary="List authors.",
32
     *     @OpenApi\Response(
33
     *         response=200,
34
     *         description="Authors list.",
35
     *         @OpenApi\Schema(type="array", @OpenApi\Items(ref=@Model(type=AuthorDto::class)))
36
     *     )
37
     * )
38
     *
39
     * @Route("", methods={"GET"})
40
     * @Serialize()
41
     */
42
    public function index(): array
43
    {
44
        return $this->service->fetchAll();
45
    }
46
47
    /**
48
     * @OpenApi\Get(
49
     *     tags={"author"},
50
     *     summary="Get author.",
51
     *     @OpenApi\Response(
52
     *         response=200,
53
     *         description="Author info.",
54
     *         @OpenApi\Schema(ref=@Model(type=AuthorDto::class))
55
     *     ),
56
     *     @OpenApi\Response(
57
     *         response=404,
58
     *         description="Author not found."
59
     *     )
60
     * )
61
     *
62
     * @Route("/{id}", methods={"GET"})
63
     * @Serialize()
64
     *
65
     * @throws NotFoundHttpException
66
     */
67
    public function view(string $id): AuthorDto
68
    {
69
        try {
70
            return $this->service->fetch($id);
71
        } catch (AuthorNotFound $e) {
72
            throw new NotFoundHttpException();
73
        }
74
    }
75
}
76