Completed
Push — master ( 8d3dbf...7592af )
by Paweł
62:36
created

StaticThemeAssetsController::handleFileLoading()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.u. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\CoreBundle\Controller;
16
17
use Hoa\File\Read;
18
use Hoa\Mime\Mime;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
24
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
25
26
class StaticThemeAssetsController extends Controller
27
{
28
    /**
29
     * Directory with assets inside theme.
30
     */
31
    const ASSETS_DIRECTORY = 'public';
32
33
    /**
34
     * @Route("/{fileName}.{fileExtension}", name="static_theme_assets_root", requirements={
35
     *     "fileName": "sw|manifest"
36
     * })
37
     * @Method("GET")
38
     */
39 1
    public function rootAction($fileName, $fileExtension)
40
    {
41 1
        $theme = $this->get('sylius.context.theme')->getTheme();
42 1
        $filePath = $theme->getPath().'/'.self::ASSETS_DIRECTORY.'/'.$fileName.'.'.$fileExtension;
43 1
        if (null !== $response = $this->handleFileLoading($filePath)) {
44 1
            return $response;
45
        }
46
47
        throw new NotFoundHttpException('Page was not found.');
48
    }
49
50
    /**
51
     * @Route("/public/{filePath}", name="static_theme_assets_public", requirements={"filePath"=".+"})
52
     * @Method("GET")
53
     */
54 1
    public function publicAction($filePath)
55
    {
56 1
        $theme = $this->get('sylius.context.theme')->getTheme();
57 1
        $filePath = $theme->getPath().'/'.self::ASSETS_DIRECTORY.'/'.$filePath;
58 1
        if (null !== $response = $this->handleFileLoading($filePath, basename($filePath))) {
0 ignored issues
show
Unused Code introduced by
The call to StaticThemeAssetsController::handleFileLoading() has too many arguments starting with basename($filePath).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
59 1
            return $response;
60
        }
61
62 1
        throw new NotFoundHttpException('File was not found.', null, 404);
63
    }
64
65
    /**
66
     * @param $filePath
67
     *
68
     * @return Response
69
     */
70 2
    private function handleFileLoading($filePath)
71
    {
72 2
        if (file_exists($filePath)) {
73 2
            $response = new Response(file_get_contents($filePath));
74 2
            $disposition = $response->headers->makeDisposition(
75 2
                ResponseHeaderBag::DISPOSITION_INLINE,
76
                basename($filePath)
77
            );
78 2
            $response->headers->set('Content-Disposition', $disposition);
79 2
            $type = new Mime(new Read($filePath));
80 2
            $mime = str_replace('/x-', '/', Mime::getMimeFromExtension($type->getExtension()));
81 2
            $response->headers->set('Content-Type', $mime);
82 2
            $response->setStatusCode(Response::HTTP_OK);
83
84 2
            return $response;
85
        }
86 1
    }
87
}
88