SettingModel::setKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Anomaly\SettingsModule\Setting;
2
3
use Anomaly\SettingsModule\Setting\Command\GetValueFieldType;
4
use Anomaly\SettingsModule\Setting\Command\GetValuePresenter;
5
use Anomaly\SettingsModule\Setting\Command\ModifyValue;
6
use Anomaly\SettingsModule\Setting\Contract\SettingInterface;
7
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
8
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter;
9
use Anomaly\Streams\Platform\Model\Settings\SettingsSettingsEntryModel;
10
11
/**
12
 * Class SettingModel
13
 *
14
 * @link          http://pyrocms.com/
15
 * @author        PyroCMS, Inc. <[email protected]>
16
 * @author        Ryan Thompson <[email protected]>
17
 */
18
class SettingModel extends SettingsSettingsEntryModel implements SettingInterface
19
{
20
21
    /**
22
     * Return the value field.
23
     *
24
     * @return FieldType
25
     */
26
    public function field()
27
    {
28
        /* @var FieldType $field */
29
        $field = $this->dispatch(new GetValueFieldType($this));
30
31
        if (!$field) {
32
            return null;
33
        }
34
35
        return $field;
36
    }
37
38
    /**
39
     * Get the key.
40
     *
41
     * @return string
42
     */
43
    public function getKey()
44
    {
45
        return $this->key;
46
    }
47
48
    /**
49
     * Set the key.
50
     *
51
     * @param $key
52
     * @return $this
53
     */
54
    public function setKey($key)
55
    {
56
        $this->key = $key;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Get the value.
63
     *
64
     * @return string
65
     */
66
    public function getValue()
67
    {
68
        return $this->value;
69
    }
70
71
    /**
72
     * Set the value attribute.
73
     *
74
     * @param $value
75
     * @return $this
76
     */
77
    public function setValue($value)
78
    {
79
        $this->value = $value;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Set the value.
86
     *
87
     * @param $value
88
     * @return $this
89
     */
90
    protected function setValueAttribute($value)
91
    {
92
        $this->attributes['value'] = $this->dispatch(new ModifyValue($this, $value));
93
94
        return $this;
95
    }
96
97
    /**
98
     * Get the value attribute.
99
     *
100
     * @return mixed
101
     */
102
    protected function getValueAttribute()
103
    {
104
        if (!$field = $this->field()) {
105
            return null;
106
        }
107
108
        return $field->getValue();
109
    }
110
111
    /**
112
     * Get the field type's presenter
113
     * for a given field slug.
114
     *
115
     * We're overriding this to catch
116
     * the "value" key.
117
     *
118
     * @param $fieldSlug
119
     * @return FieldTypePresenter
120
     */
121
    public function getFieldTypePresenter($fieldSlug)
122
    {
123
        if ($fieldSlug == 'value') {
124
            return $this->dispatch(new GetValuePresenter($this));
125
        }
126
127
        return parent::getFieldTypePresenter($fieldSlug);
128
    }
129
}
130