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

FileStorage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 61
ccs 16
cts 19
cp 0.8421
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
B set() 0 27 6
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\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