Completed
Push — master ( c822b3...7bbc84 )
by Vyacheslav
11:42
created

AssetController::__construct()   B

Complexity

Conditions 7
Paths 20

Size

Total Lines 19
Code Lines 10

Duplication

Lines 10
Ratio 52.63 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 19
rs 8.2222
cc 7
eloc 10
nc 20
nop 3
1
<?php
2
namespace NVBooster\PHPCRAssetsBundle\Controller;
3
4
use Symfony\Component\HttpFoundation\Request;
5
use Symfony\Component\HttpFoundation\Response;
6
use Assetic\Asset\StringAsset;
7
use Symfony\Component\HttpKernel\KernelInterface;
8
use Symfony\Bundle\AsseticBundle\FilterManager;
9
10
/**
11
 * AssetController
12
 *
13
 * @author nvb <[email protected]>
14
 */
15
class AssetController
16
{
17
    /**
18
     * @var FilterManager
19
     */
20
    private $filterManager;
21
    
22
    /**
23
     * @var boolean
24
     */
25
    private $isDebug = false; 
26
    
27
    /**
28
     * @var array
29
     */
30
    private $cssFilters = array();
31
    
32
    /**
33
     * @var array
34
     */
35
    private $jsFilters = array();
36
     
37
    /**
38
     * @param KernelInterface $kernel
39
     * @param array           $filters
40
     */
41
    public function __construct(FilterManager $filterManager, KernelInterface $kernel = null, $filters = null)
0 ignored issues
show
Unused Code introduced by
The parameter $filterManager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        if ($kernel) {
44
            $this->isDebug = $kernel->isDebug();
45
        }
46
        
47
        if (is_array($filters)) {
48 View Code Duplication
            if (key_exists('js', $filters)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
                if (is_array($filters['js'])) {
50
                    $this->jsFilters = $filters['js'];
51
                }
52
            }
53 View Code Duplication
            if (key_exists('css', $filters)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
                if (is_array($filters['css'])) {
55
                    $this->jsFilters = $filters['css'];
56
                }
57
            }
58
        }
59
    }
60
    
61
    /**
62
     * @param Request $request
63
     * @param object  $assetDocument
0 ignored issues
show
Bug introduced by
There is no parameter named $assetDocument. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
64
     * @param array   $filters
65
     *
66
     * @return Response
67
     */
68
    public function serveAsset(Request $request, $contentDocument, $filters = array())
69
    {
70
71
        $response = new Response();
72
        $response->setStatusCode(200);
73
        $response->setLastModified($contentDocument->getUpdatedAt());
74
        $response->setPublic();
75
76
        if (!$this->isDebug && $response->isNotModified($request)) {
77
            return $response;
78
        }
79
80
        $asset = new StringAsset($contentDocument->getContent());
81
        $asset->load();
82
83
        foreach ($filters as $filter) {
84
            if (($filter[0] !== '?') || (!$this->isDebug)) {
85
                if ($processor = $this->filterManager->get(preg_replace('@^\?@', '', $filter))) {
86
                    $processor->filterDump($asset);
87
                }
88
            }
89
        }
90
91
        $response->setContent($asset->getContent());
92
93
        $response->prepare($request);
94
95
        return $response;
96
    }
97
98
    /**
99
     * Helper action
100
     *
101
     * @param Request $request
102
     * @param object  $contentDocument
103
     * @param array   $filters
104
     *
105
     * @return Response
106
     */
107 View Code Duplication
    public function serveJs(Request $request, $contentDocument, $filters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109
        if ($filters === false) {
110
            $mergedFilters = array();
111
        } elseif ($filters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
112
            $mergedFilters = $filters;
113
        } else {
114
            $mergedFilters = $this->jsFilters;
115
        }
116
        
117
        $response = $this->serveAsset($request, $contentDocument, $mergedFilters);
118
        $response->headers->set('Content-Type', 'text/javascript');
119
        
120
        return $response;
121
    }
122
123
    /**
124
     * Helper action
125
     *
126
     * @param Request $request
127
     * @param object  $contentDocument
128
     * @param array   $filters
129
     *
130
     * @return Response
131
     */
132 View Code Duplication
    public function serveCss(Request $request, $contentDocument, $filters = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        if ($filters === false) {
135
            $mergedFilters = array();
136
        } elseif ($filters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $filters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
137
            $mergedFilters = $filters;
138
        } else {
139
            $mergedFilters = $this->cssFilters;
140
        }
141
        
142
        $response = $this->serveAsset($request, $contentDocument, $mergedFilters);
143
        $response->headers->set('Content-Type', 'text/css');
144
        
145
        return $response;
146
    }
147
}