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

FileManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 62
ccs 18
cts 24
cp 0.75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 6 2
A read() 0 7 2
A empty() 0 6 2
A hasCacheFile() 0 5 1
A filePath() 0 4 1
A filePathBase() 0 6 2
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