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

RequirementsService   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 11.11%

Importance

Changes 0
Metric Value
wmc 22
eloc 33
dl 0
loc 99
ccs 4
cts 36
cp 0.1111
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelectedItemValues() 0 22 5
A getNegationWhenConfigured() 0 7 4
A getAllRequirementsMet() 0 18 5
B getRequirementMet() 0 16 8
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
 */
24
class RequirementsService
25
{
26
    /**
27
     * Checks if facet meets all requirements.
28
     *
29
     * Evaluates configuration in "plugin.tx_solr.search.faceting.facets.[facetName].requirements",
30
     *
31
     * @param AbstractFacet $facet
32
     * @return bool true if facet might be rendered
33
     */
34 35
    public function getAllRequirementsMet(AbstractFacet $facet)
35
    {
36 35
        $requirements = $facet->getRequirements();
37 35
        if (!is_array($requirements) || count($requirements) === 0) {
0 ignored issues
show
introduced by
The condition is_array($requirements) is always true.
Loading history...
38 35
            return true;
39
        }
40
41
        foreach ($requirements as $requirement) {
42
            $requirementMet = $this->getRequirementMet($facet, $requirement);
43
            $requirementMet = $this->getNegationWhenConfigured($requirementMet, $requirement);
44
45
            if (!$requirementMet) {
46
                // early return as soon as one requirement is not met
47
                return false;
48
            }
49
        }
50
51
        return true;
52
    }
53
54
    /**
55
     * Checks if a single requirement is met.
56
     *
57
     * @param AbstractFacet $facet
58
     * @param array $requirement
59
     * @return bool
60
     */
61
    protected function getRequirementMet(AbstractFacet $facet, $requirement = []) {
62
        $selectedItemValues = $this->getSelectedItemValues($facet, $requirement['facet']);
63
        $csvActiveFacetItemValues = implode(', ', $selectedItemValues);
64
        $requirementValues = GeneralUtility::trimExplode(',', $requirement['values']);
65
66
        foreach ($requirementValues as $value) {
67
            $noFacetOptionSelectedRequirementMet = ($value === '__none' && empty($selectedItemValues));
68
            $anyFacetOptionSelectedRequirementMet = ($value === '__any' && !empty($selectedItemValues));
69
70
            if ($noFacetOptionSelectedRequirementMet || $anyFacetOptionSelectedRequirementMet || in_array($value, $selectedItemValues) || fnmatch($value, $csvActiveFacetItemValues)) {
71
                // when we find a single matching requirement we can exit and return true
72
                return true;
73
            }
74
        }
75
76
        return false;
77
    }
78
79
    /**
80
     * Returns the active item values of a facet
81
     *
82
     * @param string $facetNameToCheckRequirementsOn
83
     * @return AbstractFacetItem[]
84
     */
85
    protected function getSelectedItemValues(AbstractFacet $facet, $facetNameToCheckRequirementsOn)
86
    {
87
        $facetToCheckRequirements = $facet->getResultSet()->getFacets()->getByName($facetNameToCheckRequirementsOn)->getByPosition(0);
88
        if (!$facetToCheckRequirements instanceof AbstractFacet) {
0 ignored issues
show
introduced by
$facetToCheckRequirements is always a sub-type of ApacheSolrForTypo3\Solr\...et\Facets\AbstractFacet.
Loading history...
89
            throw new \InvalidArgumentException('Requirement for unexisting facet configured');
90
        }
91
92
        if (!$facetToCheckRequirements->getIsUsed()) {
93
            // unused facets do not have active values.
94
            return [];
95
        }
96
97
        $itemValues = [];
98
        $activeFacetItems = $facetToCheckRequirements->getAllFacetItems();
99
        foreach ($activeFacetItems as $item) {
100
            /** @var AbstractFacetItem $item */
101
            if ($item->getSelected()) {
102
                $itemValues[] = $item->getUriValue();
103
            }
104
        }
105
106
        return $itemValues;
107
    }
108
109
    /**
110
     * Negates the result when configured.
111
     *
112
     * @param boolean $value
113
     * @param array $configuration
114
     * @return boolean
115
     */
116
    protected function getNegationWhenConfigured($value, $configuration)
117
    {
118
        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...
119
            return $value;
120
        }
121
122
        return !((bool)$value);
123
    }
124
}
125