Completed
Push — master ( 38ffd4...ed5eea )
by Evan
03:02
created

Model::meta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 10
loc 10
rs 9.4285

1 Method

Rating   Name   Duplication   Size   Complexity  
A Model::taxonomy() 0 4 1
1
<?php
2
3
namespace Silk\Term;
4
5
use stdClass;
6
use WP_Term;
7
use Silk\Meta\TypeMeta;
8
use Silk\Taxonomy\Taxonomy;
9
use Silk\Query\QueryBuilder;
10
use Illuminate\Support\Collection;
11
use Silk\Exception\WP_ErrorException;
12
use Silk\Term\Exception\TermNotFoundException;
13
use Silk\Term\Exception\TaxonomyMismatchException;
14
15
/**
16
 * @property-read int $id
17
 * @property int $term_id
18
 * @property string $name
19
 * @property string $slug
20
 * @property string $term_group
21
 * @property int $term_taxonomy_id
22
 * @property string $taxonomy
23
 * @property string $description
24
 * @property int $parent
25
 * @property int $count
26
 */
27
abstract class Model
28
{
29
    /**
30
     * The term's taxonomy
31
     * @var string
32
     */
33
    const TAXONOMY = '';
34
35
    /**
36
     * The object type in WordPress
37
     * @var string
38
     */
39
    const OBJECT_TYPE = 'term';
40
41
    /**
42
     * The term object
43
     * @var WP_Term
44
     */
45
    protected $term;
46
47
    use TypeMeta;
48
    use QueryBuilder;
49
50
    /**
51
     * Model Constructor.
52
     *
53
     * @param mixed $term  WP_Term to fill data from
54
     */
55
    public function __construct(WP_Term $term = null)
56
    {
57
        if (! $term) {
58
            $term = new WP_Term(new stdClass);
59
            $term->taxonomy = static::TAXONOMY;
60
        } elseif ($term->taxonomy != static::TAXONOMY) {
61
            throw new TaxonomyMismatchException();
62
        }
63
64
        $this->term = $term;
65
    }
66
67
    /**
68
     * Create a new instance from a WP_Term object.
69
     *
70
     * @param  WP_Term $term [description]
71
     *
72
     * @return static
73
     */
74
    public static function fromWpTerm(WP_Term $term)
75
    {
76
        return new static($term);
77
    }
78
79
    /**
80
     * Create a new instance from a term ID.
81
     *
82
     * @param  int|string $id  Term ID
83
     *
84
     * @return static
85
     */
86
    public static function fromID($id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        if (! $term = get_term_by('id', (int) $id, static::TAXONOMY)) {
89
            throw new TermNotFoundException("No term found with ID $id.");
90
        }
91
92
        return static::fromWpTerm($term);
93
    }
94
95
    /**
96
     * Create a new instance from a slug.
97
     *
98
     * @param  string $slug  Term slug
99
     *
100
     * @return static
101
     */
102
    public static function fromSlug($slug)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
    {
104
        if (! $term = get_term_by('slug', $slug, static::TAXONOMY)) {
105
            throw new TermNotFoundException("No term found with slug '$slug'.");
106
        }
107
108
        return static::fromWpTerm($term);
109
    }
110
111
    /**
112
     * Create a new instance from an array of attributes.
113
     *
114
     * @param  array  $attributes [description]
115
     *
116
     * @return static
117
     */
118
    public static function fromArray(array $attributes)
119
    {
120
        return new static(
121
            new WP_Term((object) $attributes)
122
        );
123
    }
124
125
    /**
126
     * Create a new term, and get the instance for it.
127
     *
128
     * @param  array $attributes  Term attributes
129
     *
130
     * @return static
131
     */
132
    public static function create(array $attributes = [])
133
    {
134
        return static::fromArray(
135
            Collection::make($attributes)
136
                ->except(['term_id', 'term_taxonomy_id'])
137
                ->put('taxonomy', static::TAXONOMY)
138
                ->toArray()
139
        )->save();
140
    }
141
142
    /**
143
     * Save or update the term instance in the database.
144
     *
145
     * @return $this
146
     */
147
    public function save()
148
    {
149
        if ($this->exists()) {
150
            $ids = wp_update_term($this->id, static::TAXONOMY, $this->term->to_array());
151
        } else {
152
            $ids = wp_insert_term($this->name, static::TAXONOMY, $this->term->to_array());
153
        }
154
155
        if (is_wp_error($ids)) {
156
            throw new WP_ErrorException($ids);
157
        }
158
159
        foreach ($ids as $field => $id) {
160
            $this->term->$field = $id;
161
        }
162
163
        return $this;
164
    }
165
166
    /**
167
     * Delete the term from the database.
168
     *
169
     * @return $this
170
     */
171
    public function delete()
172
    {
173
        if ($result = wp_delete_term($this->id, static::TAXONOMY)) {
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
174
            $this->term->term_id = null;
175
            $this->term->term_taxonomy_id = 0;
176
        }
177
178
        return $this;
179
    }
180
181
    /**
182
     * Check if this term exists in the database.
183
     *
184
     * @return boolean
185
     */
186
    public function exists()
187
    {
188
        return (bool) term_exists((int) $this->id, static::TAXONOMY);
189
    }
190
191
    /**
192
     * Check if this term exists in the database as the child of the given parent.
193
     *
194
     * @param  int|string|object  $parent  integer Parent term ID
195
     *                                     string  Parent term slug or name
196
     *                                     object  The parent term object/model.
197
     *
198
     * @return boolean                     True if the this term and the parent
199
     *                                     exist in the database, and the instance
200
     *                                     is a child of the given parent;
201
     *                                     otherwise false
202
     */
203
    public function isChildOf($parent)
204
    {
205
        if (isset($parent->term_id)) {
206
            $parent = $parent->term_id;
207
        }
208
209
        return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent);
210
    }
