Passed
Pull Request — develop (#27)
by Michiel
06:19
created

Tiqr_UserSecretStorage_Pdo   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 60%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 21
c 1
b 0
f 0
dl 0
loc 62
ccs 15
cts 25
cp 0.6
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A log() 0 5 1
A getUserSecret() 0 7 2
A __construct() 0 9 3
A setUserSecret() 0 9 3
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
use Psr\Log\LoggerInterface;
25
26 1
require_once 'Tiqr/UserStorage/Pdo.php';
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 extends Tiqr_UserStorage_Pdo implements Tiqr_UserSecretStorage_Interface
35
{
36
    /**
37
     * Construct a user class
38
     *
39
     * @param array $config The configuration that a specific user class may use.
40
     */
41 2
    public function __construct($config, LoggerInterface $logger, $secretconfig = array())
42
    {
43 2
        $this->logger = $logger;
44 2
        $this->tablename = isset($config['table']) ? $config['table'] : 'tiqrusersecret';
45
        try {
46 2
            $this->handle = new PDO($config['dsn'],$config['username'],$config['password']);
47
        } catch (PDOException $e) {
48
            $this->logger->error(
49
                sprintf('Unable to establish a PDO connection. Error message from PDO: %s', $e->getMessage())
50
            );
51
        }
52 2
    }
53
54
    /**
55
     * Get the user's secret
56
     *
57
     * @param String $userId
58
     *
59
     * @return String The user's secret
60
     */
61 2
    public function getUserSecret($userId)
62
    {
63 2
        $sth = $this->handle->prepare("SELECT secret FROM ".$this->tablename." WHERE userid = ?");
64 2
        if($sth->execute(array($userId))) {
65 2
            return $sth->fetchColumn();
66
        }
67
        $this->logger->error('Unable to retrieve user secret from user secret storage (PDO)');
68
    }
69
70
    /**
71
     * Store a secret for a user.
72
     *
73
     * @param String $userId
74
     * @param String $secret
75
     */
76 2
    public function setUserSecret($userId, $secret)
77
    {
78 2
        if ($this->userExists($userId)) {
79 1
            $sth = $this->handle->prepare("UPDATE ".$this->tablename." SET secret = ? WHERE userid = ?");
80
        } else {
81 1
            $sth = $this->handle->prepare("INSERT INTO ".$this->tablename." (secret,userid) VALUES (?,?)");
82
        }
83 2
        if (!$sth->execute(array($secret,$userId))) {
84
            $this->logger->error('Unable to persist user secret in user secret storage (PDO)');
85
        }
86 2
    }
87
88
    /**
89
     * @deprecated this log method was probably a dev left-over.
90
     */
91
    public static function log($message)
92
    {
93
        $fp = fopen('/var/www/tiqr/logs/'.date("Ymd").'.log', 'a');
94
        fwrite($fp, $message);
95
        fclose($fp);
96
    }
97
}
98