Passed
Pull Request — release-11.2.x (#3594)
by Markus
14:43 queued 10:47
created

PageUidToHierarchy::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.108

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 18
ccs 7
cts 10
cp 0.7
rs 9.9332
cc 2
nc 2
nop 1
crap 2.108
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\FieldProcessor;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2011-2015 Michael Knoll <[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\Site;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Core\Utility\RootlineUtility;
31
32
/**
33
 * This Processor takes a PID, and resolves its rootline in solr notation.
34
 *
35
 * Format of this field corresponds to http://wiki.apache.org/solr/HierarchicalFaceting
36
 *
37
 * Let's say we have a record indexed on page 111 which is a sub page like shown in this page tree:
38
 *
39
 * 1
40
 * |-10
41
 *   |-100
42
 *     |-111
43
 *
44
 * then we get a rootline 1/10/100/111
45
 *
46
 * In Solr hierarchy notation, we get
47
 *
48
 * 0-1/
49
 * 1-1/10/
50
 * 2-1/10/100/
51
 * 3-1/10/100/11/
52
 *
53
 * which is finally saved in a multi-value field.
54
 *
55
 * @author Michael Knoll <[email protected]>
56
 */
57
class PageUidToHierarchy extends AbstractHierarchyProcessor implements FieldProcessor
58
{
59
60
    /**
61
     * Expects a page ID of a page. Returns a Solr hierarchy notation for the
62
     * rootline of the page ID.
63
     *
64
     * @param array $values Array of values, an array because of multivalued fields
65
     * @return array Modified array of values
66 37
     */
67
    public function process(array $values)
68 37
    {
69
        $results = [];
70 37
71 37
        foreach ($values as $value) {
72 37
            list($rootPageUid, $mountPoint) = GeneralUtility::trimExplode(
73 37
                ',',
74 37
                $value,
75
                true,
76
                2
77 37
            );
78
            $results[] = $this->getSolrRootlineForPageId(
79
                $rootPageUid,
80
                $mountPoint
81
            );
82
        }
83
84
        return $results;
85
    }
86
87 37
    /**
88
     * Returns a Solr hierarchy notation string for rootline of given PID.
89 37
     *
90 37
     * @param int $pageId Page ID to get a rootline as Solr hierarchy for
91
     * @param string $mountPoint The mount point parameter that will be used for building the rootline.
92 37
     * @return array Rootline as Solr hierarchy array
93
     */
94
    protected function getSolrRootlineForPageId($pageId, $mountPoint = '')
95
    {
96
        $pageIdRootline = $this->buildPageIdRootline($pageId, $mountPoint);
97
        $solrRootline = $this->buildSolrHierarchyFromIdRootline($pageIdRootline);
98
99
        return $solrRootline;
100
    }
101
102 37
    /**
103
     * Builds a page's rootline of parent page Ids
104 37
     *
105
     * @param int $pageId The page Id to build the rootline for
106 37
     * @param string $mountPoint The mount point parameter that will be passed to getRootline().
107
     * @return array Page Id rootline as array
108 37
     */
109
    protected function buildPageIdRootline($pageId, $mountPoint = '')
110
    {
111
        $rootlinePageIds = [];
112
113 37
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, $mountPoint);
114 37
        try {
115 37
            $rootline = $rootlineUtility->get();
116
        } catch (\RuntimeException $e) {
117
            $rootline = [];
118 21
        }
119
120
        foreach ($rootline as $page) {
121 37
            if (Site::isRootPage($page)) {
122
                break;
123 37
            }
124
125
            array_unshift($rootlinePageIds, $page['pid']);
126
        }
127
128
        $rootlinePageIds[] = $pageId;
129
130
        return $rootlinePageIds;
131
    }
132
}
133