InstitutionConfigurationRepository   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 53
rs 10
c 3
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A configure() 0 44 4
1
<?php declare(strict_types=1);
2
3
/**
4
 * Copyright 2023 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 Surfnet\StepupGateway\Behat\ValueObject\SmoketestPdoFactory;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupGateway\Be...ect\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...
24
use function error_get_last;
25
26
/**
27
 * A poor-mans repository, a pdo connection to the test database is established in the constructor
28
 */
29
class InstitutionConfigurationRepository
30
{
31
    private readonly PDO $connection;
32
33
    public function __construct(SmoketestPdoFactory $factory)
34
    {
35
        $this->connection = $factory->createConnection();
0 ignored issues
show
Bug introduced by
The property connection is declared read-only in Surfnet\StepupGateway\Be...ConfigurationRepository.
Loading history...
36
    }
37
38
    public function configure(string $institution, string $option, bool $value)
39
    {
40
        // Does the SP exist?
41
        $stmt = $this->connection->prepare('SELECT * FROM institution_configuration WHERE institution=:institution LIMIT 1');
42
        $stmt->bindParam('institution', $institution);
43
        $stmt->execute();
44
        if ($stmt->rowCount() === 0) {
45
            // If not, create new InstitutionConfiguration
46
            $data = [
47
                'institution' => $institution,
48
                'sso_on2fa_enabled' => $value,
49
                'sso_registration_bypass' => $value,
50
            ];
51
            $sql = <<<SQL
52
                INSERT INTO institution_configuration (institution, sso_on2fa_enabled, sso_registration_bypass)
53
                VALUES (:institution, :sso_on2fa_enabled, :sso_registration_bypass);
54
SQL;
55
            $stmt = $this->connection->prepare($sql);
56
            if ($stmt->execute($data)) {
57
                return $data;
58
            }
59
            throw new Exception(
60
                sprintf(
61
                    'Unable to insert the new institution_configuration (%s). PDO raised this error: "%s"',
62
                    $stmt->queryString,
63
                    $stmt->errorInfo()[2]
64
                )
65
            );
66
        } else {
67
            $data = [
68
                'institution' => $institution,
69
                'value' => $value,
70
            ];
71
            $sql = <<<SQL
72
                update institution_configuration
73
                set `$option` = :value
74
                where institution = :institution
75
            ;
76
SQL;
77
            $stmt = $this->connection->prepare($sql);
78
            if ($stmt->execute($data)) {
79
                return $data;
80
            }
81
            return $data;
82
        }
83
    }
84
}
85