Passed
Push — master ( 6d83d0...d2efa3 )
by Timo
45:45 queued 16:20
created

PageUidToHierarchy::getSolrRootlineForPageId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1.008
1
<?php
2
namespace ApacheSolrForTypo3\Solr\FieldProcessor;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Michael Knoll <[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 3 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\Domain\Site\Site;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
use TYPO3\CMS\Core\Utility\RootlineUtility;
30
31
/**
32
 * This Processor takes a PID, and resolves its rootline in solr notation.
33
 *
34
 * Format of this field corresponds to http://wiki.apache.org/solr/HierarchicalFaceting
35
 *
36
 * Let's say we have a record indexed on page 111 which is a sub page like shown in this page tree:
37
 *
38
 * 1
39
 * |-10
40
 *   |-100
41
 *     |-111
42
 *
43
 * then we get a rootline 1/10/100/111
44
 *
45
 * In Solr hierarchy notation, we get
46
 *
47
 * 0-1/
48
 * 1-1/10/
49
 * 2-1/10/100/
50
 * 3-1/10/100/11/
51
 *
52
 * which is finally saved in a multi-value field.
53
 *
54
 * @author Michael Knoll <[email protected]>
55
 */
56
class PageUidToHierarchy extends AbstractHierarchyProcessor implements FieldProcessor
57
{
58
59
    /**
60
     * Expects a page ID of a page. Returns a Solr hierarchy notation for the
61
     * rootline of the page ID.
62
     *
63
     * @param array $values Array of values, an array because of multivalued fields
64
     * @return array Modified array of values
65
     */
66 67
    public function process(array $values)
67
    {
68 67
        $results = [];
69
70 67
        foreach ($values as $value) {
71 67
            list($rootPageUid, $mountPoint) = GeneralUtility::trimExplode(',',
72 67
                $value, true, 2);
73 67
            $results[] = $this->getSolrRootlineForPageId($rootPageUid,
0 ignored issues
show
Bug introduced by
$rootPageUid of type string is incompatible with the type integer expected by parameter $pageId of ApacheSolrForTypo3\Solr\...SolrRootlineForPageId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $results[] = $this->getSolrRootlineForPageId(/** @scrutinizer ignore-type */ $rootPageUid,
Loading history...
74
                $mountPoint);
75
        }
76
77 67
        return $results;
78
    }
79
80
    /**
81
     * Returns a Solr hierarchy notation string for rootline of given PID.
82
     *
83
     * @param int $pageId Page ID to get a rootline as Solr hierarchy for
84
     * @param string $mountPoint The mount point parameter that will be used for building the rootline.
85
     * @return array Rootline as Solr hierarchy array
86
     */
87 67
    protected function getSolrRootlineForPageId($pageId, $mountPoint = '')
88
    {
89 67
        $pageIdRootline = $this->buildPageIdRootline($pageId, $mountPoint);
90 67
        $solrRootline = $this->buildSolrHierarchyFromIdRootline($pageIdRootline);
91
92 67
        return $solrRootline;
93
    }
94
95
    /**
96
     * Builds a page's rootline of parent page Ids
97
     *
98
     * @param int $pageId The page Id to build the rootline for
99
     * @param string $mountPoint The mount point parameter that will be passed to getRootline().
100
     * @return array Page Id rootline as array
101
     */
102 67
    protected function buildPageIdRootline($pageId, $mountPoint = '')
103
    {
104 67
        $rootlinePageIds = [];
105
106 67
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, $mountPoint);
107
        try {
108 67
            $rootline = $rootlineUtility->get();
109
        } catch (\RuntimeException $e) {
110
            $rootline = [];
111
        }
112
113 67
        foreach ($rootline as $page) {
114 67
            if (Site::isRootPage($page)) {
115 67
                break;
116
            }
117
118 54
            array_unshift($rootlinePageIds, $page['pid']);
119
        }
120
121 67
        $rootlinePageIds[] = $pageId;
122
123 67
        return $rootlinePageIds;
124
    }
125
}
126