Completed
Pull Request — master (#4)
by
unknown
01:34
created

FileStorage::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Library for confirmation tokens.
4
 *
5
 * @link      https://github.com/hiqdev/php-confirmator
6
 * @package   php-confirmator
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\confirmator;
12
13
class FileStorage implements StorageInterface
14
{
15
    public $path;
16
17 3
    public function __construct($path = null)
18
    {
19 3
        $this->path = $path;
20 3
    }
21
22 3
    protected function getFullPath($name)
23
    {
24 3
        return $this->path . '/' . $name[0] . '/' . $name;
25
    }
26
27 2
    public function has($name)
28
    {
29 2
        return is_file($this->getFullPath($name));
30
    }
31
32 2
    public function get($name)
33
    {
34 2
        $path = $this->getFullPath($name);
35
36 2
        return is_file($path) ? file_get_contents($this->getFullPath($name)) : null;
37
    }
38
39 3
    public function set($name, $text)
40
    {
41 3
        $path = $this->getFullPath($name);
42 3
        $dir = dirname($path);
43 3
        if (!is_dir($dir)) {
44 3
            if (!mkdir($dir, 0755, true)) {
45
                throw new \Exception('Could not create storage');
46
            }
47
        }
48
49 3
        return file_put_contents($path, $text);
50
    }
51
52
    public function remove($name)
53
    {
54
        $path = $this->getFullPath($name);
55
56
        return unlink($path);
57
    }
58
}
59