Completed
Pull Request — feature/acceptance-tests (#191)
by Michiel
02:42 queued 58s
created

SamlEntityRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createSp() 0 40 2
1
<?php
2
3
namespace Surfnet\StepupGateway\Behat\Repository;
4
5
use Exception;
6
use Ramsey\Uuid\Uuid;
7
8
/**
9
 * A poor mans repository, a pdo connection to the test database is established in the constructor
10
 */
11
class SamlEntityRepository
12
{
13
    const SP_ACS_LOCATION = 'https://gateway.stepup.example.com/test/authentication/consume-assertion';
14
15
    /**
16
     * @var Connection
17
     */
18
    private $connection;
19
20
    public function __construct(Connection $connection)
21
    {
22
        $this->connection = $connection;
23
    }
24
25
    public function createSp($entityId, $certificate, $sfoEnabled = false)
26
    {
27
        $uuid = Uuid::uuid4()->toString();
28
        $type = 'sp';
29
        $configuration['acs'] = [self::SP_ACS_LOCATION];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$configuration was never initialized. Although not strictly required by PHP, it is generally a good practice to add $configuration = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
30
        $configuration['public_key'] = $certificate;
31
        $configuration['loa'] = ['__default__' => 'http://stepup.example.com/assurance/loa1'];
32
        $configuration['second_factor_only'] = $sfoEnabled;
33
        $configuration['second_factor_only_nameid_patterns'] = [
34
            'urn:collab:person:stepup.example.com:admin',
35
            'urn:collab:person:stepup.example.com:*',
36
        ];
37
38
        $data = [
39
            'entityId' => $entityId,
40
            'type' => $type,
41
            'configuration' => json_encode($configuration),
42
            'id' => $uuid,
43
        ];
44
        $sql = <<<SQL
45
            INSERT INTO saml_entity (
46
                `entity_id`,
47
                `type`,
48
                `configuration`,
49
                `id`
50
            )
51
            VALUES (
52
                :entityId, 
53
                :type, 
54
                :configuration, 
55
                :id                
56
            )
57
SQL;
58
        $stmt = $this->connection->prepare($sql);
59
        if ($stmt->execute($data)) {
60
            return $data;
61
        }
62
63
        throw new Exception('Unable to insert the new SP saml_entity');
64
    }
65
}
66