Passed
Branch develop (201d1e)
by Pieter van der
11:58 queued 06:01
created

Tiqr_UserSecretStorage_Pdo::userExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 8
cp 0.875
rs 10
cc 2
nc 2
nop 1
crap 2.0078
1
<?php
2
/**
3
 * This file is part of the tiqr project.
4
 * 
5
 * The tiqr project aims to provide an open implementation for 
6
 * authentication using mobile devices. It was initiated by 
7
 * SURFnet and developed by Egeniq.
8
 *
9
 * More information: http://www.tiqr.org
10
 *
11
 * @author Patrick Honing <[email protected]>
12
 * 
13
 * @package tiqr
14
 *
15
 * @license New BSD License - See LICENSE file for details.
16
 *
17
 * @copyright (C) 2010-2012 SURFnet BV
18
 * 
19
 * Create SQL table (MySQL):
20
 * CREATE TABLE `tiqrusersecret` (`userid` varchar(10) PRIMARY KEY, `secret` varchar(100))
21
 * 
22
 */
23
24 1
require_once 'Tiqr/UserSecretStorage/UserSecretStorageTrait.php';
25
26
use Psr\Log\LoggerInterface;
27
28
/**
29
 * This user storage implementation implements a user secret storage using PDO.
30
 * It is usable for any database with a PDO driver
31
 * 
32
 * @author Patrick Honing <[email protected]>
33
 */
34
class Tiqr_UserSecretStorage_Pdo implements Tiqr_UserSecretStorage_Interface
35
{
36
    use UserSecretStorageTrait;
37
38
    private $tableName;
39
40
    private $handle;
41
42
    /**
43
     * @var LoggerInterface
44
     */
45
    private $logger;
46
47
    /**
48
     * @param Tiqr_UserSecretStorage_Encryption_Interface $encryption
49
     * @param LoggerInterface $logger
50
     * @param string $dsn
51
     * @param string $userName
52
     * @param string $password
53
     * @param string $tableName
54
     */
55 5
    public function __construct(
56
        Tiqr_UserSecretStorage_Encryption_Interface $encryption,
57
        LoggerInterface $logger,
58
        string $dsn,
59
        string $userName,
60
        string $password,
61
        string $tableName = 'tiqrusersecret'
62
    ) {
63 5
        $this->encryption = $encryption;
64 5
        $this->logger = $logger;
65 5
        $this->tableName = $tableName;
66
        try {
67 5
            $this->handle = new PDO($dsn, $userName, $password);
68 1
        } catch (PDOException $e) {
69 1
            $this->logger->error(
70 1
                sprintf('Unable to establish a PDO connection. Error message from PDO: %s', $e->getMessage())
71
            );
72
        }
73 5
    }
74 1
    private function userExists($userId)
75
    {
76 1
        $sth = $this->handle->prepare("SELECT userid FROM ".$this->tableName." WHERE userid = ?");
77 1
        $sth->execute(array($userId));
78 1
        $result = $sth->fetchColumn();
79 1
        if ($result !== false) {
80
            return true;
81
        }
82 1
        $this->logger->debug('Unable fot find user in user secret storage (PDO)');
83 1
        return false;
84
    }
85
86
    /**
87
     * Get the user's secret
88
     *
89
     * @param String $userId
90
     *
91
     * @return mixed: null|string
92
     */
93 1
    private function getUserSecret($userId)
0 ignored issues
show
Unused Code introduced by
The method getUserSecret() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
94
    {
95 1
        $sth = $this->handle->prepare("SELECT secret FROM ".$this->tableName." WHERE userid = ?");
96 1
        if($sth->execute(array($userId))) {
97 1
            $secret = $sth->fetchColumn();
98 1
            if ($secret !== false) {
99 1
                return $secret;
100
            }
101
        }
102
        $this->logger->error('Unable to retrieve user secret from user secret storage (PDO)');
103
    }
104
105
    /**
106
     * Store a secret for a user.
107
     *
108
     * @param String $userId
109
     * @param String $secret
110
     */
111 1
    private function setUserSecret($userId, $secret)
0 ignored issues
show
Unused Code introduced by
The method setUserSecret() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
112
    {
113 1
        if ($this->userExists($userId)) {
114
            $sth = $this->handle->prepare("UPDATE ".$this->tableName." SET secret = ? WHERE userid = ?");
115
        } else {
116 1
            $sth = $this->handle->prepare("INSERT INTO ".$this->tableName." (secret,userid) VALUES (?,?)");
117
        }
118 1
        $result = $sth->execute(array($secret,$userId));
119 1
        if (!$result) {
120
            $this->logger->error('Unable to persist user secret in user secret storage (PDO)');
121
        }
122 1
    }
123
}
124