Passed
Pull Request — main (#3339)
by Rafael
06:51 queued 02:48
created

ConfigurationPageResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
wmc 7
eloc 23
c 0
b 0
f 0
dl 0
loc 72
ccs 18
cts 23
cp 0.7826
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A calculateClosestPageIdWithActiveTemplate() 0 16 3
A getClosestPageIdWithActiveTemplate() 0 16 3
A __construct() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\System\Configuration;
17
18
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
19
use ApacheSolrForTypo3\Solr\System\Records\SystemTemplate\SystemTemplateRepository;
20
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
21
use RuntimeException;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Core\Utility\RootlineUtility;
24
25
/**
26
 * This class is responsible to find the closest page id from the rootline where
27
 * a typoscript template is stored on.
28
 *
29
 * @author Timo Hund <[email protected]>
30
 */
31
class ConfigurationPageResolver
32
{
33
    /**
34
     * @var SystemTemplateRepository
35
     */
36
    protected $systemTemplateRepository;
37
38
    /**
39
     * @var TwoLevelCache
40
     */
41
    protected $runtimeCache;
42
43
    /**
44
     * ConfigurationPageResolver constructor.
45
     * @param TwoLevelCache|null $twoLevelCache
46
     * @param SystemTemplateRepository|null $systemTemplateRepository
47
     */
48 4
    public function __construct(?TwoLevelCache $twoLevelCache = null, ?SystemTemplateRepository $systemTemplateRepository = null)
49
    {
50 4
        $this->runtimeCache = $twoLevelCache ?? GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'runtime');
51 4
        $this->systemTemplateRepository = $systemTemplateRepository ?? GeneralUtility::makeInstance(SystemTemplateRepository::class);
52
    }
53
54
    /**
55
     * This method fetches the rootLine and calculates the id of the closest template in the rootLine.
56
     * The result is stored in the runtime cache.
57
     *
58
     * @param int $startPageId
59
     * @return int
60
     * @throws DBALDriverException
61
     */
62 4
    public function getClosestPageIdWithActiveTemplate(int $startPageId): ?int
63
    {
64 4
        if ($startPageId === 0) {
65
            return null;
66
        }
67
68 4
        $cacheId = 'ConfigurationPageResolver' . '_' . 'getClosestPageIdWithActiveTemplate' . '_' . $startPageId;
69 4
        $methodResult = $this->runtimeCache->get($cacheId);
70 4
        if (!empty($methodResult)) {
71 1
            return $methodResult;
72
        }
73
74 4
        $methodResult = $this->calculateClosestPageIdWithActiveTemplate($startPageId);
75 4
        $this->runtimeCache->set($cacheId, $methodResult);
76
77 4
        return $methodResult;
78
    }
79
80
    /**
81
     * This method fetches the rootLine and calculates the id of the closest template in the rootLine.
82
     *
83
     * @param int $startPageId
84
     * @return int
85
     * @throws DBALDriverException
86
     */
87 4
    protected function calculateClosestPageIdWithActiveTemplate(int $startPageId): ?int
88
    {
89
        /* @var RootlineUtility $rootlineUtility */
90 4
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $startPageId);
91
        try {
92 4
            $rootline = $rootlineUtility->get();
93
        } catch (RuntimeException $e) {
94
            return $startPageId;
95
        }
96
97 4
        $closestPageIdWithTemplate = $this->systemTemplateRepository->findOneClosestPageIdWithActiveTemplateByRootLine($rootline);
98 4
        if ($closestPageIdWithTemplate === 0) {
99
            return null;
100
        }
101
102 4
        return $closestPageIdWithTemplate;
103
    }
104
}
105