| Conditions | 11 |
| Paths | 20 |
| Total Lines | 91 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 112 | public function fromNetwork() |
||
| 113 | { |
||
| 114 | $dest_path = 'harvests/' . $this->name . '/'; |
||
| 115 | |||
| 116 | Storage::disk('local')->deleteDir($dest_path); |
||
| 117 | |||
| 118 | $latest = $dest_path . 'latest.xml'; |
||
| 119 | |||
| 120 | $client = new OaiPmhClient($this->url, [ |
||
| 121 | 'schema' => $this->schema, |
||
| 122 | 'user-agent' => 'Colligator/0.1', |
||
| 123 | 'max-retries' => $this->maxRetries, |
||
| 124 | 'sleep-time-on-error' => $this->sleepTimeOnError, |
||
| 125 | ]); |
||
| 126 | |||
| 127 | $client->on('request.error', function ($msg) { |
||
| 128 | $this->error($msg); |
||
| 129 | }); |
||
| 130 | |||
| 131 | // Store each response to disk just in case |
||
| 132 | $client->on('request.complete', function ($verb, $args, $body) use ($latest) { |
||
| 133 | Storage::disk('local')->put($latest, $body); |
||
| 134 | }); |
||
| 135 | |||
| 136 | $recordsHarvested = 0; |
||
| 137 | |||
| 138 | // Loop over all records using an iterator that pulls in more data when |
||
| 139 | // the buffer is exhausted. |
||
| 140 | $records = $client->records($this->start, $this->until, $this->set, $this->resume); |
||
| 141 | while (true) { |
||
| 142 | |||
| 143 | // If no records included in the last response |
||
| 144 | if (!$records->valid()) { |
||
| 145 | break 1; |
||
| 146 | } |
||
| 147 | |||
| 148 | $record = $records->current(); |
||
| 149 | ++$recordsHarvested; |
||
| 150 | |||
| 151 | // In case of a crash, it can be useful to have the resumption_token, |
||
| 152 | // but delete it when the harvest is complete |
||
| 153 | if ($this->resume != $records->getResumptionToken()) { |
||
| 154 | $this->resume = $records->getResumptionToken(); |
||
| 155 | if (is_null($this->resume)) { |
||
| 156 | Storage::disk('local')->delete($dest_path . '/resumption_token'); |
||
| 157 | } else { |
||
| 158 | Storage::disk('local')->put($dest_path . '/resumption_token', $this->resume); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | // Note that Bibsys doesn't start counting on 0, as given in the spec, |
||
| 163 | // but it doesn't really matter since we're only interested in a |
||
| 164 | // fixed order. |
||
| 165 | $currentIndex = $records->key(); |
||
| 166 | |||
| 167 | // Move to stable location |
||
| 168 | $destPath = sprintf('%s/response_%08d.xml', $dest_path, $currentIndex); |
||
| 169 | if (Storage::disk('local')->exists($latest)) { |
||
| 170 | Storage::disk('local')->move($latest, $destPath); |
||
| 171 | } |
||
| 172 | |||
| 173 | $this->dispatch(new ImportRecord($this->collection, $record->data->asXML())); |
||
| 174 | |||
| 175 | if ($recordsHarvested % $this->statusUpdateEvery == 0) { |
||
| 176 | if (is_null($this->start)) { |
||
| 177 | $this->status($recordsHarvested, $recordsHarvested); |
||
| 178 | } else { |
||
| 179 | $this->status($recordsHarvested, $currentIndex); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | |||
| 183 | $attempt = 1; |
||
| 184 | while (true) { |
||
| 185 | try { |
||
| 186 | $records->next(); |
||
| 187 | break 1; |
||
| 188 | } catch (BadRequestError $e) { |
||
| 189 | $this->error($e->getMessage()); |
||
| 190 | $this->error($e->getCode()); |
||
| 191 | $this->error('Bad request. Attempt ' . $attempt . ' of 100. Sleeping 10 secs.'); |
||
| 192 | if ($attempt > 100) { |
||
| 193 | throw $e; |
||
| 194 | } |
||
| 195 | ++$attempt; |
||
| 196 | sleep(10); |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | return $recordsHarvested; |
||
| 202 | } |
||
| 203 | |||
| 260 |