Completed
Push — feature/implement-state-handli... ( bd5ae0 )
by Michiel
01:52
created

SecondFactorRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 SecondFactorRepository
12
{
13
    /**
14
     * @var Connection
15
     */
16
    private $connection;
17
18
    public function __construct(Connection $connection)
19
    {
20
        $this->connection = $connection;
21
    }
22
23
    public function create($nameId, $tokenType, $institution, $identifier = null)
24
    {
25
        $uuid = Uuid::uuid4()->toString();
26
27
        // If an identifier is not importand, simply use the UUID, otherwise use the provide one
28
        if (!$identifier) {
29
            $identifier = $uuid;
30
        }
31
32
        $data = [
33
            'identityId' => $uuid,
34
            'nameId' => $nameId,
35
            'institution' => $institution,
36
            'secondFactorId' => $uuid,
37
            'secondFactorType' => $tokenType,
38
            'secondFactorIdentifier' => $identifier,
39
            'id' => $uuid,
40
            'displayLocale' => 'en_GB',
41
        ];
42
        $sql = <<<SQL
43
            INSERT INTO second_factor (
44
                identity_id, 
45
                name_id, 
46
                institution, 
47
                second_factor_id, 
48
                second_factor_type, 
49
                second_factor_identifier, 
50
                id, 
51
                display_locale
52
            )
53
            VALUES (
54
                :identityId, 
55
                :nameId, 
56
                :institution, 
57
                :secondFactorId, 
58
                :secondFactorType, 
59
                :secondFactorIdentifier, 
60
                :id, 
61
                :displayLocale
62
            )
63
SQL;
64
        $stmt = $this->connection->prepare($sql);
65
        if ($stmt->execute($data)) {
66
            return $data;
67
        }
68
69
        throw new Exception('Unable to insert the new second_factor');
70
    }
71
}
72