Completed
Push — master ( 8b149d...d0a098 )
by Kevin
07:29
created

TceMainHook::isBackendUserInWorkspace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
namespace Aoe\Varnish\TYPO3\Hooks;
3
4
use Aoe\Varnish\Domain\Model\Tag\PageTag;
5
use Aoe\Varnish\Domain\Model\Tag\PageIdTag;
6
use Aoe\Varnish\System\Varnish;
7
use TYPO3\CMS\Core\DataHandling\DataHandler;
8
9
class TceMainHook extends AbstractHook
10
{
11
    /**
12
     * @param array $parameters
13
     * @param DataHandler $parent
14
     *
15
     * @todo implement cache clearing for "clearCache_pageGrandParent", "clearCache_pageSiblingChildren" and
16
     *       and "clearCache_disable"  http://docs.typo3.org/typo3cms/TSconfigReference/PageTsconfig/TCEmain/Index.html
17
     * @todo find a way how to clear all affected pages after changeing a page title in the main menu or footer
18
     * because it is not possible through the existings hooks to access the correct page tags which needs be cleared from cache
19
     */
20 12
    public function clearCachePostProc(array $parameters, DataHandler $parent)
21
    {
22 12
        if ($this->isBackendUserInWorkspace($parent)) {
23 1
            return;
24
        }
25
26
        /** @var Varnish $varnish */
27 11
        $varnish = $this->objectManager->get(Varnish::class);
28
29
        // delete all Typo3 pages
30 11
        if (isset($parameters['cacheCmd']) && $parameters['cacheCmd'] === 'pages') {
31 1
            $pageTag = new PageTag('typo3_page');
0 ignored issues
show
Unused Code introduced by
The call to PageTag::__construct() has too many arguments starting with 'typo3_page'.

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...
32 1
            $varnish->banByTag($pageTag);
33 1
        } else {
34 10
            $pageId = $this->extractPageIdFromParameters($parameters);
35 10
            if ($pageId > 0) {
36 3
                $pageIdTag = new PageIdTag($pageId);
37 3
                $varnish->banByTag($pageIdTag);
38 3
            }
39
        }
40 11
    }
41
42
    /**
43
     * extract page id from all variants of parameters that can be given
44
     *
45
     * @param array $parameters
46
     * @return integer
47
     */
48 10
    private function extractPageIdFromParameters(array $parameters)
49
    {
50 10
        if (isset($parameters['table']) && $parameters['table'] === 'pages'
51 10
            && isset($parameters['uid']) && is_numeric($parameters['uid'])
52 10
        ) {
53 3
            return (integer)$parameters['uid'];
54
        }
55 7
        if (isset($parameters['cacheCmd']) && is_numeric($parameters['cacheCmd'])) {
56 3
            return (integer)$parameters['cacheCmd'];
57
        }
58 4
        if (isset($parameters['uid_page']) && is_numeric($parameters['uid_page'])) {
59 3
            return (integer)$parameters['uid_page'];
60
        }
61 1
        return 0;
62
    }
63
64
    /**
65
     * @param DataHandler $parent
66
     * @return boolean
67
     */
68 12
    private function isBackendUserInWorkspace(DataHandler $parent)
69
    {
70 12
        if ($parent->BE_USER->workspace > 0) {
71 1
            return true;
72
        }
73 11
        return false;
74
    }
75
}
76