Completed
Pull Request — master (#23)
by Evan
02:46
created

Model   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 228
Duplicated Lines 7.02 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 12
Bugs 0 Features 3
Metric Value
c 12
b 0
f 3
dl 16
loc 228
rs 10
wmc 25
lcom 2
cbo 6

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A fromWpPost() 0 8 2
A fromID() 0 10 2
A fromSlug() 0 10 2
A fromGlobal() 0 8 2
A postTypeId() 0 4 1
A postType() 0 4 1
A url() 0 4 1
A trash() 0 8 2
A untrash() 0 8 2
A newQuery() 0 4 1
A delete() 0 8 2
A refresh() 0 6 1
A save() 16 16 3

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\Post;
4
5
use stdClass;
6
use WP_Post;
7
use Silk\Type\Model as BaseModel;
8
use Silk\PostType\PostType;
9
use Silk\Exception\WP_ErrorException;
10
use Silk\Post\Exception\PostNotFoundException;
11
use Silk\Post\Exception\ModelPostTypeMismatchException;
12
13
/**
14
 * @property-read WP_Post $post
15
 * @property-read int     $id
16
 *
17
 * @property int    $ID
18
 * @property int    $comment_count
19
 * @property string $comment_status
20
 * @property string $filter
21
 * @property string $guid
22
 * @property int    $menu_order
23
 * @property string $ping_status
24
 * @property string $pinged
25
 * @property int    $post_author
26
 * @property string $post_content
27
 * @property string $post_content_filtered
28
 * @property string $post_date
29
 * @property string $post_date_gmt
30
 * @property string $post_excerpt
31
 * @property string $post_mime_type
32
 * @property string $post_modified
33
 * @property string $post_modified_gmt
34
 * @property string $post_name
35
 * @property int    $post_parent
36
 * @property string $post_password
37
 * @property string $post_status
38
 * @property string $post_title
39
 * @property string $post_type
40
 * @property string $to_ping
41
 */
42
abstract class Model extends BaseModel
43
{
44
    /**
45
     * The post type of the post this model wraps
46
     * @var string
47
     */
48
    const POST_TYPE = '';
49
50
    /**
51
     * The object type in WordPress
52
     * @var string
53
     */
54
    const OBJECT_TYPE = 'post';
55
56
    /**
57
     * The primary ID property on the object
58
     */
59
    const ID_PROPERTY = 'ID';
60
61
    /**
62
     * Create a new instance
63
     *
64
     * @param WP_Post $post  Post object to model
65
     *
66
     * @throws ModelPostTypeMismatchException
67
     */
68
    public function __construct(WP_Post $post = null)
69
    {
70
        if (! $post) {
71
            $post = new WP_Post(new stdClass);
72
            $post->post_type = static::postTypeId();
73
        } elseif ($post->post_type !== static::postTypeId()) {
74
            throw new ModelPostTypeMismatchException(static::class, $post);
75
        }
76
77
        $this->object = $post;
78
    }
79
80
    /**
81
     * Create a new instance from the given WP_Post object
82
     * @deprecated - use static::make()
83
     *
84
     * @param  WP_Post $post
85
     *
86
     * @return static
87
     */
88
    public static function fromWpPost(WP_Post $post)
89
    {
90
        if ($post->post_type !== static::postTypeId()) {
91
            throw new ModelPostTypeMismatchException(static::class, $post);
92
        }
93
94
        return new static($post);
95
    }
96
97
    /**
98
     * Create a new instance from a Post with the given ID
99
     *
100
     * @param  int|string $id  Post ID of post to create the instance from
101
     *
102
     * @return static
103
     */
104
    public static function fromID($id)
105
    {
106
        $post = WP_Post::get_instance($id);
107
108
        if (false === $post) {
109
            throw new PostNotFoundException("No post found with ID {$id}");
110
        }
111
112
        return new static($post);
113
    }
114
115
    /**
116
     * Create a new instance from a Post with the given slug
117
     *
118
     * @param  string $slug  the post slug
119
     *
120
     * @return static
121
     */
122
    public static function fromSlug($slug)
123
    {
124
        $found = static::whereSlug($slug)->limit(1)->results();
125
126
        if ($found->isEmpty()) {
127
            throw new PostNotFoundException("No post found with slug {$slug}");
128
        }
129
130
        return $found->first();
131
    }
132
133
    /**
134
     * Create a new instance from the global $post
135
     *
136
     * @return static
137
     */
138
    public static function fromGlobal()
139
    {
140
        if (! $GLOBALS['post'] instanceof WP_Post) {
141
            throw new PostNotFoundException('Global $post not an instance of WP_Post');
142
        }
143
144
        return new static($GLOBALS['post']);
145
    }
146
147
    /**
148
     * Get the post type identifier for this model
149
     *
150
     * @return string post type identifier (slug)
151
     */
152
    public static function postTypeId()
153
    {
154
        return static::POST_TYPE;
155
    }
156
157
    /**
158
     * Get the post type API
159
     *
160
     * @return mixed        Loads an existing type as a new PostType,
161
     *                      or returns a new PostTypeBuilder for registering a new type.
162
     */
163
    public static function postType()
164
    {
165
        return PostType::make(static::postTypeId());
166
    }
167
168
    /**
169
     * Get the permalink URL.
170
     *
171
     * @return string|bool  The permalink URL, or false if the post does not exist.
172
     */
173
    public function url()
174
    {
175
        return get_permalink($this->id);
176
    }
177
178
    /**
179
     * Send the post to the trash
180
     *
181
     * If trash is disabled, the post or page is permanently deleted.
182
     *
183
     * @return $this
184
     */
185
    public function trash()
186
    {
187
        if (wp_trash_post($this->id)) {
188
            $this->refresh();
189
        }
190
191
        return $this;
192
    }
193
194
    /**
195
     * Restore a post or page from the Trash
196
     *
197
     * @return $this
198
     */
199
    public function untrash()
200
    {
201
        if (wp_untrash_post($this->id)) {
202
            $this->refresh();
203
        }
204
205
        return $this;
206
    }
207
208
    /**
209
     * Get a new query builder for the model.
210
     *
211
     * @return QueryBuilder
212
     */
213
    public function newQuery()
214
    {
215
        return QueryBuilder::make()->setModel($this);
216
    }
217
218
    /**
219
     * Save the post to the database.
220
     *
221
     * @throws WP_ErrorException
222
     *
223
     * @return $this
224
     */
225 View Code Duplication
    public function save()
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...
226
    {
227
        if (! $this->id) {
228
            $result = wp_insert_post($this->object->to_array(), true);
229
        } else {
230
            $result = wp_update_post($this->object, true);
231
        }
232
233
        if (is_wp_error($result)) {
234
            throw new WP_ErrorException($result);
235
        }
236
237
        $this->setId($result)->refresh();
238
239
        return $this;
240
    }
241
242
    /**
243
     * Permanently delete the post from the database.
244
     *
245
     * @return $this
246
     */
247
    public function delete()
248
    {
249
        if (wp_delete_post($this->id, true)) {
250
            $this->refresh();
251
        }
252
253
        return $this;
254
    }
255
256
    /**
257
     * Update the modeled object with the current state from the database.
258
     *
259
     * @return $this
260
     */
261
    public function refresh()
262
    {
263
        $this->object = WP_Post::get_instance($this->id);
264
265
        return $this;
266
    }
267
268
269
}
270