Passed
Push — master ( 5e2b09...a39721 )
by Ben
09:28 queued 02:28
created

HasDynamicAttributes::getAttribute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 15
rs 10
c 1
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Thinktomorrow\Chief\DynamicAttributes;
6
7
trait HasDynamicAttributes
8
{
9
    public function dynamic(string $key, string $index = null)
10
    {
11
        return $this->dynamicAttributesInstance()->get($index ? "$index.$key" : $key);
12
    }
13
14
    public function setDynamic(string $key, $value, string $index = null)
15
    {
16
        return $this->dynamicAttributesInstance()->set($index ? "$index.$key" : $key, $value);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->dynamicAttributes....'.$key : $key, $value) targeting Thinktomorrow\Chief\Dyna...ynamicAttributes::set() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
17
    }
18
19
    public function isDynamicKey($key): bool
20
    {
21
        if (array_key_exists($key, $this->attributes)) {
22
            return false;
23
        }
24
25
        return in_array($key, $this->dynamicKeys());
26
    }
27
28
    /**
29
     * The attribute key which the dynamic attributes is
30
     * referenced by as well as the database column name.
31
     *
32
     * @return string
33
     */
34
    protected function getDynamicKey(): string
35
    {
36
        return 'values';
37
    }
38
39
    /**
40
     * The attributes that should be treated as dynamic ones. This
41
     * is a list of keys matching the database column names.
42
     * @return array
43
     */
44
    protected function dynamicKeys(): array
45
    {
46
        return property_exists($this, 'dynamicKeys') ? $this->dynamicKeys : [];
47
    }
48
49
    /**
50
     * Part of the custom cast. Method used by the save logic on eloquent. We override this so we can
51
     * inject an json version of the dynamic attributes for saving into the database.
52
     *
53
     * @return mixed
54
     */
55
    public function getAttributes()
56
    {
57
        return $this->injectDynamicAttributes($this->attributes, false);
58
    }
59
60
    /* Part of the custom cast */
61
    public function setRawAttributes(array $attributes, $sync = false)
62
    {
63
        return parent::setRawAttributes($this->injectDynamicAttributes($attributes), $sync);
64
    }
65
66
    /* Part of the custom cast */
67
    public function fill(array $attributes)
68
    {
69
        return parent::fill($this->injectDynamicAttributes($attributes));
70
    }
71
72
    /*
73
     * If the dynamic attributes contain a localized value, this has preference over any non-localized
74
     */
75
    public function getAttribute($key)
76
    {
77
        if (!$this->isDynamicKey($key)) {
78
            return parent::getAttribute($key);
79
        }
80
81
        $locale = app()->getLocale();
0 ignored issues
show
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

81
        $locale = app()->/** @scrutinizer ignore-call */ getLocale();
Loading history...
82
83
        foreach (["{$locale}.$key", $key] as $k) {
84
            if ($this->dynamicAttributesInstance()->has($k)) {
85
                return $this->dynamic($k);
86
            }
87
        }
88
89
        return parent::getAttribute($key);
90
    }
91
92
    /* Part of the custom cast */
93
    public function setAttribute($key, $value)
94
    {
95
        if ($this->isDynamicKey($key)) {
96
            return $this->setDynamic($key, $value);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setDynamic($key, $value) targeting Thinktomorrow\Chief\Dyna...ttributes::setDynamic() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
97
        }
98
99
        return parent::setAttribute($key, $value);
100
    }
101
102
    /* Part of the custom cast */
103
    protected function dynamicAttributesInstance(): DynamicAttributes
104
    {
105
        if (!isset($this->attributes[$this->getDynamicKey()])) {
106
            $this->attributes[$this->getDynamicKey()] = DynamicAttributes::fromRawValue([]);
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
107
        } elseif (!($raw = $this->attributes[$this->getDynamicKey()]) instanceof DynamicAttributes) {
108
            $this->attributes[$this->getDynamicKey()] = DynamicAttributes::fromRawValue($raw);
109
        }
110
111
        return $this->attributes[$this->getDynamicKey()];
112
    }
113
114
    /**
115
     * Inject the dynamic attributes into the attributes array.
116
     * Either as a DynamicAttributes instance or back to a json format.
117
     *
118
     * @param array $attributes
119
     * @param bool $castToObject
120
     * @return array
121
     */
122
    private function injectDynamicAttributes(array $attributes, bool $castToObject = true): array
123
    {
124
        if (isset($attributes[$this->getDynamicKey()])) {
125
            $attributes[$this->getDynamicKey()] = $castToObject
126
                ? DynamicAttributes::fromRawValue($attributes[$this->getDynamicKey()])
127
                : $attributes[$this->getDynamicKey()]->toJson();
128
        }
129
130
        return $attributes;
131
    }
132
}
133