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