SamlEntityRepository::createIdpIfNotExists()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 55
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 42
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 55
rs 9.248

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
/**
4
 * Copyright 2020 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\Behat\Repository;
20
21
use Exception;
22
use PDO;
23
use Ramsey\Uuid\Uuid;
24
25
/**
26
 * A poor mans repository, a pdo connection to the test database is established in the constructor
27
 */
28
class SamlEntityRepository
29
{
30
    const SP_ACS_LOCATION = 'https://gateway.dev.openconext.local/test/authentication/consume-assertion';
31
32
    const SP_ADFS_SSO_LOCATION = 'https://gateway.dev.openconext.local/test/authentication/adfs/sso';
33
34
    /**
35
     * @var Connection
36
     */
37
    private $connection;
38
39
    public function __construct(Connection $connection)
40
    {
41
        $this->connection = $connection;
42
    }
43
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];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$configuration was never initialized. Although not strictly required by PHP, it is generally a good practice to add $configuration = array(); before regardless.
Loading history...
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
        }
109
    }
110
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;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$configuration was never initialized. Although not strictly required by PHP, it is generally a good practice to add $configuration = array(); before regardless.
Loading history...
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
        }
167
    }
168
}
169