JSONStorage::all()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 1
b 0
f 1
cc 1
rs 10
1
<?php
2
3
namespace TPaksu\TodoBar\Storage;
4
5
use Illuminate\Support\Facades\Storage;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Storage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
class JSONStorage implements DataStorageInterface
8
{
9
    protected $client = null;
10
    protected $json = null;
11
    protected $file = null;
12
13
    public function __construct($params)
14
    {
15
        $this->file = $params["file"];
16
        $this->client = Storage::createLocalDriver(['root' => resource_path("/todobar")]);
0 ignored issues
show
Bug introduced by
The function resource_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

16
        $this->client = Storage::createLocalDriver(['root' => /** @scrutinizer ignore-call */ resource_path("/todobar")]);
Loading history...
17
18
        if ($this->client->exists($this->file) === false) {
19
            $this->client->put($this->file, "{\"projects\": []}");
20
        }
21
22
        $this->json = json_decode($this->client->get($this->file));
23
    }
24
25
    public function all()
26
    {
27
        return $this->json->projects;
28
    }
29
30
    public function find($id)
31
    {
32
        return $this->json->projects[$id] ?? null;
33
    }
34
35
    public function insert($values)
36
    {
37
        // reorder indexes
38
        $this->json->projects = array_values($this->json->projects);
39
40
        // get the new index
41
        $id = count($this->json->projects);
42
43
        // store new item
44
        $this->json->projects[$id] = new \stdClass();
45
46
        foreach ($values as $key => $value) {
47
            $this->json->projects[$id]->{$key} = $value;
48
        }
49
        // update the file
50
        $this->updateStorage();
51
52
        // return newly added id
53
        return $id;
54
    }
55
56
    public function update($id, $values)
57
    {
58
        if (isset($this->json->projects[$id])) {
59
            foreach ($values as $key => $value) {
60
                $this->json->projects[$id]->{$key} = $value;
61
            }
62
            $this->updateStorage();
63
        }
64
    }
65
66
    public function delete($id)
67
    {
68
        unset($this->json->projects[$id]);
69
70
        // reorder indexes
71
        $this->json->projects = array_values($this->json->projects);
72
73
        // update the file
74
        $this->updateStorage();
75
    }
76
77
    protected function updateStorage()
78
    {
79
        $this->client->put($this->file, json_encode($this->json));
80
    }
81
}
82