Test Setup Failed
Push — ci ( fdc038...cdcc0c )
by litefeel
01:55
created

Writing_On_GitHub_Import::delete_post()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * GitHub Import Manager
4
 *
5
 * @package Writing_On_GitHub
6
 */
7
8
/**
9
 * Class Writing_On_GitHub_Import
10
 */
11
class Writing_On_GitHub_Import {
12
13
    /**
14
     * Application container.
15
     *
16
     * @var Writing_On_GitHub
17
     */
18
    protected $app;
19
20
    /**
21
     * Initializes a new import manager.
22
     *
23
     * @param Writing_On_GitHub $app Application container.
24
     */
25
    public function __construct( Writing_On_GitHub $app ) {
26
        $this->app = $app;
27
    }
28
29
    /**
30
     * Imports a payload.
31
     * @param  Writing_On_GitHub_Payload $payload
32
     *
33
     * @return string|WP_Error
34
     */
35 View Code Duplication
    public function payload( Writing_On_GitHub_Payload $payload ) {
0 ignored issues
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...
36
37
        $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() );
38
39
        if ( is_wp_error( $result ) ) {
40
            /* @var WP_Error $result */
41
            return $result;
42
        }
43
44
        if ( is_array( $result ) ) {
45
            $result = $this->import_files( $result );
46
        }
47
48
        if ( is_wp_error( $result ) ) {
49
            return $files;
50
        }
51
52
        return __( 'Payload processed', 'writing-on-github' );
53
    }
54
55
    /**
56
     * import blob by files
57
     * @param  Writing_On_GitHub_File_Info[] $files
58
     *
59
     * @return true|WP_Error
0 ignored issues
show
Documentation introduced by
Should the return type not be WP_Error|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
60
     */
61
    protected function import_files( $files ) {
62
63
        $error = true;
64
65
        foreach ( $files as $file ) {
66
            if ( ! $this->importable_file( $file ) ) {
67
                continue;
68
            }
69
70
            $blob = $this->app->api()->fetch()->blob( $file );
71
            // network error ?
72
            if ( ! $blob instanceof Writing_On_GitHub_Blob ) {
73
                continue;
74
            }
75
76
            $is_remove = 'removed' == $file->status;
77
78
            $result = false;
79
            if ( $this->importable_raw_file( $blob ) ) {
80
                $result = $this->import_raw_file( $blob, $is_remove );
81
            } elseif ( $this->importable_post( $blob ) ) {
82
                if ( $is_remove ) {
83
                    $result = $this->delete_post( $blob );
84
                } else {
85
                    $result = $this->import_post( $blob );
86
                }
87
            }
88
89
            if ( is_wp_error( $result ) ) {
90
                /* @var WP_Error $result */
91
                $error = wogh_append_error( $error, $result );
92
            }
93
        }
94
95
        return $error;
96
    }
97
98
    /**
99
     * Imports the latest commit on the master branch.
100
     *
101
     * @return string|WP_Error
102
     */
103 View Code Duplication
    public function master() {
0 ignored issues
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...
104
        $result = $this->app->api()->fetch()->tree_recursive();
105
106
        if ( is_wp_error( $result ) ) {
107
            /* @var WP_Error $result */
108
            return $result;
109
        }
110
111
        if ( is_array( $result ) ) {
112
            $result = $this->import_files( $result );
113
        }
114
115
        if ( is_wp_error( $result ) ) {
116
            /* @var WP_Error $result */
117
            return $result;
118
        }
119
120
        return __( 'Payload processed', 'writing-on-github' );
121
    }
122
123
    /**
124
     * Checks whether the provided blob should be imported.
125
     *
126
     * @param Writing_On_GitHub_File_Info $file
127
     *
128
     * @return bool
129
     */
130
    protected function importable_file( Writing_On_GitHub_File_Info $file ) {
131
132
        $path = $file->path;
133
134
        // only _pages, _posts and images
135
        $prefixs = array( '_pages/', '_posts/', 'images/');
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
136
        foreach ($prefixs as $prefix) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
137
            if ( ! strncasecmp($path, $prefix, strlen( $prefix ) ) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
138
                return true;
139
            }
140
        }
141
        return false;
142
    }
143
144
    /**
145
     * Checks whether the provided blob should be imported.
146
     *
147
     * @param Writing_On_GitHub_Blob $blob Blob to validate.
148
     *
149
     * @return bool
150
     */
151
    protected function importable_post( Writing_On_GitHub_Blob $blob ) {
152
        // global $wpdb;
153
154
        // // Skip the repo's readme.
155
        // if ( 'readme' === strtolower( substr( $blob->path(), 0, 6 ) ) ) {
156
        //  return false;
157
        // }
158
159
        // // If the blob sha already matches a post, then move on.
160
        // if ( ! is_wp_error( $this->app->database()->fetch_by_sha( $blob->sha() ) ) ) {
161
        //  return false;
162
        // }
163
164
        if ( ! $blob->has_frontmatter() ) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $blob->has_frontmatter();.
Loading history...
165
            return false;
166
        }
167
168
        return true;
169
    }
170
171
    /**
172
     * Delete post
173
     * @param  Writing_On_GitHub_Blob $blob
174
     * @return WP_Error|bool
175
     */
