Passed
Push — master ( 16f071...683c38 )
by Timo
22:25
created

RequirementsService::getNegationWhenConfigured()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 3
nc 2
nop 2
crap 20
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
19
/**
20
 * Service class to check for a facet if allRequirements are met for that facet.
21
 *
22
 * @author Timo Hund <[email protected]>
23
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facet
24
 */
25
class RequirementsService
26
{
27
    /**
28
     * Checks if facet meets all requirements.
29
     *
30
     * Evaluates configuration in "plugin.tx_solr.search.faceting.facets.[facetName].requirements",
31
     *
32
     * @param AbstractFacet $facet
33
     * @return bool true if facet might be rendered
34
     */
35 64
    public function getAllRequirementsMet(AbstractFacet $facet)
36
    {
37 64
        $requirements = $facet->getRequirements();
38 64
        if (!is_array($requirements) || count($requirements) === 0) {
0 ignored issues
show
introduced by
The condition is_array($requirements) is always true.
Loading history...
39 64
            return true;
40
        }
41
42
        foreach ($requirements as $requirement) {
43
            $requirementMet = $this->getRequirementMet($facet, $requirement);
44
            $requirementMet = $this->getNegationWhenConfigured($requirementMet, $requirement);
45
46
            if ($requirementMet) {
47
                // early return
48
                return true;
49
            }
50
        }
51
52
        return false;
53
    }
54
55
    /**
56
     * Checks if a single requirement is met.
57
     *
58
     * @param AbstractFacet $facet
59
     * @param array $requirement
60
     * @return bool
61
     */
62
    protected function getRequirementMet(AbstractFacet $facet, $requirement = []) {
63
        $selectedItemValues = $this->getSelectedItemValues($facet, $requirement['facet']);
64
        $csvActiveFacetItemValues = implode(', ', $selectedItemValues);
65
        $requirementValues = GeneralUtility::trimExplode(',', $requirement['values']);
66
67
        foreach ($requirementValues as $value) {
68
            $noFacetOptionSelectedRequirementMet = ($value === '__none' && empty($selectedItemValues));
69
            $anyFacetOptionSelectedRequirementMet = ($value === '__any' && !empty($selectedItemValues));
70
71
            if ($noFacetOptionSelectedRequirementMet || $anyFacetOptionSelectedRequirementMet || in_array($value, $selectedItemValues) || fnmatch($value, $csvActiveFacetItemValues)) {
72
                // when we find a single matching requirement we can exit and return true
73
                return true;
74
            }
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * Returns the active item values of a facet
82
     *
83
     * @param string $facetNameToCheckRequirementsOn
84
     * @return AbstractFacetItem[]
85
     */
86
    protected function getSelectedItemValues(AbstractFacet $facet, $facetNameToCheckRequirementsOn)
87
    {
88
        $facetToCheckRequirements = $facet->getResultSet()->getFacets()->getByName($facetNameToCheckRequirementsOn)->getByPosition(0);
89
        if (!$facetToCheckRequirements instanceof AbstractFacet) {
0 ignored issues
show
introduced by
$facetToCheckRequirements is always a sub-type of ApacheSolrForTypo3\Solr\...et\Facets\AbstractFacet.
Loading history...
90
            throw new \InvalidArgumentException('Requirement for unexisting facet configured');
91
        }
92
93
        if (!$facetToCheckRequirements->getIsUsed()) {
94
            // unused facets do not have active values.
95
            return [];
96
        }
97
98
        $itemValues = [];
99
        $activeFacetItems = $facetToCheckRequirements->getAllFacetItems();
100
        foreach ($activeFacetItems as $item) {
101
            /** @var AbstractFacetItem $item */
102
            if ($item->getSelected()) {
103
                $itemValues[] = $item->getUriValue();
104
            }
105
        }
106
107
        return $itemValues;
108
    }
109
110
    /**
111
     * Negates the result when configured.
112
     *
113
     * @param boolean $value
114
     * @param array $configuration
115
     * @return boolean
116
     */
117
    protected function getNegationWhenConfigured($value, $configuration)
118
    {
119
        if (!is_array($configuration) || empty($configuration['negate']) || (bool)$configuration['negate'] === false) {
0 ignored issues
show
introduced by
The condition is_array($configuration) is always true.
Loading history...
120
            return $value;
121
        }
122
123
        return !((bool)$value);
124
    }
125
}