Completed
Pull Request — master (#1)
by Klochok
06:34
created

FileStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 44
ccs 17
cts 20
cp 0.85
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
 * 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
            mkdir($dir, 0755, true);
45 3
        }
46
47 3
        return file_put_contents($path, $text);
48
    }
49
50
    public function remove($name)
51
    {
52
        $path = $this->getFullPath($name);
53
54
        return unlink($path);
55
    }
56
}
57