176
    protected function delete_post( Writing_On_GitHub_Blob $blob ) {
177
        $id = $blob->id();
178
        if ( empty( $id ) ) {
179
            return false;
180
        }
181
        $result = $this->app->database()->delete_post( $id );
182
        if ( is_wp_error( $result ) ) {
183
            /* @var WP_Error $result */
184
            return $result;
185
        }
186
        return true;
187
    }
188
189
    /**
190
     * Imports a post into wordpress
191
     * @param  Writing_On_GitHub_Blob $blob
192
     * @return WP_Error|bool
193
     */
194
    protected function import_post( Writing_On_GitHub_Blob $blob ) {
195
        $post = $this->blob_to_post( $blob );
196
197
        if ( ! $post instanceof Writing_On_GitHub_Post ) {
198
            return false;
199
        }
200
201
        $result = $this->app->database()->save_post( $post );
202
        if ( is_wp_error( $result ) ) {
203
            /** @var WP_Error $result */
204
            return $result;
205
        }
206
207
        if ( $post->is_new() ||
208
                ! wogh_equal_front_matter( $post, $blob ) ) {
209
210
            $result = $this->app->export()->new_posts( array( $post ) );
211
212
            if ( is_wp_error( $result ) ) {
213
                /** @var WP_Error $result */
214
                return $result;
215
            }
216
        }
217
218
        clean_post_cache( $post->id() );
219
220
        return true;
221
    }
222
223
    /**
224
     * import raw file
225
     * @param  Writing_On_GitHub_Blob $blob
226
     * @return bool
227
     */
228
    protected function importable_raw_file( Writing_On_GitHub_Blob $blob ) {
229
        if ( $blob->has_frontmatter() ) {
230
            return false;
231
        }
232
233
        // only images
234
        if ( strncasecmp($blob->path(), 'images/', strlen('images/') ) != 0) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(strncasecmp($bl...rlen('images/')) != 0);.
Loading history...
introduced by
Found "!= 0". Use Yoda Condition checks, you must
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
235
            return false;
236
        }
237
238
        return true;
239
    }
240
241
    /**
242
     * Imports a raw file content into file system.
243
     * @param  Writing_On_GitHub_Blob $blob
244
     * @param  bool                   $is_remove
245
     */
246
    protected function import_raw_file( Writing_On_GitHub_Blob $blob, $is_remove ) {
247
        $arr = wp_upload_dir();
248
        $path = $arr['basedir'] . '/writing-on-github/' . $blob->path();
249
        if ( $is_remove ) {
250
            if ( file_exists($path) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
251
                unlink($path);
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using unlink()
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
252
            }
253
        } else {
254
            $dirname = dirname($path);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
255
            if ( ! file_exists($dirname) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
256
                wp_mkdir_p($dirname);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
257
            }
258
259
            file_put_contents($path, $blob->content());
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using file_put_contents()
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
260
        }
261
        return true;
262
    }
263
264
    /**
265
     * Imports a single blob content into matching post.
266
     *
267
     * @param Writing_On_GitHub_Blob $blob Blob to transform into a Post.
268
     *
269
     * @return Writing_On_GitHub_Post|false
270
     */
271
    protected function blob_to_post( Writing_On_GitHub_Blob $blob ) {
0 ignored issues
show
Complexity introduced by
This operation has 582 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

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

Loading history...
272
        $args = array( 'post_content' => $blob->content_import() );
273
        $meta = $blob->meta();
274
275
        $id = false;
276
277
        if ( ! empty( $meta ) ) {
278
            if ( array_key_exists( 'layout', $meta ) ) {
279
                $args['post_type'] = $meta['layout'];
280
                unset( $meta['layout'] );
281
            }
282
283
            if ( array_key_exists( 'published', $meta ) ) {
284
                $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft';
285
                unset( $meta['published'] );
286
            }
287
288
            if ( array_key_exists( 'post_title', $meta ) ) {
289
                $args['post_title'] = $meta['post_title'];
290
                unset( $meta['post_title'] );
291
            }
292
293
            if ( array_key_exists( 'post_name', $meta ) ) {
294
                $args['post_name'] = $meta['post_name'];
295
                unset( $meta['post_name'] );
296
            }
297
298
            if ( array_key_exists( 'ID', $meta ) ) {
299
                $id = $args['ID'] = $meta['ID'];
300
                $blob->set_id($id);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
301
                unset( $meta['ID'] );
302
            }
303
        }
304
305
        $meta['_wogh_sha'] = $blob->sha();
306
307
        if ( $id ) {
308
            $old_sha = get_post_meta( $id, '_wogh_sha', true );
309
            $old_github_path = get_post_meta( $id, '_wogh_github_path', true );
310
311
            // dont save post when has same sha
312
            if ( $old_sha  && $old_sha == $meta['_wogh_sha'] &&
313
                 $old_github_path && $old_github_path == $blob->path() ) {
314
                return false;
315
            }
316
        }
317
318
        $post = new Writing_On_GitHub_Post( $args, $this->app->api() );
319
        $post->set_old_github_path( $blob->path() );
320
        $post->set_meta( $meta );
321
        $blob->set_id( $post->id() );
322
323
        return $post;
324
    }
325
}
326