InstitutionConfigurationRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
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 function error_get_last;
23
24
/**
25
 * A poor-mans repository, a pdo connection to the test database is established in the constructor
26
 */
27
class InstitutionConfigurationRepository
28
{
29
30
    /**
31
     * @var Connection
32
     */
33
    private $connection;
34
35
    public function __construct(Connection $connection)
36
    {
37
        $this->connection = $connection;
38
    }
39
40
    public function configure(string $institution, string $option, bool $value)
41
    {
42
        // Does the SP exist?
43
        $stmt = $this->connection->prepare('SELECT * FROM institution_configuration WHERE institution=:institution LIMIT 1');
44
        $stmt->bindParam('institution', $institution);
45
        $stmt->execute();
46
        if ($stmt->rowCount() === 0) {
47
            // If not, create new InstitutionConfiguration
48
            $data = [
49
                'institution' => $institution,
50
                'sso_on2fa_enabled' => $value,
51
                'sso_registration_bypass' => $value,
52
            ];
53
            $sql = <<<SQL
54
                INSERT INTO institution_configuration (institution, sso_on2fa_enabled, sso_registration_bypass)
55
                VALUES (:institution, :sso_on2fa_enabled, :sso_registration_bypass);
56
SQL;
57
            $stmt = $this->connection->prepare($sql);
58
            if ($stmt->execute($data)) {
59
                return $data;
60
            }
61
            throw new Exception(
62
                sprintf(
63
                    'Unable to insert the new institution_configuration (%s). PDO raised this error: "%s"',
64
                    $stmt->queryString,
65
                    $stmt->errorInfo()[2]
66
                )
67
            );
68
        } else {
69
            $data = [
70
                'institution' => $institution,
71
                'value' => $value,
72
            ];
73
            $sql = <<<SQL
74
                update institution_configuration
75
                set `$option` = :value
76
                where institution = :institution
77
            ;
78
SQL;
79
            $stmt = $this->connection->prepare($sql);
80
            if ($stmt->execute($data)) {
81
                return $data;
82
            }
83
            return $data;
84
        }
85
    }
86
}
87