JsonStore::setPath()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 17
rs 9.2
c 1
b 0
f 0
ccs 9
cts 9
cp 1
cc 4
eloc 9
nc 3
nop 1
crap 4
1
<?php namespace Arcanedev\Settings\Stores;
2
3
use Arcanedev\Settings\Bases\Store;
4
use Arcanedev\Settings\Contracts\Store as StoreContract;
5
use Illuminate\Filesystem\Filesystem;
6
use InvalidArgumentException;
7
use RuntimeException;
8
9
/**
10
 * Class     JsonStore
11
 *
12
 * @package  Arcanedev\Settings\Stores
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class JsonStore extends Store implements StoreContract
16
{
17
    /* ------------------------------------------------------------------------------------------------
18
     |  Properties
19
     | ------------------------------------------------------------------------------------------------
20
     */
21
    /**
22
     * The Filesystem instance.
23
     *
24
     * @var \Illuminate\Filesystem\Filesystem
25
     */
26
    protected $files;
27
28
    /**
29
     * The json file path.
30
     *
31
     * @var string
32
     */
33
    protected $path;
34
35
    /* ------------------------------------------------------------------------------------------------
36
     |  Constructor
37
     | ------------------------------------------------------------------------------------------------
38
     */
39
    /**
40
     * Make the Json store instance.
41
     *
42
     * @param  \Illuminate\Filesystem\Filesystem  $files
43
     * @param  string                             $path
44
     */
45 39
    public function __construct(Filesystem $files, $path = null)
46
    {
47 39
        $this->files = $files;
48 39
        $this->setPath($path ?: storage_path('app/settings.json'));
49 39
    }
50
51
    /* ------------------------------------------------------------------------------------------------
52
     |  Getters & Setters
53
     | ------------------------------------------------------------------------------------------------
54
     */
55
    /**
56
     * Set the path for the JSON file.
57
     *
58
     * @param  string  $path
59
     *
60
     * @return self
61
     */
62 39
    public function setPath($path)
63
    {
64
        if (
65 39
            ! $this->files->exists($path) &&
66 33
            $this->files->put($path, '{}') === false
67 39
        ) {
68 3
            throw new InvalidArgumentException("Could not write to $path.");
69
        }
70
71 39
        if ( ! $this->files->isWritable($path)) {
72 3
            throw new InvalidArgumentException("$path is not writable.");
73
        }
74
75 39
        $this->path = $path;
76
77 39
        return $this;
78
    }
79
80
    /* ------------------------------------------------------------------------------------------------
81
     |  Other Functions
82
     | ------------------------------------------------------------------------------------------------
83
     */
84
    /**
85
     * {@inheritdoc}
86
     */
87 24
    protected function read()
88
    {
89 24
        $contents = $this->files->get($this->path);
90
91 24
        $data = json_decode($contents, true);
92
93 24
        if ($data === null) {
94 3
            throw new RuntimeException("Invalid JSON in {$this->path}");
95
        }
96
97 21
        return $data;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 15
    protected function write(array $data)
104
    {
105 15
        $contents = ! empty($data) ? json_encode($data) : '{}';
106
107 15
        $this->files->put($this->path, $contents);
108 15
    }
109
}
110