FileSystem::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bavix\Flow;
4
5
use Bavix\Helpers\Dir;
6
use Bavix\Helpers\File;
7
8
class FileSystem
9
{
10
11
    /**
12
     * @var Flow
13
     */
14
    protected $flow;
15
16
    /**
17
     * @var string[]
18
     */
19
    protected $paths = [];
20
21
    /**
22
     * @var string
23
     */
24
    protected $path;
25
26
    /**
27
     * FileSystem constructor.
28
     *
29
     * @param Flow   $flow
30
     * @param string $path
31
     */
32 20
    public function __construct(Flow $flow, string $path)
33
    {
34 20
        $this->flow = $flow;
35 20
        $this->path = $path;
36 20
    }
37
38
    /**
39
     * @param string $view
40
     *
41
     * @return bool
42
     */
43 20
    public function has(string $view): bool
44
    {
45 20
        $path      = $this->get($view);
46 20
        $real      = $this->flow->native()->path($view . $this->flow->ext());
47 20
        $directory = \dirname($path);
48
49 20
        if (empty($this->paths[$view]) && ($this->flow->debugMode() || !File::exists($path)))
50
        {
51 20
            Dir::make($directory);
52
53 20
            return false;
54
        }
55
56 5
        return \filemtime($path) > \filemtime($real);
57
    }
58
59
    /**
60
     * @param string $view
61
     * @param string $data
62
     */
63 19
    public function set(string $view, string $data)
64
    {
65 19
        $this->paths[$view] = true;
66
67 19
        \file_put_contents(
68 19
            $this->get($view),
69 19
            $data
70
        );
71 19
    }
72
73
    /**
74
     * @param string $view
75
     *
76
     * @return string
77
     */
78 20
    public function get(string $view): string
79
    {
80 20
        return $this->path . '/' . \preg_replace('~\W~', '/', $view) . '.php';
81
    }
82
83
}
84