Passed
Push — features/addFlushedPagesToCraw... ( 98a0d9...e29076 )
by Tomas Norre
07:36
created

DataHandlerHook::getInstallUtility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
crap 1.037
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Hooks;
6
7
/*
8
 * (c) 2020 AOE GmbH <[email protected]>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21
22
use AOE\Crawler\Api\CrawlerApi;
23
use AOE\Crawler\Domain\Repository\QueueRepository;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Extbase\Object\ObjectManager;
26
use TYPO3\CMS\Extensionmanager\Utility\InstallUtility;
27
28
class DataHandlerHook
29
{
30 2
    public function addFlushedPagesToCrawlerQueue(array $parameters, \TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler): void
31
    {
32 2
        if (!in_array($parameters['table'], ['pages', 'tt_content'])) {
33
            return;
34
        }
35
36 2
        if ($this->getInstallUtility()->isLoaded('workspaces') && $dataHandler->BE_USER->workspace > 0 && !$this->isWorkspacePublishAction($dataHandler->cmdmap)) {
37
            return;
38
        }
39
40 2
        $pageIdsToBeFlushedFromCache = $parameters['pageIdArray'];
41 2
        if (empty($pageIdsToBeFlushedFromCache)) {
42
            return;
43
        }
44 2
        foreach ($pageIdsToBeFlushedFromCache as $pageId) {
45 2
            $pageId = (int) $pageId;
46 2
            if ($pageId < 1) {
47 2
                continue;
48
            }
49 2
            if ($this->getQueueRepository()->isPageInQueue($pageId)) {
50 2
                continue;
51
            }
52 2
            $this->getCrawlerApi()->addPageToQueue($pageId);
53
        }
54 2
    }
55
56
    /**
57
     * Checks if a workspace record is being published
58
     *
59
     * Example $commandMap structure as provided by TYPO3:
60
     * 'pages' => array(
61
     *     123 => array(
62
     *         'version' => array(
63
     *             'action' => 'swap'
64
     *  [...]
65
     * 'tt_content' => array(
66
     *     456 => array(
67
     *         'version' => array(
68
     *             'action' => 'swap'
69
     * [...]
70
     */
71 2
    private function isWorkspacePublishAction(array $commandMap): bool
72
    {
73 2
        $isWorkspacePublishAction = false;
74 2
        foreach ($commandMap as $tableCommandMap) {
75 2
            if (! is_array($tableCommandMap)) {
76
                continue;
77
            }
78 2
            foreach ($tableCommandMap as $singleCommandMap) {
79 2
                if (! is_array($singleCommandMap)) {
80
                    continue;
81
                }
82 2
                if (! $this->isSwapAction($singleCommandMap)) {
83
                    continue;
84
                }
85 2
                $isWorkspacePublishAction = true;
86 2
                return $isWorkspacePublishAction;
87
            }
88
        }
89
        return $isWorkspacePublishAction;
90
    }
91
92
    /**
93
     * Checks if a page is being swapped with it's workspace overlay
94
     */
95 2
    private function isSwapAction(array $singleCommandMap): bool
96
    {
97 2
        $isSwapAction = false;
98
        if (
99 2
            isset($singleCommandMap['version'])
100 2
            && is_array($singleCommandMap['version'])
101 2
            && isset($singleCommandMap['version']['action'])
102 2
            && $singleCommandMap['version']['action'] === 'swap'
103
        ) {
104 2
            $isSwapAction = true;
105
        }
106 2
        return $isSwapAction;
107
    }
108
109 2
    private function getInstallUtility(): InstallUtility
110
    {
111 2
        return GeneralUtility::makeInstance(ObjectManager::class)->get(InstallUtility::class);
112
    }
113
114 2
    private function getQueueRepository(): QueueRepository
115
    {
116 2
        return GeneralUtility::makeInstance(ObjectManager::class)->get(QueueRepository::class);
117
    }
118
119 2
    private function getCrawlerApi(): CrawlerApi
120
    {
121 2
        return GeneralUtility::makeInstance(ObjectManager::class)->get(CrawlerApi::class);
122
    }
123
}
124