| Conditions | 8 |
| Paths | 12 |
| Total Lines | 70 |
| 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 |
||
| 58 | public function syncByAssertion(UserElement $user, Assertion $assertion) |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * Nothing to do, move on |
||
| 62 | */ |
||
| 63 | if (false === Saml::getInstance()->getSettings()->syncGroups) { |
||
| 64 | return true; |
||
| 65 | } |
||
| 66 | |||
| 67 | $groupNames = Saml::getInstance()->getSettings()->groupAttributeNames; |
||
| 68 | $groups = []; |
||
| 69 | /** |
||
| 70 | * Make sure there is an attribute statement |
||
| 71 | */ |
||
| 72 | if (! $assertion->getFirstAttributeStatement()) { |
||
| 73 | Saml::debug( |
||
| 74 | 'No attribute statement found, moving on.' |
||
| 75 | ); |
||
| 76 | return true; |
||
| 77 | } |
||
| 78 | |||
| 79 | foreach ($assertion->getFirstAttributeStatement()->getAllAttributes() as $attribute) { |
||
| 80 | Saml::debug( |
||
| 81 | sprintf( |
||
| 82 | 'Is attribute group? "%s" in %s', |
||
| 83 | $attribute->getName(), |
||
| 84 | json_encode($groupNames) |
||
| 85 | ) |
||
| 86 | ); |
||
| 87 | /** |
||
| 88 | * Is there a group name match? |
||
| 89 | * Match the attribute name to the specified name in the plugin settings |
||
| 90 | */ |
||
| 91 | if (in_array($attribute->getName(), $groupNames)) { |
||
| 92 | /** |
||
| 93 | * Loop thru all of the attributes values because they could have multiple values. |
||
| 94 | * Example XML: |
||
| 95 | * <saml2:Attribute Name="groups" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
||
| 96 | * <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" |
||
| 97 | * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"> |
||
| 98 | * craft_admin |
||
| 99 | * </saml2:AttributeValue> |
||
| 100 | * <saml2:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" |
||
| 101 | * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"> |
||
| 102 | * craft_member |
||
| 103 | * </saml2:AttributeValue> |
||
| 104 | * </saml2:Attribute> |
||
| 105 | */ |
||
| 106 | foreach ($attribute->getAllAttributeValues() as $value) { |
||
| 107 | if ($group = $this->findOrCreate($value)) { |
||
| 108 | Saml::debug( |
||
| 109 | sprintf( |
||
| 110 | 'Assigning group: %s', |
||
| 111 | $group->name |
||
| 112 | ) |
||
| 113 | ); |
||
| 114 | $groups[] = $group->id; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | /** |
||
| 120 | * just return if this is empty |
||
| 121 | */ |
||
| 122 | if (empty($groups)) { |
||
| 123 | return true; |
||
| 124 | } |
||
| 125 | |||
| 126 | return \Craft::$app->getUsers()->assignUserToGroups($user->id, $groups); |
||
| 127 | } |
||
| 128 | } |
||
| 129 |