| Conditions | 10 |
| Paths | 27 |
| Total Lines | 69 |
| Code 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 |
||
| 46 | protected function getAzureUsers(): Generator |
||
| 47 | { |
||
| 48 | $userFields = [ |
||
| 49 | 'givenName', |
||
| 50 | 'surname', |
||
| 51 | 'mail', |
||
| 52 | 'userPrincipalName', |
||
| 53 | 'businessPhones', |
||
| 54 | 'mobilePhone', |
||
| 55 | 'accountEnabled', |
||
| 56 | 'mailNickname', |
||
| 57 | 'id', |
||
| 58 | ]; |
||
| 59 | |||
| 60 | $getUsersDelta = 'true' === $this->plugin->get(AzureActiveDirectory::SETTING_GET_USERS_DELTA); |
||
| 61 | |||
| 62 | if ($getUsersDelta) { |
||
| 63 | $usersDeltaLink = $this->plugin->getSyncState(AzureSyncState::USERS_DATALINK); |
||
| 64 | |||
| 65 | $query = $usersDeltaLink |
||
| 66 | ? $usersDeltaLink->getValue() |
||
| 67 | : sprintf('$select=%s', implode(',', $userFields)); |
||
| 68 | } else { |
||
| 69 | $query = sprintf( |
||
| 70 | '$top=%d&$select=%s', |
||
| 71 | AzureActiveDirectory::API_PAGE_SIZE, |
||
| 72 | implode(',', $userFields) |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | |||
| 76 | $token = null; |
||
| 77 | |||
| 78 | do { |
||
| 79 | $this->generateOrRefreshToken($token); |
||
| 80 | |||
| 81 | try { |
||
| 82 | $azureUsersRequest = $this->provider->request( |
||
| 83 | 'get', |
||
| 84 | $getUsersDelta ? "users/delta?$query" : "users?$query", |
||
| 85 | $token |
||
| 86 | ); |
||
| 87 | } catch (Exception $e) { |
||
| 88 | throw new Exception('Exception when requesting users from Azure: '.$e->getMessage()); |
||
| 89 | } |
||
| 90 | |||
| 91 | $azureUsersInfo = $azureUsersRequest['value'] ?? []; |
||
| 92 | |||
| 93 | foreach ($azureUsersInfo as $azureUserInfo) { |
||
| 94 | $azureUserInfo['mail'] = $azureUserInfo['mail'] ?? null; |
||
| 95 | $azureUserInfo['surname'] = $azureUserInfo['surname'] ?? null; |
||
| 96 | $azureUserInfo['givenName'] = $azureUserInfo['givenName'] ?? null; |
||
| 97 | |||
| 98 | yield $azureUserInfo; |
||
| 99 | } |
||
| 100 | |||
| 101 | $hasNextLink = false; |
||
| 102 | |||
| 103 | if (!empty($azureUsersRequest['@odata.nextLink'])) { |
||
| 104 | $hasNextLink = true; |
||
| 105 | $query = parse_url($azureUsersRequest['@odata.nextLink'], PHP_URL_QUERY); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($getUsersDelta && !empty($azureUsersRequest['@odata.deltaLink'])) { |
||
| 109 | $this->plugin->saveSyncState( |
||
| 110 | AzureSyncState::USERS_DATALINK, |
||
| 111 | parse_url($azureUsersRequest['@odata.deltaLink'], PHP_URL_QUERY), |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | } while ($hasNextLink); |
||
| 115 | } |
||
| 232 |