Passed
Branch master (5f6eff)
by Rafael
04:03
created

addPageToMountingSiteIndexQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.0019

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
ccs 7
cts 8
cp 0.875
rs 10
cc 1
nc 1
nop 2
crap 1.0019
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2015-2016 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
29
use ApacheSolrForTypo3\Solr\IndexQueue\Initializer\Page;
30
use ApacheSolrForTypo3\Solr\System\Page\Rootline;
31
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
32
use RuntimeException;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Core\Utility\RootlineUtility;
35
36
/**
37
 * Extracted logic from the RecordMonitor to trigger mount page updates.
38
 *
39
 * @author Timo Hund <[email protected]>
40
 */
41
class MountPagesUpdater
42
{
43
44
    /**
45
     * @var PagesRepository
46
     */
47
    protected $pagesRepository;
48
49
    /**
50
     * MountPagesUpdater constructor.
51
     *
52
     * @param PagesRepository|null $pagesRepository
53
     */
54 47
    public function __construct(PagesRepository $pagesRepository = null) {
55 47
        $this->pagesRepository = $pagesRepository ?? GeneralUtility::makeInstance(PagesRepository::class);
56 47
    }
57
58
    /**
59
     * Handles updates of the Index Queue in case a newly created or changed
60
     * page is part of a tree that is mounted into a another site.
61
     *
62
     * @param int $pageId Page Id (uid).
63
     */
64 31
    public function update($pageId)
65
    {
66
        // get the root line of the page, every parent page could be a Mount Page source
67 31
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId);
68
        try {
69 31
            $rootLineArray = $rootlineUtility->get();
70
        } catch (RuntimeException $e) {
71
            $rootLineArray = [];
72
        }
73
74 31
        $currentPage = array_shift($rootLineArray);
75 31
        $currentPageUid = (int)$currentPage['uid'];
76
77 31
        if (empty($rootLineArray) && $currentPageUid === 0) {
78 1
            return;
79
        }
80
81
        /** @var $rootLine Rootline */
82 30
        $rootLine = GeneralUtility::makeInstance(Rootline::class, /** @scrutinizer ignore-type */ $rootLineArray);
83 30
        $rootLineParentPageIds = array_map('intval', $rootLine->getParentPageIds());
84 30
        $destinationMountProperties = $this->pagesRepository->findMountPointPropertiesByPageIdOrByRootLineParentPageIds($currentPageUid, $rootLineParentPageIds);
85
86 30
        if (empty($destinationMountProperties)) {
87 29
            return;
88
        }
89
90 1
        foreach ($destinationMountProperties as $destinationMount) {
91 1
            $this->addPageToMountingSiteIndexQueue($pageId, $destinationMount);
92
        }
93 1
    }
94
95
    /**
96
     * Adds a page to the Index Queue of a site mounting the page.
97
     *
98
     * @param int $mountedPageId ID (uid) of the mounted page.
99
     * @param array $mountProperties Array of mount point properties mountPageSource, mountPageDestination, and mountPageOverlayed
100
     */
101 1
    protected function addPageToMountingSiteIndexQueue($mountedPageId, array $mountProperties)
102
    {
103 1
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
104 1
        $mountingSite = $siteRepository->getSiteByPageId($mountProperties['mountPageDestination']);
105
106
        /** @var $pageInitializer Page */
107 1
        $pageInitializer = GeneralUtility::makeInstance(Page::class);
108 1
        $pageInitializer->setSite($mountingSite);
109 1
        $pageInitializer->setIndexingConfigurationName('pages');
110
111 1
        $pageInitializer->initializeMountedPage($mountProperties, $mountedPageId);
112 1
    }
113
}
114