Passed
Push — PageService ( 0614b7 )
by Tomas Norre
17:22
created

PageService   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDisallowedDokTypes() 0 8 1
B checkIfPageShouldBeSkipped() 0 35 9
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\Domain\Repository\PageRepository;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
26
/**
27
 * @internal since v9.2.5
28
 */
29
class PageService
30
{
31
    /**
32
     * Check if the given page should be crawled
33
     *
34
     * @return false|string false if the page should be crawled (not excluded), true / skipMessage if it should be skipped
35
     */
36
    public function checkIfPageShouldBeSkipped(array $pageRow)
37
    {
38
        $extensionSettings = GeneralUtility::makeInstance(ExtensionConfigurationProvider::class)->getExtensionConfiguration();
39
40
        // if page is hidden
41
        if (! $extensionSettings['crawlHiddenPages'] && $pageRow['hidden']) {
42
            return 'Because page is hidden';
43
        }
44
45
        if (in_array($pageRow['doktype'], $this->getDisallowedDokTypes())) {
46
            return 'Because doktype is not allowed';
47
        }
48
49
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['excludeDoktype'] ?? [] as $key => $doktypeList) {
50
            if (GeneralUtility::inList($doktypeList, $pageRow['doktype'])) {
51
                return 'Doktype was excluded by "' . $key . '"';
52
            }
53
        }
54
55
        // veto hook
56
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pageVeto'] ?? [] as $key => $func) {
57
            $params = [
58
                'pageRow' => $pageRow,
59
            ];
60
            // expects "false" if page is ok and "true" or a skipMessage if this page should _not_ be crawled
61
            $veto = GeneralUtility::callUserFunction($func, $params, $this);
62
            if ($veto !== false) {
63
                if (is_string($veto)) {
64
                    return $veto;
65
                }
66
                return 'Veto from hook "' . htmlspecialchars($key) . '"';
67
            }
68
        }
69
70
        return false;
71
    }
72
73
    private function getDisallowedDokTypes(): array
74
    {
75
        return [
76
            PageRepository::DOKTYPE_LINK,
77
            PageRepository::DOKTYPE_SHORTCUT,
78
            PageRepository::DOKTYPE_SPACER,
79
            PageRepository::DOKTYPE_SYSFOLDER,
80
            PageRepository::DOKTYPE_RECYCLER,
81
        ];
82
    }
83
}
84