Completed
Branch develop (2a5993)
by Evan
02:52
created

Model   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 9
Bugs 0 Features 2
Metric Value
c 9
b 0
f 2
dl 0
loc 210
rs 10
wmc 16
lcom 1
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
newQuery() 0 1 ?
save() 0 1 ?
delete() 0 1 ?
refresh() 0 1 ?
A make() 0 8 2
A query() 0 4 1
A meta() 0 10 2
A setObject() 0 6 1
A setId() 0 6 1
A __get() 0 21 4
A __isset() 0 4 1
A __set() 0 8 2
A __call() 0 6 1
A __callStatic() 0 4 1
1
<?php
2
3
namespace Silk\Type;
4
5
use Silk\Meta\ObjectMeta;
6
7
/**
8
 * @property-read int    $id
9
 * @property-read object $object
10
 */
11
abstract class Model
12
{
13
    /**
14
     * The core model object
15
     * @var object
16
     */
17
    protected $object;
18
19
    /**
20
     * Type object property aliases
21
     * @var array
22
     */
23
    protected $objectAliases = [
24
        // 'aliasName' => 'propertyNameOnObject'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
    ];
26
27
    /**
28
     * The object type in WordPress
29
     */
30
    const OBJECT_TYPE = '';
31
32
    /**
33
     * The name of the primary ID property on the object
34
     */
35
    const ID_PROPERTY = '';
36
37
    /**
38
    * Get a new query builder for the model.
39
    *
40
    * @return \Silk\Contracts\BuildsQueries
41
    */
42
    abstract public function newQuery();
43
44
    /**
45
     * Save the changes to the database.
46
     *
47
     * @return $this
48
     */
49
    abstract public function save();
50
51
    /**
52
     * Delete the modeled record from the database.
53
     *
54
     * @return $this
55
     */
56
    abstract public function delete();
57
58
    /**
59
     * Reload the object from the database.
60
     *
61
     * @return $this
62
     */
63
    abstract public function refresh();
64
65
    /**
66
     * Make new instance.
67
     *
68
     * All provided arguments are forwarded to the constructor of the called class.
69
     *
70
     * @return static
71
     */
72
    public static function make()
73
    {
74
        if ($arguments = func_get_args()) {
75
            return (new \ReflectionClass(static::class))->newInstanceArgs($arguments);
76
        }
77
78
        return new static;
79
    }
80
81
    /**
82
     * Create a new query builder instance for this model type.
83
     *
84
     * @return \Silk\Contracts\BuildsQueries
85
     */
86
    public static function query()
87
    {
88
        return (new static)->newQuery();
89
    }
90
91
    /**
92
     * Meta API for this type
93
     *
94
     * @param  string $key  Meta key to retrieve or empty to retrieve all.
95
     *
96
     * @return ObjectMeta|\Silk\Meta\Meta
97
     */
98
    public function meta($key = '')
99
    {
100
        $meta = new ObjectMeta(static::OBJECT_TYPE, $this->id);
101
102
        if ($key) {
103
            return $meta->get($key);
104
        }
105
106
        return $meta;
107
    }
108
109
    /**
110
     * Update the core object
111
     *
112
     * @param object $object
113
     *
114
     * @return $this
115
     */
116
    public function setObject($object)
117
    {
118
        $this->object = $object;
119
120
        return $this;
121
    }
122
123
    /**
124
     * Set the primary ID on the model.
125
     *
126
     * @param string|int $id  The model's ID
127
     *
128
     * @return $this
129
     */
130
    protected function setId($id)
131
    {
132
        $this->object->{static::ID_PROPERTY} = (int) $id;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Magic getter.
139
     *
140
     * @param  string $property
141
     *
142
     * @return mixed
143
     */
144
    public function __get($property)
145
    {
146
        if ($property == 'id') {
147
            return $this->object->{static::ID_PROPERTY};
148
        }
149
150
        if (in_array($property, ['object', static::OBJECT_TYPE])) {
151
            return $this->object;
152
        }
153
154
        if (isset($this->objectAliases[$property])) {
155
            return data_get($this->object, $this->objectAliases[$property]);
156
        }
157
158
        /**
159
         * Finally, hand-off the request to the wrapped object.
160
         * We don't check for existence as we leverage the magic __get
161
         * on the wrapped object as well.
162
         */
163
        return $this->object->$property;
164
    }
165
166
    /**
167
     * Magic Isset Checker.
168
     *
169
     * @return bool
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 (isset($this->objectAliases[$property])) {
185
            $property = $this->objectAliases[$property];
186
        }
187
188
        $this->object->$property = $value;
189
    }
190
191
    /**
192
     * Handle dynamic method calls into the model.
193
     *
194
     * @param  string $method
195
     * @param  array $arguments
196
     *
197
     * @return mixed
198
     */
199
    public function __call($method, $arguments)
200
    {
201
        $query = $this->newQuery();
202
203
        return call_user_func_array([$query, $method], $arguments);
204
    }
205
206
    /**
207
     * Handle dynamic static method calls on the model class.
208
     *
209
     * Proxies calls to direct method calls on a new instance
210
     *
211
     * @param string $method
212
     * @param array $arguments
213
     *
214
     * @return mixed
215
     */
216
    public static function __callStatic($method, array $arguments)
217
    {
218
        return call_user_func_array([new static, $method], $arguments);
219
    }
220
}
221