211
212
    /**
213
     * Get the parent term instance.
214
     *
215
     * @return static
216
     */
217
    public function parent()
218
    {
219
        return static::fromID($this->term->parent);
220
    }
221
222
    /**
223
     * Get all ancestors of this term as a collection.
224
     *
225
     * @return Collection
226
     */
227
    public function ancestors()
228
    {
229
        return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy'))
230
            ->map(function ($term_ID) {
231
                return static::fromID($term_ID);
232
            });
233
    }
234
235
    /**
236
     * Get the Taxonomy model.
237
     *
238
     * @return Taxonomy
239
     */
240
    public function taxonomy()
241
    {
242
        return Taxonomy::make($this->taxonomy);
243
    }
244
245
    /**
246
     * Start a new query for terms of this type.
247
     *
248
     * @return TermQueryBuilder
249
     */
250
    public function newQuery()
251
    {
252
        return (new TermQueryBuilder)->setModel($this);
253
    }
254
255
    /**
256
     * Magic Getter.
257
     *
258
     * @param  string $property Property name accessed
259
     *
260
     * @return mixed
261
     */
262
    public function __get($property)
263
    {
264
        if ('id' == strtolower($property)) {
265
            return $this->term->term_id;
266
        }
267
268
        if (isset($this->term->$property)) {
269
            return $this->term->$property;
270
        }
271
272
        return null;
273
    }
274
275
    /**
276
     * Magic set checker.
277
     *
278
     * @param  string  $property  Property name queried
279
     *
280
     * @return boolean
281
     */
282
    public function __isset($property)
283
    {
284
        return property_exists($this->term, $property);
285
    }
286
287
    /**
288
     * Magic Setter.
289
     *
290
     * @param string $property  Property name assigned
291
     * @param mixed  $value     Assigned property value
292
     */
293
    public function __set($property, $value)
294
    {
295
        $this->term->$property = $value;
296
    }
297
}
298