Completed
Push — master ( c4a196...b954a0 )
by Bocharsky
05:22
created

EditInPlaceResponseListener::onKernelResponse()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 0
cts 39
cp 0
rs 8.7361
c 0
b 0
f 0
cc 5
nc 6
nop 1
crap 30

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\EventListener;
13
14
use Symfony\Component\Asset\Packages;
15
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
16
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
17
use Translation\Bundle\EditInPlace\ActivatorInterface;
18
19
/**
20
 * Adds Javascript/CSS files to the Response if the Activator returns true.
21
 *
22
 * @author Damien Alexandre <[email protected]>
23
 */
24
final class EditInPlaceResponseListener
25
{
26
    const HTML = <<<'HTML'
27
<!-- TranslationBundle -->
28
<link rel="stylesheet" type="text/css" href="%s">
29
30
<script type="text/javascript" src="%s"></script>
31
<script type="text/javascript" src="%s"></script>
32
33
<script type="text/javascript">
34
window.onload = function() {
35
    TranslationBundleEditInPlace("%s");
36
}
37
</script>
38
<!-- /TranslationBundle -->
39
HTML;
40
41
    /**
42
     * @var ActivatorInterface
43
     */
44
    private $activator;
45
46
    /**
47
     * @var UrlGeneratorInterface
48
     */
49
    private $router;
50
51
    /**
52
     * @var Packages
53
     */
54
    private $packages;
55
56
    /**
57
     * @var string
58
     */
59
    private $configName;
60
61
    /**
62
     * Determines whether the message for untranslatable content like placeholders will be rendered.
63
     *
64
     * @var bool
65
     */
66
    private $showUntranslatable;
67
68
    public function __construct(ActivatorInterface $activator, UrlGeneratorInterface $router, Packages $packages, $configName = 'default', $showUntranslatable = true)
69
    {
70
        $this->activator = $activator;
71
        $this->router = $router;
72
        $this->packages = $packages;
73
        $this->configName = $configName;
74
        $this->showUntranslatable = $showUntranslatable;
75
    }
76
77
    public function onKernelResponse(FilterResponseEvent $event)
78
    {
79
        $request = $event->getRequest();
80
81
        if (!$this->activator->checkRequest($request)) {
82
            return;
83
        }
84
85
        $content = $event->getResponse()->getContent();
86
87
        if (false === $content) {
88
            return;
89
        }
90
91
        // Clean the content for malformed tags in attributes or encoded tags
92
        $replacement = "\"$1🚫 Can't be translated here. 🚫\"";
93
        $pattern = "@\\s*[\"']\\s*(.[a-zA-Z]+:|)(<x-trans.+data-value=\"([^&\"]+)\".+?(?=<\\/x-trans)<\\/x-trans>)\\s*[\"']@mi";
94
        if (!$this->showUntranslatable) {
95
            $replacement = '"$3"';
96
        }
97
        $content = preg_replace($pattern, $replacement, $content);
98
99
        // Remove escaped content (e.g. Javascript)
100
        $pattern = '@&lt;x-trans.+data-key=&quot;([^&]+)&quot;.+data-value=&quot;([^&]+)&quot;.+&lt;\\/x-trans&gt;@mi';
101
        $replacement = '🚫 $1 🚫';
102
        if (!$this->showUntranslatable) {
103
            $replacement = '$2';
104
        }
105
        $content = preg_replace($pattern, $replacement, $content);
106
107
        $html = sprintf(
108
            self::HTML,
109
            $this->packages->getUrl('bundles/translation/css/content-tools.min.css'),
110
            $this->packages->getUrl('bundles/translation/js/content-tools.min.js'),
111
            $this->packages->getUrl('bundles/translation/js/editInPlace.js'),
112
113
            $this->router->generate('translation_edit_in_place_update', [
114
                'configName' => $this->configName,
115
                'locale' => $event->getRequest()->getLocale(),
116
            ])
117
        );
118
        $content = str_replace('</body>', $html."\n".'</body>', $content);
119
120
        $response = $event->getResponse();
121
122
        // Remove the cache because we do not want the modified page to be cached
123
        $response->headers->set('cache-control', 'no-cache, no-store, must-revalidate');
124
        $response->headers->set('pragma', 'no-cache');
125
        $response->headers->set('expires', '0');
126
127
        $event->getResponse()->setContent($content);
128
    }
129
}
130