Passed
Push — task/2976_TYPO3.11_compatibili... ( 0f123d...0e8702 )
by Rafael
31:18
created

PageUidToHierarchy::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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 31
    public function process(array $values)
67
    {
68 31
        $results = [];
69
70 31
        foreach ($values as $value) {
71 31
            $results[] = $this->getSolrRootlineForPageId(
72 31
                ...GeneralUtility::trimExplode(
0 ignored issues
show
Bug introduced by
TYPO3\CMS\Core\Utility\G...e(',', $value, true, 2) is expanded, but the parameter $pageId of ApacheSolrForTypo3\Solr\...SolrRootlineForPageId() does not expect variable arguments. ( Ignorable by Annotation )

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

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