Passed
Pull Request — develop (#295)
by Peter
04:30
created

SamlEntityRepository::createSpIfNotExists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 59
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 47
nc 3
nop 3
dl 0
loc 59
rs 9.1563
c 2
b 0
f 0

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
namespace Surfnet\StepupGateway\Behat\Repository;
4
5
use Exception;
6
use PDO;
7
use Ramsey\Uuid\Uuid;
8
9
/**
10
 * A poor mans repository, a pdo connection to the test database is established in the constructor
11
 */
12
class SamlEntityRepository
13
{
14
    const SP_ACS_LOCATION = 'https://gateway.stepup.example.com/test/authentication/consume-assertion';
15
16
    const SP_ADFS_SSO_LOCATION = 'https://gateway.stepup.example.com/test/authentication/adfs/sso';
17
18
    /**
19
     * @var Connection
20
     */
21
    private $connection;
22
23
    public function __construct(Connection $connection)
24
    {
25
        $this->connection = $connection;
26
    }
27
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];
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...
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
        }
88
    }
89
90
    public function createIdpIfNotExists($entityId, $certificate)
91
    {
92
        // Does the SP exist?
93
        $stmt = $this->connection->prepare('SELECT * FROM saml_entity WHERE entity_id=:entityId LIMIT 1');
94
        $stmt->bindParam('entityId', $entityId, PDO::PARAM_STR);
95
        $stmt->execute();
96
        if ($stmt->rowCount() === 0) {
97
            // If not, create it
98
            $uuid = Uuid::uuid4()->toString();
99
            $type = 'idp';
100
101
            $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...
102
103
            $data = [
104
                'entityId' => $entityId,
105
                'type' => $type,
106
                'configuration' => json_encode($configuration),
107
                'id' => $uuid,
108
            ];
109
            $sql = <<<SQL
110
            INSERT INTO saml_entity (
111
                `entity_id`,
112
                `type`,
113
                `configuration`,
114
                `id`
115
            )
116
            VALUES (
117
                :entityId, 
118
                :type, 
119
                :configuration, 
120
                :id                
121
            )
122
SQL;
123
            $stmt = $this->connection->prepare($sql);
124
            if ($stmt->execute($data)) {
125
                return $data;
126
            }
127
128
            throw new Exception('Unable to insert the new SP saml_entity');
129
        } else {
130
            // Return the SP data
131
            $results = $stmt->fetchAll();
132
            $result = $results[0];
133
            $data = [
134
                'entityId' => $result['entity_id'],
135
                'type' => $result['type'],
136
                'configuration' => $result['configuration'],
137
                'id' => $result['id'],
138
            ];
139
            return $data;
140
        }
141
    }
142
}
143