Conditions | 3 |
Paths | 3 |
Total Lines | 59 |
Code Lines | 47 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
28 | public function createSpIfNotExists($entityId, $certificate, $sfoEnabled = false) |
||
29 | { |
||
30 | // Does the SP exist? |
||
31 | $stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1'); |
||
32 | $stmt->bindParam('entityId', $entityId); |
||
33 | $stmt->execute(); |
||
34 | if ($stmt->rowCount() === 0) { |
||
35 | // If not, create it |
||
36 | $uuid = Uuid::uuid4()->toString(); |
||
37 | $type = 'sp'; |
||
38 | $configuration['acs'] = [self::SP_ACS_LOCATION]; |
||
|
|||
39 | $configuration['public_key'] = $certificate; |
||
40 | $configuration['loa'] = ['__default__' => 'http://stepup.example.com/assurance/loa1']; |
||
41 | $configuration['second_factor_only'] = $sfoEnabled; |
||
42 | $configuration['set_sso_cookie_on_2fa'] = true; |
||
43 | $configuration['allow_sso_on_2fa'] = true; |
||
44 | $configuration['second_factor_only_nameid_patterns'] = [ |
||
45 | 'urn:collab:person:stepup.example.com:admin', |
||
46 | 'urn:collab:person:stepup.example.com:*', |
||
47 | ]; |
||
48 | |||
49 | $data = [ |
||
50 | 'entityId' => $entityId, |
||
51 | 'type' => $type, |
||
52 | 'configuration' => json_encode($configuration), |
||
53 | 'id' => $uuid, |
||
54 | ]; |
||
55 | $sql = <<<SQL |
||
56 | INSERT INTO saml_entity ( |
||
57 | `entity_id`, |
||
58 | `type`, |
||
59 | `configuration`, |
||
60 | `id` |
||
61 | ) |
||
62 | VALUES ( |
||
63 | :entityId, |
||
64 | :type, |
||
65 | :configuration, |
||
66 | :id |
||
67 | ) |
||
68 | SQL; |
||
69 | $stmt = $this->connection->prepare($sql); |
||
70 | if ($stmt->execute($data)) { |
||
71 | return $data; |
||
72 | } |
||
73 | |||
74 | throw new Exception('Unable to insert the new SP saml_entity'); |
||
75 | } else { |
||
76 | // Return the SP data |
||
77 | $results = $stmt->fetchAll(); |
||
78 | $result = $results[0]; |
||
79 | $data = [ |
||
80 | 'entityId' => $result['entity_id'], |
||
81 | 'type' => $result['type'], |
||
82 | 'configuration' => $result['configuration'], |
||
83 | 'id' => $result['id'], |
||
84 | ]; |
||
85 | |||
86 | return $data; |
||
87 | } |
||
143 |