Completed
Push — master ( 3efc81...a16fdb )
by Fabien
03:51
created

CompositeFacet::modifyMatcher()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
namespace Fab\Vidi\Facet;
3
4
/*
5
 * This file is part of the Fab/Vidi project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10
11
use Fab\Vidi\Persistence\Matcher;
12
use Fab\Vidi\Tca\Tca;
13
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Lang\LanguageService;
16
17
/**
18
 * Class CompositeFacet
19
 */
20
class CompositeFacet implements FacetInterface
21
{
22
23
    /**
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * @var string
30
     */
31
    protected $label;
32
33
    /**
34
     * @var array
35
     */
36
    protected $suggestions = [
37
        '0' => 'LLL:EXT:vidi/Resources/Private/Language/locallang.xlf:active.0',
38
    ];
39
40
    /**
41
     * @var array
42
     */
43
    protected $configuration = [];
44
45
    /**
46
     * @var string
47
     */
48
    protected $dataType;
49
50
    /**
51
     * @var bool
52
     */
53
    protected $canModifyMatcher = true;
54
55
    /**
56
     * Constructor of a Generic Facet in Vidi.
57
     *
58
     * @param string $name
59
     * @param string $label
60
     * @param array $suggestions
61
     * @param array $configuration
62
     */
63
    public function __construct($name, $label = '', array $suggestions = [], $configuration = [])
64
    {
65
        $this->name = $name;
66
        if (empty($label)) {
67
            $label = $this->name;
68
        }
69
        $this->label = $label;
70
        if ($suggestions) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $suggestions of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
71
            $this->suggestions = $suggestions;
72
        }
73
        if ($configuration) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $configuration of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
74
            $this->configuration = $configuration;
75
        }
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getName(): string
82
    {
83
        return $this->name;
84
    }
85
86
    /**
87
     * @return string
88
     */
89 View Code Duplication
    public function getLabel(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        if ($this->label === $this->name) {
92
            $label = Tca::table($this->dataType)->field($this->getName())->getLabel();
93
        } else {
94
            try {
95
                $label = LocalizationUtility::translate($this->label, '');
96
            } catch (\InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
97
            }
98
            if (empty($label)) {
99
                $label = $this->label;
100
            }
101
        }
102
103
        return $label;
104
    }
105
106
    /**
107
     * @return array
108
     */
109 View Code Duplication
    public function getSuggestions(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
112
        $values = [];
113
        foreach ($this->suggestions as $key => $label) {
114
            $localizedLabel = $this->getLanguageService()->sL($label);
115
            if (!empty($localizedLabel)) {
116
                $label = $localizedLabel;
117
            }
118
119
            $values[] = [$key => $label];
120
        }
121
122
        return $values;
123
    }
124
125
    /**
126
     * @return LanguageService
127
     */
128 View Code Duplication
    protected function getLanguageService(): LanguageService
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        /** @var LanguageService $langService */
131
        $langService = $GLOBALS['LANG'];
132
        if (!$langService) {
133
            $langService = GeneralUtility::makeInstance(LanguageService::class);
134
            $langService->init('en');
135
        }
136
137
        return $langService;
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function hasSuggestions(): bool
144
    {
145
        return !empty($this->suggestions);
146
    }
147
148
    /**
149
     * @param string $dataType
150
     * @return $this
151
     */
152
    public function setDataType($dataType): self
153
    {
154
        $this->dataType = $dataType;
155
        return $this;
156
    }
157
158
    /**
159
     * @return bool
160
     */
161
    public function canModifyMatcher(): bool
162
    {
163
        return $this->canModifyMatcher;
164
    }
165
166
    /**
167
     * @param Matcher $matcher
168
     * @param $value
169
     * @return Matcher
170
     */
171
    public function modifyMatcher(Matcher $matcher, $value): Matcher
172
    {
173
        foreach ($this->configuration as $fieldNameAndPath => $operand) {
174
            if ($operand === '*') {
175
                $matcher->notEquals($fieldNameAndPath, '');
176
            } else {
177
                $matcher->equals($fieldNameAndPath, $operand);
178
            }
179
        }
180
        return $matcher;
181
    }
182
183
}
184