| Conditions | 13 |
| Paths | 320 |
| Total Lines | 77 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 122 | public function search($query, $rows = 10, $offset = 0, $searchField = 'author') |
||
| 123 | { |
||
| 124 | $requestUri = $this->apiUrl . $this->resource . '?rows='.$rows; |
||
| 125 | |||
| 126 | if ($offset > 0) $requestUri .= '&offset=' . $offset; |
||
| 127 | |||
| 128 | if ($searchField) { |
||
| 129 | $requestUri .= '&query.'.$searchField.'='.urlencode($query); |
||
| 130 | } else { |
||
| 131 | switch (PublicationIdentifier::determineIdentifierType(trim($query))) { |
||
| 132 | case 'DOI': |
||
| 133 | $requestUri .= "&filter=doi:".urlencode(trim($query)); |
||
| 134 | break; |
||
| 135 | case 'ISBN': |
||
| 136 | $isbn = str_replace(['-',' '], '', $query); |
||
| 137 | $requestUri .= "&filter=isbn:".$isbn; |
||
| 138 | break; |
||
| 139 | case 'ISSN': |
||
| 140 | $requestUri .= "&filter=issn:".trim($query); |
||
| 141 | break; |
||
| 142 | default: |
||
| 143 | $requestUri .= "&query=".urlencode($query); |
||
| 144 | break; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | try { |
||
| 149 | $response = Request::get($requestUri)->send(); |
||
| 150 | |||
| 151 | if (!$response->hasErrors() && $response->code == 200) { |
||
| 152 | |||
| 153 | $jsonString = $response->__toString(); |
||
| 154 | if ($jsonString) { |
||
| 155 | |||
| 156 | $data = json_decode($response->__toString(),true); |
||
| 157 | $encoder = new XmlEncoder(); |
||
| 158 | |||
| 159 | if ($data['message']['total-results'] < 1) { |
||
| 160 | return []; |
||
| 161 | } |
||
| 162 | |||
| 163 | foreach ($data['message']['items'] as $item) { |
||
| 164 | |||
| 165 | /** @var CrossRefMetadata $crossRefMetadata */ |
||
| 166 | $crossRefMetadata = $this->objectManager->get(CrossRefMetadata::class); |
||
| 167 | |||
| 168 | $crossRefMetadata->setSource(get_class($this)); |
||
| 169 | $crossRefMetadata->setFeUser($this->security->getUser()->getUid()); |
||
| 170 | $crossRefMetadata->setData($encoder->encode(['message' => $item], 'xml')); |
||
| 171 | $crossRefMetadata->setPublicationIdentifier($item['DOI']); |
||
| 172 | |||
| 173 | $results['items'][$crossRefMetadata->getPublicationIdentifier()] = $crossRefMetadata; |
||
| 174 | } |
||
| 175 | |||
| 176 | $results['total-results'] = $data['message']['total-results']; |
||
| 177 | $results['items-per-page'] = $data['message']['items-per-page']; |
||
| 178 | |||
| 179 | // Because the CrossRef api does not allow an offset more than 10000 we need to limit the result total |
||
| 180 | $maxPages = ceil(10000 / $results['items-per-page']); |
||
|
|
|||
| 181 | $pages = ceil($results['total-results'] / $results['items-per-page']); |
||
| 182 | if ($pages > $maxPages) { |
||
| 183 | $results['total-results'] = $maxPages * $results['items-per-page']; |
||
| 184 | } |
||
| 185 | |||
| 186 | return $results; |
||
| 187 | } |
||
| 188 | |||
| 189 | } else { |
||
| 190 | return []; |
||
| 191 | } |
||
| 192 | |||
| 193 | } catch (\Throwable $throwable) { |
||
| 194 | $this->logger->error($throwable->getMessage()); |
||
| 195 | throw $throwable; |
||
| 196 | } |
||
| 197 | |||
| 198 | return []; |
||
| 199 | } |
||
| 233 |