Completed
Push — master ( bd6aca...1d4cf9 )
by Nicolas
9s
created

Json::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Nwidart\Modules;
4
5
use Illuminate\Filesystem\Filesystem;
6
7
class Json
8
{
9
    /**
10
     * The file path.
11
     *
12
     * @var string
13
     */
14
    protected $path;
15
16
    /**
17
     * The laravel filesystem instance.
18
     *
19
     * @var \Illuminate\Filesystem\Filesystem
20
     */
21
    protected $filesystem;
22
23
    /**
24
     * The attributes collection.
25
     *
26
     * @var \Illuminate\Support\Collection
27
     */
28
    protected $attributes;
29
30
    /**
31
     * The constructor.
32
     *
33
     * @param mixed                             $path
34
     * @param \Illuminate\Filesystem\Filesystem $filesystem
35
     */
36 72
    public function __construct($path, Filesystem $filesystem = null)
37
    {
38 72
        $this->path = (string) $path;
39 72
        $this->filesystem = $filesystem ?: new Filesystem();
40 72
        $this->attributes = Collection::make($this->getAttributes());
41 72
    }
42
43
    /**
44
     * Get filesystem.
45
     *
46
     * @return Filesystem
47
     */
48 1
    public function getFilesystem()
49
    {
50 1
        return $this->filesystem;
51
    }
52
53
    /**
54
     * Set filesystem.
55
     *
56
     * @param Filesystem $filesystem
57
     *
58
     * @return $this
59
     */
60
    public function setFilesystem(Filesystem $filesystem)
61
    {
62
        $this->filesystem = $filesystem;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Get path.
69
     *
70
     * @return string
71
     */
72 72
    public function getPath()
73
    {
74 72
        return $this->path;
75
    }
76
77
    /**
78
     * Set path.
79
     *
80
     * @param mixed $path
81
     *
82
     * @return $this
83
     */
84 1
    public function setPath($path)
85
    {
86 1
        $this->path = (string) $path;
87
88 1
        return $this;
89
    }
90
91
    /**
92
     * Make new instance.
93
     *
94
     * @param string                            $path
95
     * @param \Illuminate\Filesystem\Filesystem $filesystem
96
     *
97
     * @return static
98
     */
99 56
    public static function make($path, Filesystem $filesystem = null)
100
    {
101 56
        return new static($path, $filesystem);
102
    }
103
104
    /**
105
     * Get file content.
106
     *
107
     * @return string
108
     */
109 72
    public function getContents()
110
    {
111 72
        return $this->filesystem->get($this->getPath());
112
    }
113
114
    /**
115
     * Get file contents as array.
116
     *
117
     * @return array
118
     */
119 72
    public function getAttributes()
120
    {
121 72
        $attributes = json_decode($this->getContents(), 1);
122
123
        // any JSON parsing errors should throw an exception
124 72
        if (json_last_error() > 0) {
125 1
            throw new \Exception('Error processing file: ' . $this->getPath() . '. Error: ' . json_last_error_msg());
126
        }
127
128 72
        if (config('modules.cache.enabled') === false) {
129 72
            return $attributes;
130
        }
131
132
        return app('cache')->remember($this->getPath(), config('modules.cache.lifetime'), function () use ($attributes) {
133
            return $attributes;
134
        });
135
    }
136
137
    /**
138
     * Convert the given array data to pretty json.
139
     *
140
     * @param array $data
141
     *
142
     * @return string
143
     */
144 3
    public function toJsonPretty(array $data = null)
145
    {
146 3
        return json_encode($data ?: $this->attributes, JSON_PRETTY_PRINT);
147
    }
148
149
    /**
150
     * Update json contents from array data.
151
     *
152
     * @param array $data
153
     *
154
     * @return bool
155
     */
156
    public function update(array $data)
157
    {
158
        $this->attributes = new Collection(array_merge($this->attributes->toArray(), $data));
159
160
        return $this->save();
161
    }
162
163
    /**
164
     * Set a specific key & value.
165
     *
166
     * @param string $key
167
     * @param mixed  $value
168
     *
169
     * @return $this
170
     */
171 3
    public function set($key, $value)
172
    {
173 3
        $this->attributes->offsetSet($key, $value);
174
175 3
        return $this;
176
    }
177
178
    /**
179
     * Save the current attributes array to the file storage.
180
     *
181
     * @return bool
182
     */
183 2
    public function save()
184
    {
185 2
        return $this->filesystem->put($this->getPath(), $this->toJsonPretty());
186
    }
187
188
    /**
189
     * Handle magic method __get.
190
     *
191
     * @param string $key
192
     *
193
     * @return mixed
194
     */
195 1
    public function __get($key)
196
    {
197 1
        return $this->get($key);
198
    }
199
200
    /**
201
     * Get the specified attribute from json file.
202
     *
203
     * @param $key
204
     * @param null $default
205
     *
206
     * @return mixed
207
     */
208 66
    public function get($key, $default = null)
209
    {
210 66
        return $this->attributes->get($key, $default);
211
    }
212
213
    /**
214
     * Handle call to __call method.
215
     *
216
     * @param string $method
217
     * @param array  $arguments
218
     *
219
     * @return mixed
220
     */
221
    public function __call($method, $arguments = [])
222
    {
223
        if (method_exists($this, $method)) {
224
            return call_user_func_array([$this, $method], $arguments);
225
        }
226
227
        return call_user_func_array([$this->attributes, $method], $arguments);
228
    }
229
230
    /**
231
     * Handle call to __toString method.
232
     *
233
     * @return string
234
     */
235 1
    public function __toString()
236
    {
237 1
        return $this->getContents();
238
    }
239
}
240