Completed
Push — develop ( 2a5993...441247 )
by Evan
02:20
created

Model   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 241
Duplicated Lines 6.64 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 18
Bugs 0 Features 6
Metric Value
c 18
b 0
f 6
dl 16
loc 241
rs 10
wmc 25
lcom 1
cbo 7

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A fromWpTerm() 0 4 1
A fromArray() 0 6 1
A fromID() 8 8 2
A fromSlug() 8 8 2
A exists() 0 4 2
A isChildOf() 0 8 2
A parent() 0 4 1
A ancestors() 0 7 1
A taxonomy() 0 4 1
A url() 0 10 2
A newQuery() 0 4 1
A save() 0 16 3
A delete() 0 8 2
A refresh() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Silk\Term;
4
5
use stdClass;
6
use WP_Term;
7
use Silk\Taxonomy\Taxonomy;
8
use Silk\Type\Model as BaseModel;
9
use Illuminate\Support\Collection;
10
use Silk\Exception\WP_ErrorException;
11
use Silk\Term\Exception\TermNotFoundException;
12
use Silk\Term\Exception\TaxonomyMismatchException;
13
14
/**
15
 * @property-read WP_Term $term
16
 * @property int    $term_id
17
 * @property string $name
18
 * @property string $slug
19
 * @property string $term_group
20
 * @property int    $term_taxonomy_id
21
 * @property string $taxonomy
22
 * @property string $description
23
 * @property int    $parent
24
 * @property int    $count
25
 */
26
abstract class Model extends BaseModel
27
{
28
    /**
29
     * The term's taxonomy
30
     * @var string
31
     */
32
    const TAXONOMY = '';
33
34
    /**
35
     * The object type in WordPress
36
     * @var string
37
     */
38
    const OBJECT_TYPE = 'term';
39
40
    /**
41
     * The primary ID property on the object
42
     */
43
    const ID_PROPERTY = 'term_id';
44
45
    /**
46
     * Model Constructor.
47
     *
48
     * @param mixed $term  WP_Term to fill data from
49
     *
50
     * @throws TaxonomyMismatchException
51
     */
52
    public function __construct(WP_Term $term = null)
53
    {
54
        if (! $term) {
55
            $term = new WP_Term(new stdClass);
56
            $term->taxonomy = static::TAXONOMY;
57
        } elseif ($term->taxonomy != static::TAXONOMY) {
58
            throw new TaxonomyMismatchException();
59
        }
60
61
        $this->object = $term;
62
    }
63
64
    /**
65
     * Create a new instance from a WP_Term object.
66
     * @deprecated - use static::make()
67
     *
68
     * @param  WP_Term $term [description]
69
     *
70
     * @return static
71
     */
72
    public static function fromWpTerm(WP_Term $term)
73
    {
74
        return new static($term);
75
    }
76
77
    /**
78
     * Create a new instance from a term ID.
79
     *
80
     * @param  int|string $id  Term ID
81
     *
82
     * @throws TermNotFoundException
83
     *
84
     * @return static
85
     */
86 View Code Duplication
    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 new static($term);
93
    }
94
95
    /**
96
     * Create a new instance from a slug.
97
     *
98
     * @param  string $slug  Term slug
99
     *
100
     * @throws TermNotFoundException
101
     *
102
     * @return static
103
     */
104 View Code Duplication
    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...
105
    {
106
        if (! $term = get_term_by('slug', $slug, static::TAXONOMY)) {
107
            throw new TermNotFoundException("No term found with slug '$slug'.");
108
        }
109
110
        return new static($term);
111
    }
112
113
    /**
114
     * Create a new instance from an array of attributes.
115
     *
116
     * @param  array  $attributes [description]
117
     *
118
     * @return static
119
     */
120
    public static function fromArray(array $attributes)
121
    {
122
        return new static(
123
            new WP_Term((object) $attributes)
124
        );
125
    }
126
127
    /**
128
     * Check if this term exists in the database.
129
     *
130
     * @return boolean
131
     */
132
    public function exists()
133
    {
134
        return $this->id && ((bool) term_exists((int) $this->id, static::TAXONOMY));
135
    }
136
137
    /**
138
     * Check if this term exists in the database as the child of the given parent.
139
     *
140
     * @param  int|string|object  $parent  integer Parent term ID
141
     *                                     string  Parent term slug or name
142
     *                                     object  The parent term object/model.
143
     *
144
     * @return boolean                     True if the this term and the parent
145
     *                                     exist in the database, and the instance
146
     *                                     is a child of the given parent;
147
     *                                     otherwise false
148
     */
149
    public function isChildOf($parent)
150
    {
151
        if (isset($parent->term_id)) {
152
            $parent = $parent->term_id;
153
        }
154
155
        return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent);
156
    }
157
158
    /**
159
     * Get the parent term instance.
160
     *
161
     * @return static
162
     */
163
    public function parent()
164
    {
165
        return static::fromID($this->object->parent);
166
    }
167
168
    /**
169
     * Get all ancestors of this term as a collection.
170
     *
171
     * @return Collection
172
     */
173
    public function ancestors()
174
    {
175
        return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy'))
176
            ->map(function ($term_ID) {
177
                return static::fromID($term_ID);
178
            });
179
    }
180
181
    /**
182
     * Get the Taxonomy model.
183
     *
184
     * @return Taxonomy|\Silk\Taxonomy\Builder
185
     */
186
    public static function taxonomy()
187
    {
188
        return Taxonomy::make(static::TAXONOMY);
189
    }
190
191
    /**
192
     * Get the URL for this term.
193
     *
194
     * @return string|bool
195
     */
196
    public function url()
197
    {
198
        $url = get_term_link($this->id, $this->taxonomy);
199
200
        if (is_wp_error($url)) {
201
            throw new WP_ErrorException($url);
202
        }
203
204
        return $url;
205
    }
206
207
    /**
208
     * Start a new query for terms of this type.
209
     *
210
     * @return QueryBuilder
211
     */
212
    public function newQuery()
213
    {
214
        return QueryBuilder::make()->setModel($this);
215
    }
216
217
    /**
218
     * Save the term to the database.
219
     *
220
     * @throws WP_ErrorException
221
     *
222
     * @return $this
223
     */
224
    public function save()
225
    {
226
        if ($this->id) {
227
            $ids = wp_update_term($this->id, $this->taxonomy, $this->object->to_array());
228
        } else {
229
            $ids = wp_insert_term($this->name, $this->taxonomy, $this->object->to_array());
230
        }
231
232
        if (is_wp_error($ids)) {
233
            throw new WP_ErrorException($ids);
234
        }
235
236
        $this->setId($ids['term_id'])->refresh();
237
238
        return $this;
239
    }
240
241
    /**
242
     * Delete the term from the database.
243
     *
244
     * @return $this
245
     */
246
    public function delete()
247
    {
248
        if (wp_delete_term($this->id, $this->taxonomy)) {
249
            $this->object = new WP_Term(new stdClass);
250
        }
251
252
        return $this;
253
    }
254
255
    /**
256
     * Reload the term object from the database.
257
     *
258
     * @return $this
259
     */
260
    public function refresh()
261
    {
262
        $this->object = WP_Term::get_instance($this->id, $this->taxonomy);
263
264
        return $this;
265
    }
266
}
267