Completed
Push — master ( a40fc9...4580d0 )
by ARCANEDEV
03:02
created

JsonStore   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.26%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 88
rs 10
c 1
b 0
f 0
ccs 18
cts 23
cp 0.7826

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A setPath() 0 17 4
A read() 0 12 2
A write() 0 6 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 15
    public function __construct(Filesystem $files, $path = null)
39
    {
40 15
        $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 15
        $this->setPath($path ?: storage_path('app/settings.json'));
42 9
    }
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 15
    public function setPath($path)
56
    {
57
        if (
58 15
            ! $this->files->exists($path) &&
59 6
            $this->files->put($path, '{}') === false
60 15
        ) {
61 3
            throw new InvalidArgumentException("Could not write to $path.");
62
        }
63
64 12
        if ( ! $this->files->isWritable($path)) {
65 3
            throw new InvalidArgumentException("$path is not writable.");
66
        }
67
68 9
        $this->path = $path;
69
70 9
        return $this;
71
    }
72
73
    /* ------------------------------------------------------------------------------------------------
74
     |  Other Functions
75
     | ------------------------------------------------------------------------------------------------
76
     */
77
    /**
78
     * {@inheritdoc}
79
     */
80 3
    protected function read()
81
    {
82 3
        $contents = $this->files->get($this->path);
83
84 3
        $data = json_decode($contents, true);
85
86 3
        if ($data === null) {
87 3
            throw new RuntimeException("Invalid JSON in {$this->path}");
88
        }
89
90
        return $data;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    protected function write(array $data)
97
    {
98
        $contents = ! empty($data) ? json_encode($data) : '{}';
99
100
        $this->files->put($this->path, $contents);
101
    }
102
}
103