EditorResponseListener::onKernelResponse()   C
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 18
cts 18
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 7
nop 1
crap 8
1
<?php
2
3
/*
4
 * This file is part of the Ivoaz ContentEditable bundle.
5
 *
6
 * (c) Ivo Azirjans <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Ivoaz\Bundle\ContentEditableBundle\EventListener;
13
14
use Ivoaz\Bundle\ContentEditableBundle\Editor\EditorInterface;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
18
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
19
20
/**
21
 * @author Ivo Azirjans <[email protected]>
22
 */
23
class EditorResponseListener
24
{
25
    /**
26
     * @var EditorInterface
27
     */
28
    private $editor;
29
30
    /**
31
     * @var AuthorizationCheckerInterface
32
     */
33
    private $authorizationChecker;
34
35
    /**
36
     * @param EditorInterface               $editor
37
     * @param AuthorizationCheckerInterface $authorizationChecker
38
     */
39 8
    public function __construct(EditorInterface $editor, AuthorizationCheckerInterface $authorizationChecker)
40
    {
41 8
        $this->editor = $editor;
42 8
        $this->authorizationChecker = $authorizationChecker;
43 8
    }
44
45
    /**
46
     * @param FilterResponseEvent $event
47
     */
48 8
    public function onKernelResponse(FilterResponseEvent $event)
49
    {
50 8
        if (!$event->isMasterRequest()) {
51 1
            return;
52
        }
53
54
        try {
55 7
            if (!$this->authorizationChecker->isGranted('ROLE_ADMIN')) {
56 1
                return;
57
            }
58 6
        } catch (AuthenticationCredentialsNotFoundException $e) {
59 1
            return;
60
        }
61
62 5
        $request = $event->getRequest();
63
64 5
        if ($request->isXmlHttpRequest()) {
65 1
            return;
66
        }
67
68 4
        $response = $event->getResponse();
69
70 4
        if ($response->isRedirection() || false === strpos($response->headers->get('Content-Type', ''), 'text/html')) {
71 2
            return;
72
        }
73
74 2
        $html = $this->editor->renderEditor($response);
75
76 2
        if (!empty($html)) {
77 1
            $this->injectEditor($response, $html);
78 1
        }
79 2
    }
80
81
    /**
82
     * @param Response $response
83
     * @param string   $editorHtml
84
     */
85 1
    private function injectEditor(Response $response, $editorHtml)
86
    {
87 1
        $html = $response->getContent();
88 1
        $pos = mb_strripos($html, '</body>');
89
90 1
        if (false === $pos) {
91
            return;
92
        }
93
94 1
        $html = mb_substr($html, 0, $pos).$editorHtml.mb_substr($html, $pos);
95
96 1
        $response->setContent($html);
97 1
    }
98
}
99