Completed
Pull Request — master (#322)
by Mathieu
03:43
created

Json::setFilesystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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