Total Complexity | 59 |
Total Lines | 182 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ConceptSearchParameters often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConceptSearchParameters, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class ConceptSearchParameters |
||
7 | { |
||
8 | private $config; |
||
9 | private $request; |
||
10 | private $vocabs; |
||
11 | private $rest; |
||
12 | private $hidden; |
||
13 | private $unique; |
||
14 | |||
15 | public function __construct($request, $config, $rest = false) |
||
16 | { |
||
17 | $this->request = $request; |
||
18 | $this->config = $config; |
||
19 | $this->rest = $rest; |
||
20 | $this->hidden = true; |
||
21 | $this->unique = $request->getQueryParamBoolean('unique', false); |
||
22 | } |
||
23 | |||
24 | public function getLang() |
||
25 | { |
||
26 | if ($this->rest && $this->request->getQueryParam('labellang')) { |
||
27 | return $this->request->getQueryParam('labellang'); |
||
28 | } |
||
29 | return $this->request->getLang(); |
||
30 | } |
||
31 | |||
32 | public function getVocabs() |
||
33 | { |
||
34 | if ($this->vocabs) { |
||
35 | return $this->vocabs; |
||
36 | } |
||
37 | if ($this->request->getVocab()) { |
||
38 | return array($this->request->getVocab()); |
||
39 | } |
||
40 | return array(); |
||
41 | } |
||
42 | |||
43 | public function getVocabIds() |
||
44 | { |
||
45 | if ($this->rest || $this->request->getQueryParam('vocabs')) { |
||
46 | $vocabs = $this->rest ? $this->request->getQueryParam('vocab') : $this->request->getQueryParam('vocabs'); |
||
47 | return ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null; |
||
48 | } |
||
49 | $vocabs = $this->getVocabs(); |
||
50 | return isset($vocabs[0]) ? array($vocabs[0]->getId()) : null; |
||
51 | } |
||
52 | |||
53 | public function setVocabularies($vocabs) |
||
54 | { |
||
55 | $this->vocabs = $vocabs; |
||
56 | } |
||
57 | |||
58 | public function getArrayClass() |
||
59 | { |
||
60 | if (sizeof($this->getVocabs()) == 1) { // search within vocabulary |
||
61 | $vocabs = $this->getVocabs(); |
||
62 | return $vocabs[0]->getConfig()->getArrayClassURI(); |
||
63 | } |
||
64 | return null; |
||
65 | } |
||
66 | |||
67 | public function getSearchTerm(): string |
||
68 | { |
||
69 | $term = $this->request->getQueryParamRaw('q') !== null ? $this->request->getQueryParamRaw('q') : $this->request->getQueryParamRaw('query'); |
||
70 | if ((!isset($term) || strlen(trim($term)) === 0) && $this->rest) { |
||
71 | $term = $this->request->getQueryParamRaw('label'); |
||
72 | } |
||
73 | $term = trim(strval($term)); // surrounding whitespace is not considered significant |
||
74 | $term = Normalizer::normalize($term, Normalizer::FORM_C); //Normalize decomposed unicode characters #1184 |
||
75 | if ($this->rest) { |
||
76 | return $term; |
||
77 | } |
||
78 | return strpos($term, "*") === false ? $term . "*" : $term; // default to prefix search |
||
79 | } |
||
80 | |||
81 | public function getContentLang() |
||
84 | } |
||
85 | |||
86 | public function getSearchLang() |
||
87 | { |
||
88 | if ($this->rest) { |
||
89 | return $this->request->getQueryParam('lang'); |
||
90 | } |
||
91 | return $this->request->getQueryParam('anylang') ? '' : $this->getContentLang(); |
||
92 | } |
||
93 | |||
94 | private function getDefaultTypeLimit() |
||
103 | } |
||
104 | |||
105 | public function getTypeLimit() |
||
106 | { |
||
107 | $type = $this->request->getQueryParam('type') !== '' ? $this->request->getQueryParam('type') : null; |
||
108 | if ($type && strpos($type, ' ')) { |
||
109 | $type = explode(' ', $type); |
||
110 | } elseif ($type && strpos($type, '+')) { |
||
111 | $type = explode('+', $type); |
||
112 | } elseif ($type && !is_array($type)) { |
||
113 | // if only one type param given place it into an array regardless |
||
114 | $type = array($type); |
||
115 | } |
||
116 | if ($type === null) { |
||
117 | return $this->getDefaultTypeLimit(); |
||
118 | } |
||
119 | return $type; |
||
120 | } |
||
121 | |||
122 | private function getQueryParam($name) |
||
123 | { |
||
124 | return $this->request->getQueryParam($name) !== '' ? $this->request->getQueryParam($name) : null; |
||
125 | } |
||
126 | |||
127 | private function getQueryParamArray($name) |
||
128 | { |
||
129 | return $this->request->getQueryParam($name) ? explode(' ', urldecode($this->request->getQueryParam($name))) : []; |
||
130 | } |
||
131 | |||
132 | public function getGroupLimit() |
||
133 | { |
||
134 | return $this->getQueryParam('group'); |
||
135 | } |
||
136 | |||
137 | public function getParentLimit() |
||
138 | { |
||
139 | return $this->getQueryParam('parent'); |
||
140 | } |
||
141 | |||
142 | public function getSchemeLimit() |
||
143 | { |
||
144 | return $this->getQueryParamArray('scheme'); |
||
145 | } |
||
146 | |||
147 | public function getOffset() |
||
148 | { |
||
149 | return ($this->request->getQueryParam('offset') && is_numeric($this->request->getQueryParam('offset')) && $this->request->getQueryParam('offset') >= 0) ? $this->request->getQueryParam('offset') : 0; |
||
150 | } |
||
151 | |||
152 | public function getSearchLimit() |
||
153 | { |
||
154 | if ($this->rest) { |
||
155 | return ($this->request->getQueryParam('maxhits')) ? $this->request->getQueryParam('maxhits') : 0; |
||
156 | } |
||
157 | return $this->config->getSearchResultsSize(); |
||
158 | } |
||
159 | |||
160 | public function getUnique() |
||
161 | { |
||
162 | return $this->unique; |
||
163 | } |
||
164 | |||
165 | public function setUnique($unique) |
||
166 | { |
||
167 | $this->unique = $unique; |
||
168 | } |
||
169 | |||
170 | public function getAdditionalFields() |
||
173 | } |
||
174 | |||
175 | public function getHidden() |
||
176 | { |
||
177 | return $this->hidden; |
||
178 | } |
||
179 | |||
180 | public function setHidden($hidden) |
||
181 | { |
||
182 | $this->hidden = $hidden; |
||
183 | } |
||
184 | |||
185 | public function getRest() |
||
188 | } |
||
189 | } |
||
190 |