Completed
Push — master ( fe10ec...920617 )
by Dmitry
02:08
created

FileStorage::set()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7.8984

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 5
cts 8
cp 0.625
rs 8.8657
c 0
b 0
f 0
cc 6
nc 14
nop 2
crap 7.8984
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\base\ErrorException;
14
15
class FileStorage implements StorageInterface
16
{
17 3
    public $path;
18
19 3
    public function __construct($path = null)
20 3
    {
21
        $this->path = $path;
22 3
    }
23
24 3
    protected function getFullPath($name)
25
    {
26
        return $this->path . '/' . $name[0] . '/' . $name;
27 2
    }
28
29 2
    public function has($name)
30
    {
31
        return is_file($this->getFullPath($name));
32 2
    }
33
34 2
    public function get($name)
35
    {
36 2
        $path = $this->getFullPath($name);
37
38
        return is_file($path) ? file_get_contents($this->getFullPath($name)) : null;
39 3
    }
40
41 3
    public function set($name, $text)
42 3
    {
43 3
        $path = $this->getFullPath($name);
44 3
        $dir = dirname($path);
45
        if (!is_dir($dir)) {
46
            try {
47 3
                $success = mkdir($dir, 0755, true);
48
            } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
49
                $success = false;
50
            }
51
            if (!$success) {
52
                throw new \Exception('Could not create storage in ' . $dir);
53
            }
54
        }
55
56
        try {
57
            $success = file_put_contents($path, $text);
58
        } catch (\Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
59
            $success = false;
60
        }
61
62
        if ($success === false) {
63
            throw new \Exception('Failed write file: ' . $path);
64
        }
65
66
        return $success;
67
    }
68
69
    public function remove($name)
70
    {
71
        $path = $this->getFullPath($name);
72
73
        return unlink($path);
74
    }
75
}
76