Passed
Push — master ( 68f720...5e727b )
by Paul
10:53
created

Storage::retrieveAs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews;
4
5
use GeminiLabs\SiteReviews\Helpers\Arr;
6
use GeminiLabs\SiteReviews\Helpers\Cast;
7
8
trait Storage
9
{
10
    /**
11
     * @var Arguments
12
     */
13
    protected $storage;
14
15
    /**
16
     * @param string $property
17
     * @param mixed $value
18
     * @param string $key
19
     * @return false|array
20
     */
21 7
    public function append($property, $value, $key = null)
22
    {
23 7
        $stored = $this->retrieve($property, []);
24 7
        if (!is_array($stored)) {
25
            return false;
26
        }
27 7
        if ($key) {
28 7
            $stored[$key] = $value;
29
        } else {
30
            $stored[] = $value;
31
        }
32 7
        $this->store($property, $stored);
33 7
        return $stored;
34
    }
35
36
    /**
37
     * @param string $property
38
     * @return void
39
     */
40
    public function discard($property)
41
    {
42
        unset($this->storage()->$property);
43
    }
44
45
    /**
46
     * @param string $property
47
     * @param mixed $fallback
48
     * @return mixed
49
     */
50 21
    public function retrieve($property, $fallback = null)
51
    {
52 21
        return $this->storage()->get($property, $fallback);
53
    }
54
55
    /**
56
     * @param string $cast
57
     * @param string $property
58
     * @param mixed $fallback
59
     * @return mixed
60
     */
61 14
    public function retrieveAs($cast, $property, $fallback = null)
62
    {
63 14
        return Cast::to($cast, $this->storage()->get($property, $fallback));
64
    }
65
66
    /**
67
     * @return Arguments
68
     */
69 21
    public function storage()
70
    {
71 21
        if (!$this->storage instanceof Arguments) {
72
            $this->storage = new Arguments([]);
73
        }
74 21
        return $this->storage;
75
    }
76
77
    /**
78
     * @param string $property
79
     * @param mixed $value
80
     * @return void
81
     */
82 11
    public function store($property, $value)
83
    {
84 11
        $this->storage()->set($property, $value);
85 11
    }
86
}
87