Conditions | 3 |
Paths | 3 |
Total Lines | 64 |
Code Lines | 50 |
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 |
||
44 | public function createSpIfNotExists($entityId, $certificate, $sfoEnabled = false) |
||
45 | { |
||
46 | // Does the SP exist? |
||
47 | $stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1'); |
||
48 | $stmt->bindParam('entityId', $entityId); |
||
49 | $stmt->execute(); |
||
50 | if ($stmt->rowCount() === 0) { |
||
51 | // If not, create it |
||
52 | $uuid = Uuid::uuid4()->toString(); |
||
53 | $type = 'sp'; |
||
54 | $configuration['acs'] = [self::SP_ACS_LOCATION]; |
||
|
|||
55 | $configuration['public_key'] = $certificate; |
||
56 | $configuration['loa'] = ['__default__' => 'http://dev.openconext.local/assurance/loa1']; |
||
57 | $configuration['second_factor_only'] = $sfoEnabled; |
||
58 | $configuration['set_sso_cookie_on_2fa'] = true; |
||
59 | $configuration['allow_sso_on_2fa'] = true; |
||
60 | $configuration['second_factor_only_nameid_patterns'] = [ |
||
61 | 'urn:collab:person:stepup.example.com:admin', |
||
62 | 'urn:collab:person:dev.openconext.local:*', |
||
63 | ]; |
||
64 | |||
65 | $data = [ |
||
66 | 'entityId' => $entityId, |
||
67 | 'type' => $type, |
||
68 | 'configuration' => json_encode($configuration), |
||
69 | 'id' => $uuid, |
||
70 | ]; |
||
71 | $sql = <<<SQL |
||
72 | INSERT INTO saml_entity ( |
||
73 | `entity_id`, |
||
74 | `type`, |
||
75 | `configuration`, |
||
76 | `id` |
||
77 | ) |
||
78 | VALUES ( |
||
79 | :entityId, |
||
80 | :type, |
||
81 | :configuration, |
||
82 | :id |
||
83 | ) |
||
84 | SQL; |
||
85 | $stmt = $this->connection->prepare($sql); |
||
86 | if ($stmt->execute($data)) { |
||
87 | return $data; |
||
88 | } |
||
89 | |||
90 | throw new Exception( |
||
91 | sprintf( |
||
92 | 'Unable to insert the new SP saml_entity. PDO raised this error: "%s"', |
||
93 | $stmt->errorInfo()[2] |
||
94 | ) |
||
95 | ); |
||
96 | } else { |
||
97 | // Return the SP data |
||
98 | $results = $stmt->fetchAll(); |
||
99 | $result = $results[0]; |
||
100 | $data = [ |
||
101 | 'entityId' => $result['entity_id'], |
||
102 | 'type' => $result['type'], |
||
103 | 'configuration' => $result['configuration'], |
||
104 | 'id' => $result['id'], |
||
105 | ]; |
||
106 | |||
107 | return $data; |
||
108 | } |
||
169 |