Completed
Pull Request — master (#2)
by Klochok
01:42
created

FileStorage   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 44
ccs 0
cts 19
cp 0
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
use Yii;
14
15
class FileStorage implements StorageInterface
16
{
17
    public $path;
18
19
    public function __construct($path = null)
20
    {
21
        $this->path = $path;
22
    }
23
24
    protected function getFullPath($name)
25
    {
26
        return Yii::getAlias($this->path) . '/' . $name[0] . '/' . $name;
27
    }
28
29
    public function has($name)
30
    {
31
        return is_file($this->getFullPath($name));
32
    }
33
34
    public function get($name)
35
    {
36
        $path = $this->getFullPath($name);
37
38
        return is_file($path) ? file_get_contents($this->getFullPath($name)) : null;
39
    }
40
41
    public function set($name, $text)
42
    {
43
        $path = $this->getFullPath($name);
44
        $dir = dirname($path);
45
        if (!is_dir($dir)) {
46
            mkdir($dir, 0755, true);
47
        }
48
49
        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