DownloadController::__construct()   A
last analyzed

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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Media\Bridge\Symfony\Bundle\Controller\Standard;
6
7
use Damax\Media\Application\Exception\ImageProcessingFailure;
8
use Damax\Media\Application\Exception\MediaNotFound;
9
use Damax\Media\Application\Query\DownloadMedia;
10
use Damax\Media\Application\Query\GetImage;
11
use League\Tactician\CommandBus as QueryBus;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
/**
19
 * @Route("/media")
20
 */
21
final class DownloadController
22
{
23
    private $queryBus;
24
25
    public function __construct(QueryBus $queryBus)
26
    {
27
        $this->queryBus = $queryBus;
28
    }
29
30
    /**
31
     * @Route("/{id}/download", methods={"GET"}, name="media_download")
32
     *
33
     * @throws NotFoundHttpException
34
     */
35
    public function download(string $id): Response
36
    {
37
        try {
38
            return $this->queryBus->handle(new DownloadMedia($id));
39
        } catch (MediaNotFound $e) {
40
            throw new NotFoundHttpException();
41
        }
42
    }
43
44
    /**
45
     * @Route("/{id}/image", methods={"GET"}, name="media_image")
46
     *
47
     * @throws NotFoundHttpException
48
     * @throws BadRequestHttpException
49
     */
50
    public function image(Request $request, string $id): Response
51
    {
52
        try {
53
            return $this->queryBus->handle(new GetImage($id, $request->query->all()));
54
        } catch (MediaNotFound $e) {
55
            throw new NotFoundHttpException();
56
        } catch (ImageProcessingFailure $e) {
57
            throw new BadRequestHttpException();
58
        }
59
    }
60
}
61