Completed
Push — master ( ed9f5f...162e0f )
by Evan
8s
created

ActiveRecord::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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