Passed
Push — main ( fcc530...d42fc3 )
by Tomas Norre
28:03 queued 21:49
created

PageService::checkIfPageShouldBeSkipped()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 35
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 17
c 1
b 0
f 0
nc 11
nop 1
dl 0
loc 35
ccs 17
cts 17
cp 1
crap 9
rs 8.0555
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AOE\Crawler\Service;
6
7
/*
8
 * (c) 2021 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\Configuration\ExtensionConfigurationProvider;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * @internal since v9.2.5
27
 */
28
class PageService
29
{
30
    /**
31
     * Check if the given page should be crawled
32
     *
33
     * @return false|string false if the page should be crawled (not excluded), true / skipMessage if it should be skipped
34
     */
35 15
    public function checkIfPageShouldBeSkipped(array $pageRow)
36
    {
37 15
        $extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration();
38
39
        // if page is hidden
40 15
        if (! $extensionSettings['crawlHiddenPages'] && $pageRow['hidden']) {
41 1
            return 'Because page is hidden';
42
        }
43
44 14
        if (in_array($pageRow['doktype'], $this->getDisallowedDokTypes())) {
45 3
            return 'Because doktype is not allowed';
46
        }
47
48 11
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['excludeDoktype'] ?? [] as $key => $doktypeList) {
49 1
            if (GeneralUtility::inList($doktypeList, $pageRow['doktype'])) {
50 1
                return 'Doktype was excluded by "' . $key . '"';
51
            }
52
        }
53
54
        // veto hook
55 10
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pageVeto'] ?? [] as $key => $func) {
56
            $params = [
57 2
                'pageRow' => $pageRow,
58
            ];
59
            // expects "false" if page is ok and "true" or a skipMessage if this page should _not_ be crawled
60 2
            $veto = GeneralUtility::callUserFunction($func, $params, $this);
61 2
            if ($veto !== false) {
62 2
                if (is_string($veto)) {
63 1
                    return $veto;
64
                }
65 1
                return 'Veto from hook "' . htmlspecialchars($key) . '"';
66
            }
67
        }
68
69 8
        return false;
70
    }
71
72 14
    private function getDisallowedDokTypes(): array
73
    {
74
        // Todo: Use PageRepository::DOKTYPE_ when dropping support for TYPO3 v9
75 14
        return [3,4,199,254,255];
76
    }
77
}
78