Completed
Pull Request — master (#10)
by Tomas Norre
09:40
created

BackendAjaxHook::getVarnish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Aoe\Varnish\TYPO3\Hooks;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2018 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\Varnish\System\Varnish;
29
use Psr\Http\Message\ServerRequestInterface;
30
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
31
use TYPO3\CMS\Core\Http\Response;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Object\ObjectManager;
34
35
class BackendAjaxHook extends AbstractHook
36
{
37
    public function banAll(ServerRequestInterface $request): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
38
    {
39
        $varnish = $this->getVarnish();
40
        $varnish->banAll();
41
42
        if ($this->isAuthorizedBackendSession()) {
43
            $this->getBackendUser()->writelog(
44
                3,
45
                1,
46
                0,
47
                0,
48
                'User %s has cleared the Varnish cache',
49
                [$this->getBackendUser()->user['username']]
50
            );
51
        }
52
53
        // We need to return a response to satisfy the
54
        // TYPO3\CMS\Backend\Http\RouteDispatcher::dispatch()
55
        // don't like the solution, but haven't come up with something better yet.
56
        return new Response();
57
    }
58
59
    protected function getVarnish(): Varnish
60
    {
61
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
62
        $this->varnish = $objectManager->get(Varnish::class);
63
        return $this->varnish;
64
    }
65
66
    /**
67
     * Checks if a user is logged in and the session is active.
68
     *
69
     * @return bool
70
     */
71
    protected function isAuthorizedBackendSession()
72
    {
73
        $backendUser = $this->getBackendUser();
74
        return $backendUser !== null && $backendUser instanceof BackendUserAuthentication && isset($backendUser->user['uid']);
75
    }
76
77
    /**
78
     * @return BackendUserAuthentication|null
79
     */
80
    protected function getBackendUser()
81
    {
82
        return isset($GLOBALS['BE_USER']) ? $GLOBALS['BE_USER'] : null;
83
    }
84
}
85