| Conditions | 12 |
| Paths | 39 |
| Total Lines | 67 |
| Code Lines | 41 |
| 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 | protected function getAzureGroups(): Generator |
||
| 123 | { |
||
| 124 | $groupFilter = $this->plugin->get(AzureActiveDirectory::SETTING_GROUP_FILTER); |
||
| 125 | |||
| 126 | $groupFields = [ |
||
| 127 | 'id', |
||
| 128 | 'displayName', |
||
| 129 | 'description', |
||
| 130 | ]; |
||
| 131 | |||
| 132 | $getUsergroupsDelta = 'true' === $this->plugin->get(AzureActiveDirectory::SETTING_GET_USERGROUPS_DELTA); |
||
| 133 | |||
| 134 | if ($getUsergroupsDelta) { |
||
| 135 | $usergroupsDeltaLink = $this->plugin->getSyncState(AzureSyncState::USERGROUPS_DATALINK); |
||
| 136 | |||
| 137 | $query = $usergroupsDeltaLink |
||
| 138 | ? $usergroupsDeltaLink->getValue() |
||
| 139 | : sprintf('$select=%s', implode(',', $groupFields)); |
||
| 140 | } else { |
||
| 141 | $query = sprintf( |
||
| 142 | '$top=%d&$select=%s', |
||
| 143 | AzureActiveDirectory::API_PAGE_SIZE, |
||
| 144 | implode(',', $groupFields) |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | $token = null; |
||
| 149 | |||
| 150 | do { |
||
| 151 | $this->generateOrRefreshToken($token); |
||
| 152 | |||
| 153 | try { |
||
| 154 | $azureGroupsRequest = $this->provider->request( |
||
| 155 | 'get', |
||
| 156 | $getUsergroupsDelta ? "groups/delta?$query" : "groups?$query", |
||
| 157 | $token |
||
| 158 | ); |
||
| 159 | } catch (Exception $e) { |
||
| 160 | throw new Exception('Exception when requesting groups from Azure: '.$e->getMessage()); |
||
| 161 | } |
||
| 162 | |||
| 163 | $azureGroupsInfo = $azureGroupsRequest['value'] ?? []; |
||
| 164 | |||
| 165 | foreach ($azureGroupsInfo as $azureGroupInfo) { |
||
| 166 | if (!empty($groupFilter) && |
||
| 167 | !preg_match("/$groupFilter/", $azureGroupInfo['displayName']) |
||
| 168 | ) { |
||
| 169 | continue; |
||
| 170 | } |
||
| 171 | |||
| 172 | yield $azureGroupInfo; |
||
| 173 | } |
||
| 174 | |||
| 175 | $hasNextLink = false; |
||
| 176 | |||
| 177 | if (!empty($azureGroupsRequest['@odata.nextLink'])) { |
||
| 178 | $hasNextLink = true; |
||
| 179 | $query = parse_url($azureGroupsRequest['@odata.nextLink'], PHP_URL_QUERY); |
||
| 180 | } |
||
| 181 | |||
| 182 | if ($getUsergroupsDelta && !empty($azureGroupsRequest['@odata.deltaLink'])) { |
||
| 183 | $this->plugin->saveSyncState( |
||
| 184 | AzureSyncState::USERGROUPS_DATALINK, |
||
| 185 | parse_url($azureGroupsRequest['@odata.deltaLink'], PHP_URL_QUERY), |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | } while ($hasNextLink); |
||
| 189 | } |
||
| 240 |