Conditions | 3 |
Paths | 3 |
Total Lines | 55 |
Code Lines | 42 |
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 |
||
111 | public function createIdpIfNotExists($entityId, $certificate) |
||
112 | { |
||
113 | // Does the SP exist? |
||
114 | $stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1'); |
||
115 | $stmt->bindParam('entityId', $entityId, PDO::PARAM_STR); |
||
116 | $stmt->execute(); |
||
117 | if ($stmt->rowCount() === 0) { |
||
118 | // If not, create it |
||
119 | $uuid = Uuid::uuid4()->toString(); |
||
120 | $type = 'idp'; |
||
121 | |||
122 | $configuration['public_key'] = $certificate; |
||
123 | |||
124 | $data = [ |
||
125 | 'entityId' => $entityId, |
||
126 | 'type' => $type, |
||
127 | 'configuration' => json_encode($configuration), |
||
128 | 'id' => $uuid, |
||
129 | ]; |
||
130 | $sql = <<<SQL |
||
131 | INSERT INTO saml_entity ( |
||
132 | `entity_id`, |
||
133 | `type`, |
||
134 | `configuration`, |
||
135 | `id` |
||
136 | ) |
||
137 | VALUES ( |
||
138 | :entityId, |
||
139 | :type, |
||
140 | :configuration, |
||
141 | :id |
||
142 | ) |
||
143 | SQL; |
||
144 | $stmt = $this->connection->prepare($sql); |
||
145 | if ($stmt->execute($data)) { |
||
146 | return $data; |
||
147 | } |
||
148 | |||
149 | throw new Exception( |
||
150 | sprintf( |
||
151 | 'Unable to insert the new SP saml_entity. PDO raised this error: "%s"', |
||
152 | $stmt->errorInfo()[2] |
||
153 | ) |
||
154 | ); |
||
155 | } else { |
||
156 | // Return the SP data |
||
157 | $results = $stmt->fetchAll(); |
||
158 | $result = $results[0]; |
||
159 | $data = [ |
||
160 | 'entityId' => $result['entity_id'], |
||
161 | 'type' => $result['type'], |
||
162 | 'configuration' => $result['configuration'], |
||
163 | 'id' => $result['id'], |
||
164 | ]; |
||
165 | return $data; |
||
166 | } |
||
169 |