Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class User extends Component |
||
| 29 | { |
||
| 30 | use AssertionTrait; |
||
| 31 | |||
| 32 | const EVENT_BEFORE_USER_SAVE = 'eventBeforeUserSave'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var FieldLayout|null |
||
| 36 | */ |
||
| 37 | private $fieldLayout; |
||
| 38 | /** |
||
| 39 | * @var Field[] |
||
| 40 | */ |
||
| 41 | private $fields = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param SamlResponse $response |
||
| 45 | * @return UserElement |
||
| 46 | * @throws InvalidMessage |
||
| 47 | * @throws UserException |
||
| 48 | */ |
||
| 49 | 3 | public function getByResponse( |
|
| 50 | SamlResponse $response, |
||
| 51 | ProviderRecord $serviceProvider, |
||
| 52 | ProviderRecord $identityProvider, |
||
| 53 | Settings $settings |
||
| 54 | ) |
||
| 55 | { |
||
| 56 | |||
| 57 | 3 | $username = null; |
|
| 58 | |||
| 59 | 3 | $nameIdOverride = $settings->nameIdAttributeOverride ?? $identityProvider->nameIdOverride; |
|
|
|
|||
| 60 | |||
| 61 | 3 | if (!is_null($nameIdOverride)) { |
|
| 62 | // use override |
||
| 63 | foreach ($this->getAssertions( |
||
| 64 | $response, |
||
| 65 | $serviceProvider |
||
| 66 | ) as $assertion) { |
||
| 67 | $attributes = $assertion->getAttributes(); |
||
| 68 | if (isset($attributes[$nameIdOverride])) { |
||
| 69 | $attributeValue = $attributes[$nameIdOverride]; |
||
| 70 | $username = $this->getAttributeValue($attributeValue); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | } else { |
||
| 74 | // use nameid |
||
| 75 | 3 | $assertion = $this->getFirstAssertion($response, $serviceProvider); |
|
| 76 | |||
| 77 | 3 | if (! $assertion->getNameId()) { |
|
| 78 | 3 | throw new InvalidMessage('Name ID is missing.'); |
|
| 79 | } |
||
| 80 | 3 | $username = $assertion->getNameId()->getValue(); |
|
| 81 | |||
| 82 | 3 | Saml::debug('NameId: ' . $assertion->getNameId()->getValue()); |
|
| 83 | } |
||
| 84 | |||
| 85 | 3 | return $this->find($username); |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param ProviderIdentityRecord $identity |
||
| 90 | * @return bool |
||
| 91 | * @throws UserException |
||
| 92 | * @throws \Throwable |
||
| 93 | */ |
||
| 94 | public function login(\flipbox\saml\sp\records\ProviderIdentityRecord $identity) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param UserElement $user |
||
| 120 | * @param SamlResponse $response |
||
| 121 | * @param ProviderRecord $idp |
||
| 122 | * @param Settings $settings |
||
| 123 | * @throws UserException |
||
| 124 | * @throws \Throwable |
||
| 125 | * @throws \craft\errors\ElementNotFoundException |
||
| 126 | * @throws \yii\base\Exception |
||
| 127 | */ |
||
| 128 | public function sync( |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param UserElement $user |
||
| 151 | * @return bool |
||
| 152 | * @throws UserException |
||
| 153 | * @throws \Throwable |
||
| 154 | * @throws \craft\errors\ElementNotFoundException |
||
| 155 | * @throws \yii\base\Exception |
||
| 156 | */ |
||
| 157 | protected function save(UserElement $user) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Response Based Methods |
||
| 181 | */ |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param UserElement $user |
||
| 185 | * @param SamlResponse $response |
||
| 186 | * @param ProviderRecord $idp |
||
| 187 | * @param Settings $settings |
||
| 188 | * @throws UserException |
||
| 189 | * @throws \Throwable |
||
| 190 | */ |
||
| 191 | 3 | protected function construct( |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param UserElement $user |
||
| 234 | * @param SamlResponse $response |
||
| 235 | * @return UserElement |
||
| 236 | */ |
||
| 237 | 3 | protected function transform( |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param UserElement $user |
||
| 280 | * @param $attributeName |
||
| 281 | * @param $attributeValue |
||
| 282 | * @param $craftProperty |
||
| 283 | */ |
||
| 284 | 3 | protected function assignProperty( |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param UserElement $user |
||
| 323 | * @return Field|null |
||
| 324 | */ |
||
| 325 | 3 | protected function getFieldLayoutField(UserElement $user, $fieldHandle) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param UserElement $user |
||
| 344 | * @param string $name |
||
| 345 | * @param mixed $value |
||
| 346 | */ |
||
| 347 | 3 | private function setSimpleProperty(UserElement $user, $name, $value) |
|
| 366 | |||
| 367 | /************************************************** |
||
| 368 | * Craft User Methods |
||
| 369 | **************************************************/ |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param $username |
||
| 373 | * @return UserElement |
||
| 374 | * @throws UserException |
||
| 375 | */ |
||
| 376 | 3 | protected function find($username) |
|
| 377 | { |
||
| 378 | 3 | return $this->forceGet($username); |
|
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param $username |
||
| 383 | * @return UserElement |
||
| 384 | * @throws UserException |
||
| 385 | */ |
||
| 386 | 3 | protected function forceGet($username) |
|
| 387 | { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Is there a user that exists already? |
||
| 391 | */ |
||
| 392 | 3 | if (!($user = $this->getByUsernameOrEmail($username))) { |
|
| 393 | // Should we create a new user? what's the setting say? |
||
| 394 | 3 | if (! Saml::getInstance()->getSettings()->createUser) { |
|
| 395 | throw new UserException("System doesn't have permission to create a new user."); |
||
| 396 | } |
||
| 397 | |||
| 398 | // new user! |
||
| 399 | 3 | $user = new UserElement( |
|
| 400 | [ |
||
| 401 | 3 | 'username' => $username, |
|
| 402 | ] |
||
| 403 | ); |
||
| 404 | } |
||
| 405 | |||
| 406 | 3 | return $user; |
|
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param $usernameOrEmail |
||
| 411 | * @param bool $archived |
||
| 412 | * @return array|bool|\craft\base\ElementInterface|UserElement|null |
||
| 413 | */ |
||
| 414 | 3 | protected function getByUsernameOrEmail($usernameOrEmail, $archived = false) |
|
| 415 | { |
||
| 416 | |||
| 417 | 3 | return UserElement::find() |
|
| 418 | 3 | ->where( |
|
| 419 | [ |
||
| 420 | 3 | 'or', |
|
| 421 | 3 | ['username' => $usernameOrEmail], |
|
| 422 | 3 | ['email' => $usernameOrEmail], |
|
| 423 | ] |
||
| 424 | ) |
||
| 425 | 3 | ->status(null) |
|
| 426 | 3 | ->archived($archived) |
|
| 427 | 3 | ->one(); |
|
| 428 | } |
||
| 429 | |||
| 430 | private function getAttributeValue($attributeValue) |
||
| 439 | } |
||
| 440 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.