ModelJsonStorage::getRawArrayFromJson()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Okipa\LaravelModelJsonStorage;
4
5
use File;
6
use Illuminate\Support\Collection;
7
8
trait ModelJsonStorage
9
{
10
    use BuilderOverride;
11
    use BuildsQueriesOverride;
12
    use ModelOverride;
13
14
    /**
15
     * Make the given, typically hidden, attributes visible.
16
     *
17
     * @param  array|string $attributes
18
     *
19
     * @return $this
20
     */
21
    public abstract function makeVisible($attributes);
22
23
    /**
24
     * Set the array of model attributes. No checking is done.
25
     *
26
     * @param  array $attributes
27
     * @param  bool  $sync
28
     *
29
     * @return $this
30
     */
31
    public abstract function setRawAttributes(array $attributes, $sync = false);
32
33
    /**
34
     * Get the hidden attributes for the model.
35
     *
36
     * @return array
37
     */
38
    abstract public function getHidden();
39
40
    /**
41
     * Decode the given JSON back into an array or object.
42
     *
43
     * @param  string $value
44
     * @param  bool   $asObject
45
     *
46
     * @return mixed
47
     */
48
    abstract public function fromJson($value, $asObject = false);
49
50
    /**
51
     * Get the class name for polymorphic relations.
52
     *
53
     * @return string
54
     */
55
    abstract public function getMorphClass();
56
57
    /**
58
     * Load all of the models from the json file in the "modelsFromJson" variable.
59
     *
60
     * @return Collection
61
     */
62 22
    protected function loadModelsFromJson()
63
    {
64 22
        $modelsArray = $this->getRawArrayFromJson();
65 22
        foreach ($modelsArray as $key => $modelArray) {
66 21
            $modelsArray[$key] = app($this->getMorphClass())->setRawAttributes($modelArray);
0 ignored issues
show
Bug introduced by
The method setRawAttributes() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

66
            $modelsArray[$key] = app($this->getMorphClass())->/** @scrutinizer ignore-call */ setRawAttributes($modelArray);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
        }
68
69 22
        return collect($modelsArray);
70
    }
71
72
    /**
73
     * Get an array containing all of the models from the json file.
74
     *
75
     * @return array
76
     */
77 22
    protected function getRawArrayFromJson()
78
    {
79 22
        $modelsArray = [];
80 22
        if (file_exists($this->getJsonStoragePath())) {
81 21
            $modelsArray = $this->fromJson(File::get($this->getJsonStoragePath()));
82
        }
83
84 22
        return $modelsArray;
85
    }
86
}
87