Completed
Push — master ( 8803de...e3dc66 )
by Andrii
03:17
created

FileStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 44
ccs 16
cts 19
cp 0.8421
rs 10
c 0
b 0
f 0

6 Methods

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