SamlEntityRepository::createSpIfNotExists()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 64
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 50
c 2
b 0
f 0
nc 3
nop 3
dl 0
loc 64
rs 9.0909

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
use Surfnet\StepupGateway\Behat\Factory\SmoketestPdoFactory;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupGateway\Be...ory\SmoketestPdoFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
/**
27
 * A poor mans repository, a pdo connection to the test database is established in the constructor
28
 */
29
class SamlEntityRepository
30
{
31
    const SP_ACS_LOCATION = 'https://gateway.dev.openconext.local/test/authentication/consume-assertion';
32
33
    const SP_ADFS_SSO_LOCATION = 'https://gateway.dev.openconext.local/test/authentication/adfs/sso';
34
35
    private readonly PDO $connection;
36
37
    public function __construct(SmoketestPdoFactory $factory)
38
    {
39
        $this->connection = $factory->createConnection();
0 ignored issues
show
Bug introduced by
The property connection is declared read-only in Surfnet\StepupGateway\Be...ry\SamlEntityRepository.
Loading history...
40
    }
41
42
    public function createSpIfNotExists($entityId, $certificate, $sfoEnabled = false)
43
    {
44
        // Does the SP exist?
45
        $stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1');
46
        $stmt->bindParam('entityId', $entityId);
47
        $stmt->execute();
48
        if ($stmt->rowCount() === 0) {
49
            // If not, create it
50
            $uuid = Uuid::uuid4()->toString();
51
            $type = 'sp';
52
            $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...
53
            $configuration['public_key'] = $certificate;
54
            $configuration['loa'] = ['__default__' => 'http://dev.openconext.local/assurance/loa1'];
55
            $configuration['second_factor_only'] = $sfoEnabled;
56
            $configuration['set_sso_cookie_on_2fa'] = true;
57
            $configuration['allow_sso_on_2fa'] = true;
58
            $configuration['second_factor_only_nameid_patterns'] = [
59
                'urn:collab:person:stepup.example.com:admin',
60
                'urn:collab:person:dev.openconext.local:*',
61
            ];
62
63
            $data = [
64
                'entityId' => $entityId,
65
                'type' => $type,
66
                'configuration' => json_encode($configuration),
67
                'id' => $uuid,
68
            ];
69
            $sql = <<<SQL
70
            INSERT INTO saml_entity (
71
                `entity_id`,
72
                `type`,
73
                `configuration`,
74
                `id`
75
            )
76
            VALUES (
77
                :entityId, 
78
                :type, 
79
                :configuration, 
80
                :id                
81
            )
82
SQL;
83
            $stmt = $this->connection->prepare($sql);
84
            if ($stmt->execute($data)) {
85
                return $data;
86
            }
87
88
            throw new Exception(
89
                sprintf(
90
                    'Unable to insert the new SP saml_entity. PDO raised this error: "%s"',
91
                    $stmt->errorInfo()[2]
92
               )
93
            );
94
        } else {
95
            // Return the SP data
96
            $results = $stmt->fetchAll();
97
            $result = $results[0];
98
            $data = [
99
                'entityId' => $result['entity_id'],
100
                'type' => $result['type'],
101
                'configuration' => $result['configuration'],
102
                'id' => $result['id'],
103
            ];
104
105
            return $data;
106
        }
107
    }
108
109
    public function createIdpIfNotExists($entityId, $certificate)
110
    {
111
        // Does the SP exist?
112
        $stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1');
113
        $stmt->bindParam('entityId', $entityId, PDO::PARAM_STR);
114
        $stmt->execute();
115
        if ($stmt->rowCount() === 0) {
116
            // If not, create it
117
            $uuid = Uuid::uuid4()->toString();
118
            $type = 'idp';
119
120
            $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...
121
122
            $data = [
123
                'entityId' => $entityId,
124
                'type' => $type,
125
                'configuration' => json_encode($configuration),
126
                'id' => $uuid,
127
            ];
128
            $sql = <<<SQL
129
            INSERT INTO saml_entity (
130
                `entity_id`,
131
                `type`,
132
                `configuration`,
133
                `id`
134
            )
135
            VALUES (
136
                :entityId, 
137
                :type, 
138
                :configuration, 
139
                :id                
140
            )
141
SQL;
142
            $stmt = $this->connection->prepare($sql);
143
            if ($stmt->execute($data)) {
144
                return $data;
145
            }
146
147
            throw new Exception(
148
                sprintf(
149
                    'Unable to insert the new SP saml_entity. PDO raised this error: "%s"',
150
                    $stmt->errorInfo()[2]
151
                )
152
            );
153
        } else {
154
            // Return the SP data
155
            $results = $stmt->fetchAll();
156
            $result = $results[0];
157
            $data = [
158
                'entityId' => $result['entity_id'],
159
                'type' => $result['type'],
160
                'configuration' => $result['configuration'],
161
                'id' => $result['id'],
162
            ];
163
            return $data;
164
        }
165
    }
166
}
167