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

Tiqr_UserStorage_File::_loadUser()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.8666
cc 4
nc 6
nop 2
crap 4.0119
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
use Psr\Log\LoggerInterface;
21
22 1
require_once 'Tiqr/UserStorage/GenericStore.php';
23
24
/**
25
 * This user storage implementation implements a simple user storage using json files.
26
 * This is mostly for demonstration and development purposes. In a production environment
27
 * please supply your own implementation that hosts the data in your user database OR
28
 * in a secure (e.g. hardware encrypted) storage.
29
 * @author ivo
30
 */
31
class Tiqr_UserStorage_File extends Tiqr_UserStorage_GenericStore
32
{
33
    protected $_path;
34
35
    /**
36
     * Create an instance
37
     * @param $config
38
     */
39 1
    public function __construct($config, LoggerInterface $logger, $secretconfig = array())
40
    {
41 1
        parent::__construct($config, $logger, $secretconfig);
42 1
        $this->_path = $config["path"];
43 1
    }
44
45
    /**
46
     * This function takes care of actually saving the user data to a JSON file.
47
     * @param String $userId
48
     * @param array $data
49
     */
50 1
    protected function _saveUser($userId, $data)
51
    {
52 1
        if (file_put_contents($this->getPath().$userId.".json", json_encode($data)) === false) {
53
            $this->logger->error('Unable to save the user to user storage (file storage)');
54
            return false;
55
        }
56 1
        return true;
57
    }
58
  
59
    /**
60
     * This function takes care of loading the user data from a JSON file.
61
     * @param String $userId
62
     * @return false if the data is not present, or an array containing the data.
63
     */
64 1
    protected function _loadUser($userId, $failIfNotFound = TRUE)
65
    {
66 1
        $fileName = $this->getPath().$userId.".json";
67
68 1
        $data = NULL;
69 1
        if (file_exists($fileName)) { 
70 1
            $data = json_decode(file_get_contents($this->getPath().$userId.".json"), true);
71
        }
72
73 1
        if ($data === NULL) {
74 1
            if ($failIfNotFound) {
75
                throw new Exception('Error loading data for user: ' . var_export($userId, TRUE));
76
            } else {
77 1
                $this->logger->error('Error loading data for user from user storage (file storage)');
78 1
                return false;
79
            }
80
        } else {
81 1
            return $data;
82
        }
83
    }
84
85
    /**
86
     * Delete user data (un-enroll).
87
     * @param String $userId
88
     */
89
    protected function _deleteUser($userId)
90
    {
91
        $filename = $this->getPath().$userId.".json";
92
        if (file_exists($filename)) {
93
            unlink($filename);
94
        } else {
95
            $this->logger->error('Unable to remove the user from user storage (file storage)');
96
        }
97
    }
98
99
    /**
100
     * Retrieve the path where the json files are stored.
101
     * @return String
102
     */
103 1
    public function getPath()
104
    {
105 1
         if (substr($this->_path, -1)!="/") return $this->_path."/";
106
         return $this->_path;
107
    }
108
    
109
}
110