SecondFactorRepository::create()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 53
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 44
c 1
b 0
f 0
nc 8
nop 5
dl 0
loc 53
rs 9.216

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\StepupBundle\Value\VettingType;
25
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...
26
27
/**
28
 * A poor mans repository, a pdo connection to the test database is established in the constructor
29
 */
30
class SecondFactorRepository
31
{
32
    private readonly PDO $connection;
33
34
    public function __construct(SmoketestPdoFactory $factory)
35
    {
36
        $this->connection = $factory->createConnection();
0 ignored issues
show
Bug introduced by
The property connection is declared read-only in Surfnet\StepupGateway\Be...\SecondFactorRepository.
Loading history...
37
    }
38
39
    public function create($nameId, $tokenType, $institution, bool $selfAsserted = false, $identifier = null)
40
    {
41
        $uuid = Uuid::uuid4()->toString();
42
43
        // If an identifier is not important, simply use the UUID, otherwise use the provide one
44
        if (!$identifier) {
45
            $identifier = $uuid;
46
        }
47
48
        $data = [
49
            'identityId' => $uuid,
50
            'nameId' => $nameId,
51
            'institution' => $institution,
52
            'secondFactorId' => $uuid,
53
            'secondFactorType' => $tokenType,
54
            'secondFactorIdentifier' => $identifier,
55
            'id' => $uuid,
56
            'displayLocale' => 'en_GB',
57
            'identityVetted' => $selfAsserted ? 0 : 1,
58
        ];
59
        $sql = <<<SQL
60
            INSERT INTO second_factor (
61
                identity_id, 
62
                name_id, 
63
                institution, 
64
                second_factor_id, 
65
                second_factor_type, 
66
                second_factor_identifier, 
67
                id, 
68
                display_locale,
69
                identity_vetted
70
            )
71
            VALUES (
72
                :identityId, 
73
                :nameId, 
74
                :institution, 
75
                :secondFactorId, 
76
                :secondFactorType, 
77
                :secondFactorIdentifier, 
78
                :id, 
79
                :displayLocale,
80
                :identityVetted
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 second_factor. PDO raised this error: "%s"',
91
                $stmt->errorInfo()[2]
92
            )
93
        );
94
    }
95
96
    public function findBy(string $nameId, string $secondFactorType): array
97
    {
98
        $sql = 'SELECT * FROM `second_factor` WHERE `name_id` = :nameId AND `second_factor_type` = :type';
99
        $stmt = $this->connection->prepare($sql);
100
        $stmt->execute([
101
            'nameId' => $nameId,
102
            'type' => $secondFactorType
103
        ]);
104
        if ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
105
            return $result;
106
        }
107
        throw new Exception(sprintf('Unable to find %s SF token for %s', $secondFactorType, $nameId));
108
    }
109
110
    public function has(string $nameId, string $secondFactorType): bool
111
    {
112
        try {
113
            $this->findBy($nameId, $secondFactorType);
114
            return true;
115
        } catch (Exception $e) {
116
            return false;
117
        }
118
    }
119
}
120