RequirementsService::getRequirementMet()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 8.4444
cc 8
nc 9
nop 2
crap 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
    public function getAllRequirementsMet(AbstractFacet $facet)
35 65
    {
36
        $requirements = $facet->getRequirements();
37 65
        if (!is_array($requirements) || count($requirements) === 0) {
0 ignored issues
show
introduced by
The condition is_array($requirements) is always true.
Loading history...
38 65
            return true;
39 59
        }
40
41
        foreach ($requirements as $requirement) {
42 8
            $requirementMet = $this->getRequirementMet($facet, $requirement);
43 8
            $requirementMet = $this->getNegationWhenConfigured($requirementMet, $requirement);
44 7
45
            if (!$requirementMet) {
46 7
                // early return as soon as one requirement is not met
47
                return false;
48 7
            }
49
        }
50
51
        return true;
52 4
    }
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 8
        $selectedItemValues = $this->getSelectedItemValues($facet, $requirement['facet']);
63 8
        $csvActiveFacetItemValues = implode(', ', $selectedItemValues);
64 7
        $requirementValues = GeneralUtility::trimExplode(',', $requirement['values']);
65 7
66
        foreach ($requirementValues as $value) {
67 7
            $noFacetOptionSelectedRequirementMet = ($value === '__none' && empty($selectedItemValues));
68 7
            $anyFacetOptionSelectedRequirementMet = ($value === '__any' && !empty($selectedItemValues));
69 7
70
            if ($noFacetOptionSelectedRequirementMet || $anyFacetOptionSelectedRequirementMet || in_array($value, $selectedItemValues) || fnmatch($value, $csvActiveFacetItemValues)) {
71 7
                // when we find a single matching requirement we can exit and return true
72
                return true;
73 7
            }
74
        }
75
76
        return false;
77 5
    }
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 8
    {
87
        $facetToCheckRequirements = $facet->getResultSet()->getFacets()->getByName($facetNameToCheckRequirementsOn)->getByPosition(0);
88 8
        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 8
            throw new \InvalidArgumentException('Requirement for unexisting facet configured');
90 1
        }
91
92
        if (!$facetToCheckRequirements->getIsUsed()) {
93 7
            // unused facets do not have active values.
94
            return [];
95 2
        }
96
97
        $itemValues = [];
98 5
        $activeFacetItems = $facetToCheckRequirements->getAllFacetItems();
99 5
        foreach ($activeFacetItems as $item) {
100 5
            /** @var AbstractFacetItem $item */
101
            if ($item->getSelected()) {
102 5
                $itemValues[] = $item->getUriValue();
103 5
            }
104
        }
105
106
        return $itemValues;
107 5
    }
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 7
    {
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 7
            return $value;
120 6
        }
121
122
        return !((bool)$value);
123 1
    }
124
}
125