| Conditions | 10 |
| Paths | 13 |
| Total Lines | 46 |
| 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 |
||
| 104 | protected function fetchBatch($attempt = 1, $chunkSize = null) |
||
| 105 | { |
||
| 106 | if ($this->isFinished) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | $results = $this->client->getXML($this->url('', [ |
||
| 111 | 'path' => $this->resumptionToken ? null : $this->path, |
||
| 112 | 'limit' => $chunkSize ?: $this->chunkSize, |
||
| 113 | 'token' => $this->resumptionToken, |
||
| 114 | 'filter' => $this->filter ? str_replace(['\''], ['''], $this->filter) : null, |
||
| 115 | ])); |
||
| 116 | |||
| 117 | $results->registerXPathNamespaces([ |
||
| 118 | 'rowset' => 'urn:schemas-microsoft-com:xml-analysis:rowset', |
||
| 119 | 'xsd' => 'http://www.w3.org/2001/XMLSchema', |
||
| 120 | 'saw-sql' => 'urn:saw-sql', |
||
| 121 | ]); |
||
| 122 | |||
| 123 | $this->readColumnHeaders($results); |
||
| 124 | |||
| 125 | $rows = $results->all('//rowset:Row'); |
||
| 126 | |||
| 127 | foreach ($rows as $row) { |
||
| 128 | $this->resources[] = $this->convertToResource($row); |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->resumptionToken = $results->text('/report/QueryResult/ResumptionToken') ?: $this->resumptionToken; |
||
| 132 | $this->isFinished = ($results->text('/report/QueryResult/IsFinished') === 'true'); |
||
| 133 | |||
| 134 | if (!count($rows) && !$this->isFinished) { |
||
| 135 | // If the Analytics server spends too long time preparing the results, it can |
||
| 136 | // sometimes return an empty result set. If this happens, we should just wait |
||
| 137 | // a little and retry the request. |
||
| 138 | // See: https://bitbucket.org/uwlib/uwlib-alma-analytic-tools/wiki/Understanding_Analytic_GET_Requests#!analytic-still-loading |
||
| 139 | if ($attempt >= self::$maxAttempts) { |
||
| 140 | // Give up |
||
| 141 | throw new RequestFailed( |
||
| 142 | 'Not getting any data from the Analytics server - max number of retries exhausted.' |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | // Sleep for a few seconds, then retry |
||
| 146 | sleep(self::$retryDelayTime); |
||
| 147 | $this->fetchBatch($attempt + 1); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 234 |