Completed
Branch master (bcc3a6)
by Evan
04:36
created

Model::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
cc 1
eloc 3
nc 1
nop 0
rs 9.4285
1
<?php
2
3
namespace Silk\Type;
4
5
use Silk\Meta\ObjectMeta;
6
use Silk\Database\NullAction;
7
use Silk\Contracts\Executable;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * @property-read int    $id
12
 * @property-read object $object
13
 */
14
abstract class Model
15
{
16
    /**
17
     * The core model object
18
     * @var object
19
     */
20
    protected $object;
21
22
    /**
23
     * The object type in WordPress
24
     */
25
    const OBJECT_TYPE = '';
26
27
    /**
28
     * The name of the primary ID property on the object
29
     */
30
    const ID_PROPERTY = '';
31
32
    /**
33
     * Get the map of action => class for resolving active actions.
34
     *
35
     * @return array
36
     */
37
    abstract protected function actionClasses();
38
39
    /**
40
    * Get a new query builder for the model.
41
    *
42
    * @return \Silk\Contracts\BuildsQueries
43
    */
44
    abstract public function newQuery();
45
46
    /**
47
     * Create a new query builder instance for this model type.
48
     *
49
     * @return \Silk\Contracts\BuildsQueries
50
     */
51
    public static function query()
52
    {
53
        return (new static)->newQuery();
54
    }
55
56
    /**
57
     * Save the changes to the database.
58
     *
59
     * @return $this
60
     */
61
    public function save()
62
    {
63
        $this->activeAction('save');
64
65
        return $this;
66
    }
67
68
    /**
69
     * Delete the record from the database.
70
     *
71
     * @return $this
72
     */
73
    public function delete()
74
    {
75
        $this->activeAction('delete');
76
77
        return $this;
78
    }
79
80
    /**
81
     * Load and set the object from the database.
82
     *
83
     * @return $this
84
     */
85
    public function refresh()
86
    {
87
        $this->activeAction('load');
88
89
        return $this;
90
    }
91
92
    /**
93
     * Meta API for this type
94
     *
95
     * @param  string $key  Meta key to retrieve or empty to retrieve all.
96
     *
97
     * @return ObjectMeta|\Silk\Meta\Meta
98
     */
99
    public function meta($key = '')
100
    {
101
        $meta = new ObjectMeta(static::OBJECT_TYPE, $this->id);
102
103
        if ($key) {
104
            return $meta->get($key);
105
        }
106
107
        return $meta;
108
    }
109
110
    /**
111
     * Update the core object
112
     *
113
     * @param object $object
114
     *
115
     * @return $this
116
     */
117
    public function setObject($object)
118
    {
119
        $this->object = $object;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Set the primary ID on the model.
126
     *
127
     * @param string|int $id  The model's ID
128
     *
129
     * @return $this
130
     */
131
    public function setId($id)
132
    {
133
        $this->object->{static::ID_PROPERTY} = (int) $id;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Perform a database action.
140
     *
141
     * @return void
142
     */
143
    protected function activeAction($action)
144
    {
145
        $actionClass = Collection::make(
146
            $this->actionClasses()
147
        )->get($action, NullAction::class);
148
149
        $this->executeAction(new $actionClass($this));
150
    }
151
152
    /**
153
     * Execute the active action
154
     *
155
     * @param Executable $action
156
     *
157
     * @return void
158
     */
159
    protected function executeAction(Executable $action)
160
    {
161
        $action->execute();
162
    }
163
164
    /**
165
     * Magic getter.
166
     *
167
     * @param  string $property
168
     *
169
     * @return mixed
170
     */
171
    public function __get($property)
172
    {
173
        if ($property == 'id') {
174
            return $this->object->{static::ID_PROPERTY};
175
        }
176
177
        if (in_array($property, ['object', static::OBJECT_TYPE])) {
178
            return $this->object;
179
        }
180
181
        return $this->object->$property;
182
    }
183
184
    /**
185
     * Magic Isset Checker.
186
     *
187
     * @return bool
188
     */
189
    public function __isset($property)
190
    {
191
        return ! is_null($this->__get($property));
192
    }
193
194
    /**
195
     * Magic setter.
196
     *
197
     * @param string $property  The property name
198
     * @param mixed  $value     The new property value
199
     */
200
    public function __set($property, $value)
201
    {
202
        if (property_exists($this->object, $property)) {
203
            $this->object->$property = $value;
204
        }
205
    }
206
207
    /**
208
     * Handle dynamic method calls into the model.
209
     *
210
     * @param  string $method
211
     * @param  array $arguments
212
     *
213
     * @return mixed
214
     */
215
    public function __call($method, $arguments)
216
    {
217
        $query = $this->newQuery();
218
219
        return call_user_func_array([$query, $method], $arguments);
220
    }
221
222
    /**
223
     * Handle dynamic static method calls on the model class.
224
     *
225
     * Proxies calls to direct method calls on a new instance
226
     *
227
     * @param string $method
228
     * @param array $arguments
229
     *
230
     * @return mixed
231
     */
232
    public static function __callStatic($method, array $arguments)
233
    {
234
        return call_user_func_array([new static, $method], $arguments);
235
    }
236
}
237