MediaController::download()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 6
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Controller;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
6
use Symfony\Component\HttpFoundation\BinaryFileResponse;
7
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
8
9
class MediaController extends AbstractController
10
{
11
    protected $translator;
12
13
    public function download(string $path)
14
    {
15
        $projectDir = $this->get('kernel')->getProjectDir();
16
        $pathToFile = $projectDir.'/media/'.substr(str_replace('..', '', $path), \strlen('media/'));
17
18
        if (! file_exists($pathToFile)) {
19
            throw $this->createNotFoundException('The media does not exist...');
20
        }
21
22
        $response = new BinaryFileResponse($pathToFile);
23
        $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT);
24
25
        return $response;
26
    }
27
}
28