Passed
Pull Request — develop (#27)
by Michiel
06:20
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
     * @param Tiqr_UserSecretStorage_Encryption_Interface $encryption
44
     * @param LoggerInterface $logger
45
     * @param string $dsn
46
     * @param string $userName
47
     * @param string $password
48
     * @param string $tableName
49
     */
50 5
    public function __construct(
51
        Tiqr_UserSecretStorage_Encryption_Interface $encryption,
52
        LoggerInterface $logger,
53
        string $dsn,
54
        string $userName,
55
        string $password,
56
        string $tableName = 'tiqrusersecret'
57
    ) {
58 5
        $this->encryption = $encryption;
59 5
        $this->logger = $logger;
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60 5
        $this->tableName = $tableName;
61
        try {
62 5
            $this->handle = new PDO($dsn, $userName, $password);
63 1
        } catch (PDOException $e) {
64 1
            $this->logger->error(
65 1
                sprintf('Unable to establish a PDO connection. Error message from PDO: %s', $e->getMessage())
66
            );
67
        }
68 5
    }
69 1
    private function userExists($userId)
70
    {
71 1
        $sth = $this->handle->prepare("SELECT userid FROM ".$this->tableName." WHERE userid = ?");
72 1
        $sth->execute(array($userId));
73 1
        $result = $sth->fetchColumn();
74 1
        if ($result !== false) {
75
            return true;
76
        }
77 1
        $this->logger->debug('Unable fot find user in user secret storage (PDO)');
78 1
        return false;
79
    }
80
81
    /**
82
     * Get the user's secret
83
     *
84
     * @param String $userId
85
     *
86
     * @return mixed: null|string
87
     */
88 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...
89
    {
90 1
        $sth = $this->handle->prepare("SELECT secret FROM ".$this->tableName." WHERE userid = ?");
91 1
        if($sth->execute(array($userId))) {
92 1
            $secret = $sth->fetchColumn();
93 1
            if ($secret !== false) {
94 1
                return $secret;
95
            }
96
        }
97
        $this->logger->error('Unable to retrieve user secret from user secret storage (PDO)');
98
    }
99
100
    /**
101
     * Store a secret for a user.
102
     *
103
     * @param String $userId
104
     * @param String $secret
105
     */
106 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...
107
    {
108 1
        if ($this->userExists($userId)) {
109
            $sth = $this->handle->prepare("UPDATE ".$this->tableName." SET secret = ? WHERE userid = ?");
110
        } else {
111 1
            $sth = $this->handle->prepare("INSERT INTO ".$this->tableName." (secret,userid) VALUES (?,?)");
112
        }
113 1
        $result = $sth->execute(array($secret,$userId));
114 1
        if (!$result) {
115
            $this->logger->error('Unable to persist user secret in user secret storage (PDO)');
116
        }
117 1
    }
118
119
    /**
120
     * @deprecated this log method was probably a dev left-over.
121
     */
122
    public static function log($message)
123
    {
124
        $fp = fopen('/var/www/tiqr/logs/'.date("Ymd").'.log', 'a');
125
        fwrite($fp, $message);
126
        fclose($fp);
127
    }
128
}
129