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

FileStorage::getFullPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
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