Passed
Pull Request — develop (#295)
by Peter
04:30
created

InstitutionConfigurationRepository::configure()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 38
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 30
nc 4
nop 3
dl 0
loc 38
rs 9.44
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Surfnet\StepupGateway\Behat\Repository;
4
5
use Exception;
6
use function error_get_last;
7
8
/**
9
 * A poor-mans repository, a pdo connection to the test database is established in the constructor
10
 */
11
class InstitutionConfigurationRepository
12
{
13
14
    /**
15
     * @var Connection
16
     */
17
    private $connection;
18
19
    public function __construct(Connection $connection)
20
    {
21
        $this->connection = $connection;
22
    }
23
24
    public function configure(string $institution, string $option, bool $value)
25
    {
26
        // Does the SP exist?
27
        $stmt = $this->connection->prepare('SELECT * FROM institution_configuration WHERE institution=:institution LIMIT 1');
28
        $stmt->bindParam('institution', $institution);
29
        $stmt->execute();
30
        if ($stmt->rowCount() === 0) {
31
            // If not, create new InstitutionConfiguration
32
            $data = [
33
                'institution' => $institution,
34
                'sso_on2fa_enabled' => $value,
35
            ];
36
            $sql = <<<SQL
37
                INSERT INTO institution_configuration (institution, sso_on2fa_enabled)
38
                VALUES (:institution, :sso_on2fa_enabled);
39
SQL;
40
            $stmt = $this->connection->prepare($sql);
41
            if ($stmt->execute($data)) {
42
                return $data;
43
            }
44
            throw new Exception(sprintf('Unable to insert the new institution_configuration (%s)', $stmt->queryString));
45
        } else {
46
            $data = [
47
                'institution' => $institution,
48
                'option' => $option,
49
                'value' => $value,
50
            ];
51
            $sql = <<<SQL
52
                update gateway.institution_configuration
53
                set :option = :value
54
                where institutiton = :institution
55
            );
56
SQL;
57
            $stmt = $this->connection->prepare($sql);
58
            if ($stmt->execute($data)) {
59
                return $data;
60
            }
61
            return $data;
62
        }
63
    }
64
}
65