Test Failed
Pull Request — develop (#35)
by Michiel
03:51
created

Tiqr_StateStorage_File::getValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 15
ccs 4
cts 4
cp 1
rs 9.9332
cc 4
nc 3
nop 1
crap 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-2011 SURFnet BV
18
 */
19
20
use Psr\Log\LoggerInterface;
21
22
23
/**
24
 * File based implementation to store session state data. 
25
 * Note that it is more secure to use a memory based storage such as memcached.
26
 * This implementation is mostly for demo, test or experimental setups that 
27
 * do not have access to a memcache instance.
28
 * 
29
 * This StateStorage implementation has no options, files are always stored
30
 * in /tmp and prefixed with tiqr_state_*
31
 * 
32
 * @author ivo
33
 *
34
 */
35
class Tiqr_StateStorage_File implements Tiqr_StateStorage_StateStorageInterface
36
{
37
    private $logger;
38
39 3
    private $path;
40
41 3
    public function __construct(string $path, LoggerInterface $logger)
42 3
    {
43 3
        $this->logger = $logger;
44 3
        $this->path = $path;
45
    }
46 3
47
    /**
48 3
     * (non-PHPdoc)
49
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::setValue()
50
     */
51
    public function setValue($key, $value, $expire=0)
52
    {   
53
        $envelope = array("expire"=>$expire,
54
                          "createdAt"=>time(),
55 3
                          "value"=>$value);
56
        $filename = $this->getFilenameByKey($key);
57 3
        
58 3
        if (!file_put_contents($filename, serialize($envelope))) {
59 3
            $this->logger->error('Unable to write the value to state storage');
60
        }
61 1
        
62
        return $key;
63 3
    }
64
65
    /**
66
     * (non-PHPdoc)
67
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::unsetValue()
68
     */
69 3
    public function unsetValue($key)
70
    {
71 3
        $filename = $this->getFilenameByKey($key);
72 3
        if (file_exists($filename)) {
73 3
            unlink($filename);
74 3
        } else {
75
            $this->logger->error('Unable to unlink the value from state storage, key not found on filesystem');
76 3
        }
77 1
    }
78 1
    
79 1
    /**
80
     * (non-PHPdoc)
81
     * @see library/tiqr/Tiqr/StateStorage/Tiqr_StateStorage_Abstract::getValue()
82 3
     */
83
    public function getValue($key)
84 3
    {
85 3
        $filename = $this->getFilenameByKey($key);
86
        if (file_exists($filename)) {
87
            $envelope = unserialize(file_get_contents($filename), ['allowed_classes' => false]);
88
            // This data is time-limited. If it's too old we discard it.
89
            if (($envelope["expire"] != 0) && time() - $envelope["createdAt"] > $envelope["expire"]) {
90
                $this->unsetValue($key);
91
                $this->logger->notice('Unable to retrieve the state storage value, it is expired');
92 3
                return null;
93
            }
94 3
            return $envelope["value"];
95
        }
96
        $this->logger->notice('Unable to retrieve the state storage value, file not found');
97
        return NULL;
98
    }
99
100
    private function getPath(): string
101
    {
102
        if (substr($this->path, -1)!=="/") {
103
            return $this->path . "/";
104
        }
105
        return $this->path;
106
    }
107
108
    /**
109
     * Determine the name of a temporary file to hold the contents of $key
110
     */
111
    private function getFilenameByKey(string $key): string
112
    {
113
        return sprintf(
114
            "%stiqr_state_%s",
115
            $this->getPath(),
116
            strtr(base64_encode($key), '+/', '-_')
117
        );
118
    }
119
120
    public function init()
121
    {
122
        # Nothing to do here
123
    }
124
}
125