Completed
Push — master ( c5041c...dff1db )
by
unknown
27:23
created

MediaController::getMediaById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Controller;
4
5
use MediaMonks\SonataMediaBundle\ParameterBag\DownloadParameterBag;
6
use MediaMonks\SonataMediaBundle\ParameterBag\ImageParameterBag;
7
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\RedirectResponse;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\StreamedResponse;
12
13
class MediaController extends Controller
14
{
15
    /**
16
     * @param Request $request
17
     * @param int $id
18
     * @param int $width
19
     * @param int $height
20
     * @return RedirectResponse
21
     */
22
    public function imageRedirectAction(Request $request, $id, $width, $height)
23
    {
24
        return $this->get('mediamonks.sonata_media.utility.image')->getRedirectResponse(
25
            $this->getMediaById($id),
26
            new ImageParameterBag($width, $height, $request->query->all())
27
        );
28
    }
29
30
    /**
31
     * @param Request $request
32
     * @param $id
33
     * @return StreamedResponse
34
     */
35
    public function downloadAction(Request $request, $id)
36
    {
37
        return $this->get('mediamonks.sonata_media.utility.download')->getStreamedResponse(
38
            $this->getMediaById($id),
39
            new DownloadParameterBag($request->query->all())
40
        );
41
    }
42
43
    /**
44
     * @param $id
45
     * @return MediaInterface
46
     */
47
    protected function getMediaById($id)
48
    {
49
        return $this->getDoctrine()->getManager()->find(
50
            $this->getParameter('mediamonks.sonata_media.entity.class'),
51
            $id
52
        );
53
    }
54
}
55