Passed
Push — develop ( 22eefe...e65dcd )
by Pieter van der
06:04
created

Tiqr_UserStorage_File   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 22
c 1
b 0
f 0
dl 0
loc 70
ccs 18
cts 25
cp 0.72
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _saveUser() 0 4 1
A _deleteUser() 0 5 2
A getPath() 0 4 2
A __construct() 0 4 1
A _loadUser() 0 17 4
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 Ivo Jansch <[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
20 1
require_once 'Tiqr/UserStorage/GenericStore.php';
21
22
/**
23
 * This user storage implementation implements a simple user storage using json files.
24
 * This is mostly for demonstration and development purposes. In a production environment
25
 * please supply your own implementation that hosts the data in your user database OR
26
 * in a secure (e.g. hardware encrypted) storage.
27
 * @author ivo
28
 */
29
class Tiqr_UserStorage_File extends Tiqr_UserStorage_GenericStore
30
{
31
    protected $_path;
32
33
    /**
34
     * Create an instance
35
     * @param $config
36
     */
37 1
    public function __construct($config, $secretconfig = array())
38
    {
39 1
        parent::__construct($config, $secretconfig);
40 1
        $this->_path = $config["path"];
41 1
    }
42
43
    /**
44
     * This function takes care of actually saving the user data to a JSON file.
45
     * @param String $userId
46
     * @param array $data
47
     */
48 1
    protected function _saveUser($userId, $data)
49
    {
50 1
        file_put_contents($this->getPath().$userId.".json", json_encode($data));
51 1
        return true;
52
    }
53
  
54
    /**
55
     * This function takes care of loading the user data from a JSON file.
56
     * @param String $userId
57
     * @return false if the data is not present, or an array containing the data.
58
     */
59 1
    protected function _loadUser($userId, $failIfNotFound = TRUE)
60
    {
61 1
        $fileName = $this->getPath().$userId.".json";
62
63 1
        $data = NULL;
64 1
        if (file_exists($fileName)) { 
65 1
            $data = json_decode(file_get_contents($this->getPath().$userId.".json"), true);
66
        }
67
68 1
        if ($data === NULL) {
69 1
            if ($failIfNotFound) {
70
                throw new Exception('Error loading data for user: ' . var_export($userId, TRUE));
71
            } else {
72 1
                return false;
73
            }
74
        } else {
75 1
            return $data;
76
        }
77
    }
78
79
    /**
80
     * Delete user data (un-enroll).
81
     * @param String $userId
82
     */
83
    protected function _deleteUser($userId)
84
    {
85
        $filename = $this->getPath().$userId.".json";
86
        if (file_exists($filename)) {
87
            unlink($filename);
88
        }
89
    }
90
91
    /**
92
     * Retrieve the path where the json files are stored.
93
     * @return String
94
     */
95 1
    public function getPath()
96
    {
97 1
         if (substr($this->_path, -1)!="/") return $this->_path."/";
98
         return $this->_path;
99
    }
100
    
101
}
102