Passed
Push — master ( e6b537...86ebab )
by Timo
25:31 queued 20:58
created

ConfigurationPageResolver   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 59.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 82
ccs 19
cts 32
cp 0.5938
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 4
A getClosestPageIdWithActiveTemplate() 0 17 3
A calculateClosestPageIdWithActiveTemplate() 0 15 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Configuration;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2016 Timo Schmidt <[email protected]
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
28
use ApacheSolrForTypo3\Solr\System\Records\SystemTemplate\SystemTemplateRepository;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Frontend\Page\PageRepository;
31
32
/**
33
 * This class is responsible to find the closest page id from the rootline where
34
 * a typoscript template is stored on.
35
 *
36
 * @package ApacheSolrForTypo3\Solr\System\Configuration
37
 * @author Timo Hund <[email protected]>
38
 */
39
class ConfigurationPageResolver
40
{
41
    /**
42
     * @var PageRepository
43
     */
44
    protected $pageRepository;
45
46
    /**
47
     * @var SystemTemplateRepository
48
     */
49
    protected $systemTemplateRepository;
50
51
    /**
52
     * @var TwoLevelCache
53
     */
54
    protected $twoLevelCache;
55
56
    /**
57
     * @var TwoLevelCache
58
     */
59
    protected $runtimeCache;
60
61
    /**
62
     * ConfigurationPageResolver constructor.
63
     * @param PageRepository|null $pageRepository
64
     * @param TwoLevelCache|null $twoLevelCache
65
     * @param SystemTemplateRepository $systemTemplateRepository
66
     */
67 1
    public function __construct(PageRepository $pageRepository = null, TwoLevelCache $twoLevelCache = null, SystemTemplateRepository $systemTemplateRepository = null)
68
    {
69 1
        $this->pageRepository = isset($pageRepository) ? $pageRepository : GeneralUtility::makeInstance(PageRepository::class);
70 1
        $this->runtimeCache = isset($twoLevelCache) ? $twoLevelCache : GeneralUtility::makeInstance(TwoLevelCache::class, 'cache_runtime');
71 1
        $this->systemTemplateRepository = isset($systemTemplateRepository) ? $systemTemplateRepository : GeneralUtility::makeInstance(SystemTemplateRepository::class);
72 1
    }
73
74
    /**
75
     * This method fetches the rootLine and calculates the id of the closest template in the rootLine.
76
     * The result is stored in the runtime cache.
77
     *
78
     * @param integer $startPageId
79
     * @return integer
80
     */
81 1
    public function getClosestPageIdWithActiveTemplate($startPageId)
82
    {
83 1
        if ($startPageId === 0) {
84
            return 0;
85
        }
86
87 1
        $cacheId = 'ConfigurationPageResolver' . '_' . 'getClosestPageIdWithActiveTemplate' . '_' . $startPageId;
88 1
        $methodResult = $this->runtimeCache->get($cacheId);
89 1
        if (!empty($methodResult)) {
90
            return $methodResult;
91
        }
92
93 1
        $methodResult = $this->calculateClosestPageIdWithActiveTemplate($startPageId);
94 1
        $this->runtimeCache->set($cacheId, $methodResult);
95
96 1
        return $methodResult;
97
    }
98
99
    /**
100
     * This method fetches the rootLine and calculates the id of the closest template in the rootLine.
101
     *
102
     * @param integer $startPageId
103
     * @return int
104
     */
105 1
    protected function calculateClosestPageIdWithActiveTemplate($startPageId)
106
    {
107 1
        $rootLine = $this->pageRepository->getRootLine($startPageId);
108
        // when no rootline is present the startpage it's self is the closest page
109 1
        if (!is_array($rootLine)) {
110
            return $startPageId;
111
        }
112
113 1
        $closestPageIdWithTemplate = $this->systemTemplateRepository->findOneClosestPageIdWithActiveTemplateByRootLine($rootLine);
114 1
        if ($closestPageIdWithTemplate === 0) {
115
            return $startPageId;
116
        }
117
118 1
        return (int)$closestPageIdWithTemplate;
119
    }
120
}
121