Completed
Pull Request — master (#3)
by Klochok
02:08
created

FileStorage::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 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\yii2\confirmator;
12
13
use hiqdev\php\confirmator\StorageInterface;
14
use Yii;
15
16
class FileStorage implements StorageInterface
17
{
18
    public $path;
19
20
    public function __construct($path = null)
21
    {
22
        $this->path = Yii::getAlias($path);
23
    }
24
25
    protected function getFullPath($name)
26
    {
27
        return $this->path . '/' . $name[0] . '/' . $name;
28
    }
29
30
    public function has($name)
31
    {
32
        return is_file($this->getFullPath($name));
33
    }
34
35
    public function get($name)
36
    {
37
        $path = $this->getFullPath($name);
38
39
        return is_file($path) ? file_get_contents($this->getFullPath($name)) : null;
40
    }
41
42
    public function set($name, $text)
43
    {
44
        $path = $this->getFullPath($name);
45
        $dir = dirname($path);
46
        if (!is_dir($dir)) {
47
            mkdir($dir, 0755, true);
48
        }
49
50
        return file_put_contents($path, $text);
51
    }
52
53
    public function remove($name)
54
    {
55
        $path = $this->getFullPath($name);
56
57
        return unlink($path);
58
    }
59
}
60