Completed
Push — feature/implement-state-handli... ( bd5ae0 )
by Michiel
01:52
created

WhitelistRepository::whitelist()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Surfnet\StepupGateway\Behat\Repository;
4
5
use Exception;
6
use PDO;
7
8
/**
9
 * A poor mans repository, a pdo connection to the test database is established in the constructor
10
 */
11
class WhitelistRepository
12
{
13
    /**
14
     * @var Connection
15
     */
16
    private $connection;
17
18
    public function __construct(Connection $connection)
19
    {
20
        $this->connection = $connection;
21
    }
22
23
    /**
24
     * @param string $institution
25
     * @return array
26
     * @throws Exception
27
     */
28
    public function whitelist($institution)
29
    {
30
        // Does the whitelist entry exist?
31
        $stmt = $this->connection->prepare('SELECT * FROM whitelist_entry WHERE institution=:institution LIMIT 1');
32
        $stmt->bindParam('institution', $institution, PDO::PARAM_STR);
33
        $stmt->execute();
34
        if ($stmt->rowCount() === 0) {
35
            $sql = "INSERT INTO whitelist_entry (`institution`) VALUES (:institution);";
36
            $stmt = $this->connection->prepare($sql);
37
            $data = ['institution' => $institution];
38
            if ($stmt->execute($data)) {
39
                return $data;
40
            }
41
            throw new Exception('Unable add the institution to the whitelist');
42
        } else {
43
            // Return the existing whitelist data
44
            return reset($stmt->fetchAll());
0 ignored issues
show
Bug introduced by
$stmt->fetchAll() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
45
        }
46
    }
47
}
48