WhitelistRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A whitelist() 0 22 3
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
24
/**
25
 * A poor mans repository, a pdo connection to the test database is established in the constructor
26
 */
27
class WhitelistRepository
28
{
29
    /**
30
     * @var Connection
31
     */
32
    private $connection;
33
34
    public function __construct(Connection $connection)
35
    {
36
        $this->connection = $connection;
37
    }
38
39
    /**
40
     * @param string $institution
41
     * @return array
42
     * @throws Exception
43
     */
44
    public function whitelist($institution)
45
    {
46
        // Does the whitelist entry exist?
47
        $stmt = $this->connection->prepare('SELECT * FROM whitelist_entry WHERE institution=:institution LIMIT 1');
48
        $stmt->bindParam('institution', $institution, PDO::PARAM_STR);
49
        $stmt->execute();
50
        if ($stmt->rowCount() === 0) {
51
            $sql = "INSERT INTO whitelist_entry (`institution`) VALUES (:institution);";
52
            $stmt = $this->connection->prepare($sql);
53
            $data = ['institution' => $institution];
54
            if ($stmt->execute($data)) {
55
                return $data;
56
            }
57
            throw new Exception(
58
                sprintf(
59
                    'Unable ad the institution to the whitelist. PDO raised this error: "%s"',
60
                    $stmt->errorInfo()[2]
61
                )
62
            );
63
        } else {
64
            // Return the existing whitelist data
65
            return $stmt->fetchAll()[0];
66
        }
67
    }
68
}
69