Conditions | 6 |
Paths | 3 |
Total Lines | 58 |
Code Lines | 30 |
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 |
||
37 | public function performQuery($siteUrl, $rows, $request): Collection |
||
38 | { |
||
39 | $searchanalyticsResource = $this->getWebmastersService()->searchanalytics; |
||
40 | |||
41 | $maxQueries = 2000; |
||
42 | $currentRequest = 1; |
||
43 | $dataRows = new Collection(); |
||
44 | |||
45 | while ($currentRequest < $maxQueries) { |
||
46 | $startRow = ($currentRequest - 1) * self::CHUNK_SIZE; |
||
47 | |||
48 | $request->setRowLimit(self::CHUNK_SIZE); |
||
49 | $request->setStartRow($startRow); |
||
50 | |||
51 | $backoff = new ExponentialBackoff(10); |
||
52 | $response = $backoff->execute(function () use ($searchanalyticsResource, $siteUrl, $request) { |
||
53 | return $searchanalyticsResource->query($siteUrl, $request, $this->queryOptParams); |
||
54 | }); |
||
55 | |||
56 | // Stop if no more rows returned |
||
57 | if (count($response->getRows()) == 0) { |
||
58 | break; |
||
59 | } |
||
60 | |||
61 | foreach ($response->getRows() as $row) { |
||
62 | /* |
||
63 | * Use a unique hash as key to prevent duplicates caused by the query dimension problem with the google api |
||
64 | * Google give less than 5000 rows back when two or more dimension with the query dimension are choosen, repeated calls give back more rows |
||
65 | * https://productforums.google.com/forum/?hl=en#!topic/webmasters/wF_Rm9CGr4U |
||
66 | */ |
||
67 | |||
68 | $uniqueHash = md5(str_random()); |
||
69 | $item = []; |
||
70 | |||
71 | if (count($row->getKeys())) { |
||
72 | $item = array_combine($request->getDimensions(), $row->getKeys()); |
||
73 | $uniqueHash = $this->getUniqueItemHash($row, $request); |
||
74 | } |
||
75 | |||
76 | $item['clicks'] = $row->getClicks(); |
||
77 | $item['impressions'] = $row->getImpressions(); |
||
78 | $item['ctr'] = $row->getCtr(); |
||
79 | $item['position'] = $row->getPosition(); |
||
80 | $item['searchType'] = $request->getSearchType(); |
||
81 | |||
82 | $dataRows->put($uniqueHash, $item); |
||
83 | } |
||
84 | |||
85 | //Stop if the requested row count are reached |
||
86 | if ($dataRows->count() >= $rows) { |
||
87 | break; |
||
88 | } |
||
89 | |||
90 | $currentRequest++; |
||
91 | } |
||
92 | |||
93 | return $dataRows->take($rows); |
||
94 | } |
||
95 | |||
147 |