Completed
Push — master ( 4580d0...cfdf98 )
by ARCANEDEV
03:07
created

JsonStore::read()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
crap 2
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 json file path.
23
     *
24
     * @var string
25
     */
26
    protected $path;
27
28
    /* ------------------------------------------------------------------------------------------------
29
     |  Constructor
30
     | ------------------------------------------------------------------------------------------------
31
     */
32
    /**
33
     * Make the Json store instance.
34
     *
35
     * @param  \Illuminate\Filesystem\Filesystem  $files
36
     * @param  string                             $path
37
     */
38 36
    public function __construct(Filesystem $files, $path = null)
39
    {
40 36
        $this->files = $files;
0 ignored issues
show
Bug introduced by
The property files does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41 36
        $this->setPath($path ?: storage_path('app/settings.json'));
42 30
    }
43
44
    /* ------------------------------------------------------------------------------------------------
45
     |  Getters & Setters
46
     | ------------------------------------------------------------------------------------------------
47
     */
48
    /**
49
     * Set the path for the JSON file.
50
     *
51
     * @param  string  $path
52
     *
53
     * @return self
54
     */
55 36
    public function setPath($path)
56
    {
57
        if (
58 36
            ! $this->files->exists($path) &&
59 6
            $this->files->put($path, '{}') === false
60 36
        ) {
61 3
            throw new InvalidArgumentException("Could not write to $path.");
62
        }
63
64 33
        if ( ! $this->files->isWritable($path)) {
65 3
            throw new InvalidArgumentException("$path is not writable.");
66
        }
67
68 30
        $this->path = $path;
69
70 30
        return $this;
71
    }
72
73
    /* ------------------------------------------------------------------------------------------------
74
     |  Other Functions
75
     | ------------------------------------------------------------------------------------------------
76
     */
77
    /**
78
     * {@inheritdoc}
79
     */
80 24
    protected function read()
81
    {
82 24
        $contents = $this->files->get($this->path);
83
84 24
        $data = json_decode($contents, true);
85
86 24
        if ($data === null) {
87 3
            throw new RuntimeException("Invalid JSON in {$this->path}");
88
        }
89
90 21
        return $data;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 15
    protected function write(array $data)
97
    {
98 15
        $contents = ! empty($data) ? json_encode($data) : '{}';
99
100 15
        $this->files->put($this->path, $contents);
101 15
    }
102
}
103