Test Failed
Pull Request — develop (#30)
by Michiel
07:02
created

Tiqr_UserSecretStorage_Pdo::log()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
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 2
40
    private $handle;
41 2
42
    /**
43 2
     * Construct a user class
44
     *
45
     * @param array $config The configuration that a specific user class may use.
46
     */
47 2
    public function __construct(
48
        Tiqr_UserSecretStorage_Encryption_Interface $encryption,
49
        LoggerInterface $logger,
50
        string $dsn,
51
        string $userName,
52
        string $password,
53
        string $tableName = 'tiqrusersecret'
54
    ) {
55
        $this->encryption = $encryption;
56 2
        $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...
57
        $this->tableName = $tableName;
58 2
        try {
59 2
            $this->handle = new PDO($dsn, $userName, $password);
60 2
        } catch (PDOException $e) {
61
            $this->logger->error(
62
                sprintf('Unable to establish a PDO connection. Error message from PDO: %s', $e->getMessage())
63
            );
64
        }
65
    }
66
    private function userExists($userId)
67
    {
68
        $sth = $this->handle->prepare("SELECT userid FROM ".$this->tableName." WHERE userid = ?");
69 2
        if ($sth->execute(array($userId))) {
70
            return true;
71 2
        }
72 1
        $this->logger->debug('Unable fot find user in user secret storage (PDO)');
73
        return false;
74 1
    }
75
76 2
    /**
77 2
     * Get the user's secret
78
     *
79
     * @param String $userId
80
     *
81
     * @return String The user's secret
82
     */
83
    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...
84
    {
85
        $sth = $this->handle->prepare("SELECT secret FROM ".$this->tableName." WHERE userid = ?");
86
        if($sth->execute(array($userId))) {
87
            return $sth->fetchColumn();
88
        }
89
        $this->logger->error('Unable to retrieve user secret from user secret storage (PDO)');
90
    }
91
92
    /**
93
     * Store a secret for a user.
94
     *
95
     * @param String $userId
96
     * @param String $secret
97
     */
98
    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...
99
    {
100
        if ($this->userExists($userId)) {
101
            $sth = $this->handle->prepare("UPDATE ".$this->tableName." SET secret = ? WHERE userid = ?");
102
        } else {
103
            $sth = $this->handle->prepare("INSERT INTO ".$this->tableName." (secret,userid) VALUES (?,?)");
104
        }
105
        if (!$sth->execute(array($secret,$userId))) {
106
            $this->logger->error('Unable to persist user secret in user secret storage (PDO)');
107
        }
108
    }
109
110
    /**
111
     * @deprecated this log method was probably a dev left-over.
112
     */
113
    public static function log($message)
114
    {
115
        $fp = fopen('/var/www/tiqr/logs/'.date("Ymd").'.log', 'a');
116
        fwrite($fp, $message);
117
        fclose($fp);
118
    }
119
}
120