Completed
Push — master ( 56570e...2c8793 )
by Gabriel
07:17
created

FileManager::hasCacheFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Nip\Dispatcher\Resolver\Cache;
4
5
/**
6
 * Class FileManager
7
 * @package Nip\Dispatcher\Resolver\Cache
8
 */
9
class FileManager
10
{
11
    /**
12
     * @param DefinitionsCollection $definitionCollection
13
     * @param null $path
14
     */
15 3
    public static function write($definitionCollection, $path = null)
16
    {
17 3
        $path = $path ? $path : static::filePath();
18 3
        $content = '<?php return ' . var_export($definitionCollection->all(), true) . ';';
19 3
        file_put_contents($path, $content);
20 3
    }
21
22
    /**
23
     * @param DefinitionsCollection $definitionCollection
24
     * @param null $path
25
     */
26 2
    public static function read($definitionCollection, $path = null)
27
    {
28 2
        $path = $path ? $path : static::filePath();
29
        /** @noinspection PhpIncludeInspection */
30 2
        $items = require $path;
31 2
        $definitionCollection->replace($items);
32 2
    }
33
34
    /**
35
     * @param null $path
36
     */
37
    public static function empty($path = null)
38
    {
39
        $path = $path ? $path : static::filePath();
40
        $content = '<?php return [];';
41
        file_put_contents($path, $content);
42
    }
43
44
    /**
45
     * @return bool
46
     */
47 1
    public static function hasCacheFile()
48
    {
49 1
        $path = static::filePath();
50 1
        return is_file($path);
51
    }
52
53
    /**
54
     * @return string
55
     */
56 3
    public static function filePath()
57
    {
58 3
        return static::filePathBase() . '/dispatcher.php';
59
    }
60
61
    /**
62
     * @return string
63
     */
64 3
    public static function filePathBase()
65
    {
66 3
        return function_exists('app')
67
            ? app('path.storage') . DIRECTORY_SEPARATOR . 'cache'
68 3
            : dirname(dirname(dirname(__DIR__))) . '/cache';
69
    }
70
}
71