Passed
Push — master ( 410349...4e2bae )
by litefeel
02:24
created

Writing_On_GitHub_Export::post_to_blob()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 3
nop 1
dl 0
loc 18
ccs 0
cts 10
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * GitHub Export Manager.
4
 *
5
 * @package Writing_On_GitHub
6
 */
7
8
/**
9
 * Class Writing_On_GitHub_Export
10
 */
11
class Writing_On_GitHub_Export {
12
13
    /**
14
     * Application container.
15
     *
16
     * @var Writing_On_GitHub
17
     */
18
    protected $app;
19
20
    /**
21
     * Initializes a new export 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
     * Updates all of the current posts in the database on master.
31
     *
32
     * @param  bool    $force
33
     *
34
     * @return string|WP_Error
35
     */
36
    public function full( $force = false ) {
37
        $posts = $this->app->database()->fetch_all_supported( $force );
38
39
        if ( is_wp_error( $posts ) ) {
40
            /* @var WP_Error $posts */
41
            return $posts;
42
        }
43
44
        $error = '';
45
46
        foreach ( $posts as $post ) {
47
            $result = $this->update( $post->id() );
48
            if ( is_wp_error( $result ) ) {
49
                /* @var WP_Error $result */
50
                $error = wogh_append_error( $error, $result );
51
            }
52
        }
53
54
        if ( is_wp_error( $error ) ) {
55
            /* @var WP_Error $error */
56
            return $error;
57
        }
58
59
        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
60
    }
61
62
63
    /**
64
     * Check if it exists in github
65
     * @param  int  $post_id
66
     * @return boolean
67
     */
68
    protected function github_path( $post_id ) {
69
        $github_path = get_post_meta( $post_id, '_wogh_github_path', true );
70
71
        if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) {
72
            return $github_path;
73
        }
74
75
        return false;
76
    }
77
78
    /**
79
     * Updates the provided post ID in master.
80
     *
81
     * @param int $post_id Post ID to update.
82
     *
83
     * @return string|WP_Error
84
     */
85
    public function update( $post_id ) {
86
        $post = $this->app->database()->fetch_by_id( $post_id );
87
88
        if ( is_wp_error( $post ) ) {
89
            /* @var WP_Error $post */
90
            return $post;
91
        }
92
93
        if ( 'trash' === $post->status() ) {
94
            return $this->delete( $post_id );
95
        }
96
97
        if ( $old_github_path = $this->github_path( $post->id() ) ) {
98
            error_log("old_github_path: $old_github_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...
99
            $post->set_old_github_path($old_github_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...
100
        }
101
102
        $result = $this->export_post( $post );
103
104
        if ( is_wp_error( $result ) ) {
105
            /* @var WP_Error $result */
106
            return $result;
107
        }
108
109
        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
110
    }
111
112
    /**
113
     * Post to blob
114
     * @param  Writing_On_GitHub_Post $post
115
     * @return WP_Error|Writing_On_GitHub_Blob
116
     */
117
    protected function post_to_blob( Writing_On_GitHub_Post $post ) {
118
        if ( ! $post->get_blob()
119
            && $post->old_github_path()
120
            && wogh_is_dont_export_content() ) {
121
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
122
123
            $blob = $this->app->api()->fetch()->blob_by_path( $post->old_github_path() );
124
125
            if ( is_wp_error( $blob ) ) {
126
                /** @var WP_Error $blob */
127
                return $blob;
128
            }
129
130
            $post->set_blob( $blob );
1 ignored issue
show
Bug introduced by
It seems like $blob defined by $this->app->api()->fetch...ost->old_github_path()) on line 123 can also be of type object<stdClass>; however, Writing_On_GitHub_Post::set_blob() does only seem to accept object<Writing_On_GitHub_Blob>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
131
        }
132
133
        return $post->to_blob();
134
    }
135
136
    /**
137
     * Export post to github
138
     * @param  Writing_On_GitHub_Post $post
139
     * @return WP_Error|true
0 ignored issues
show
Documentation introduced by
Should the return type not be WP_Error|stdClass|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...
140
     */
141
    public function export_post( Writing_On_GitHub_Post $post ) {
142
        // check blob
143
        $blob = $this->post_to_blob( $post );
144
        if ( is_wp_error( $blob ) ) {
145
            /** @var WP_Error $blob */
146
            return $blob;
147
        }
148
149
        $result = false;
150
151
        $persist = $this->app->api()->persist();
152
        $github_path = $post->github_path();
153
        $old_github_path = $post->old_github_path();
154
155
        if ( $old_github_path && $old_github_path != $github_path ) {
156
            // rename
157
            $message = apply_filters(
158
                'wogh_commit_msg_move_post',
159
                sprintf(
160
                    'Move %s to %s via WordPress at %s (%s)',
161
                    $old_github_path, $github_path,
162
                    site_url(),
163
                    get_bloginfo( 'name' )
164
                )
165
            ) . $this->get_commit_msg_tag();
166
167
            $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message );
168
            if ( is_wp_error( $result ) ) {
169
                return $result;
170
            }
171
172
            $result = $persist->create_file( $blob, $message );
173
            if ( is_wp_error( $result ) ) {
174
                return $result;
175
            }
176
        } elseif ( ! $old_github_path ) {
177
            // create new
178
            $message = apply_filters(
179
                'wogh_commit_msg_new_post',
180
                sprintf(
181
                    'Create new post %s from WordPress at %s (%s)',
182
                    $github_path,
183
                    site_url(),
184
                    get_bloginfo( 'name' )
185
                )
186
            ) . $this->get_commit_msg_tag();
187
            $result = $persist->create_file( $blob, $message );
188
            if ( is_wp_error( $result ) ) {
189
                return $result;
190
            }
191
        } elseif ( $old_github_path && $old_github_path == $github_path ) {
192
            // update
193
            $sha = wogh_git_sha( $blob->content() );
194
            if ( $sha === $blob->sha() ) {
195
                // don't export when has not changed
196
                return true;
197
            }
198
            $message = apply_filters(
199
                'wogh_commit_msg_update_post',
200
                sprintf(
201
                    'Update post %s from WordPress at %s (%s)',
202
                    $github_path,
203
                    site_url(),
204
                    get_bloginfo( 'name' )
205
                )
206
            ) . $this->get_commit_msg_tag();
207
            $result = $persist->update_file( $blob, $message );
208
            if ( is_wp_error( $result ) ) {
209
                return $result;
210
            }
211
        }
212
213
        $sha = $result->content->sha;
214
        $post->set_sha( $sha );
215
        $post->set_old_github_path( $github_path );
216
217
        return true;
218
    }
219
220
    /**
221
     * Deletes a provided post ID from master.
222
     *
223
     * @param int $post_id Post ID to delete.
224
     *
225
     * @return string|WP_Error
226
     */
227
    public function delete( $post_id ) {
228
        $post = $this->app->database()->fetch_by_id( $post_id );
229
230
        if ( is_wp_error( $post ) ) {
231
            /* @var WP_Error $post */
232
            return $post;
233
        }
234
235
        $github_path = get_post_meta( $post_id, '_wogh_github_path', true );
236
237
        $message = apply_filters(
238
            'wogh_commit_msg_delete',
239
            sprintf(
240
                'Deleting %s via WordPress at %s (%s)',
241
                $github_path,
242
                site_url(),
243
                get_bloginfo( 'name' )
244
            ),
245
            $post
246
        ) . $this->get_commit_msg_tag();
247
248
        $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message );
249
250
        if ( is_wp_error( $result ) ) {
251
            /* @var WP_Error $result */
252
            return $result;
253
        }
254
255
        return __( 'Export to GitHub completed successfully.', 'writing-on-github' );
256
    }
257
258
259
    /**
260
     * Saves the export user to the database.
261
     *
262
     * @param int $user_id User ID to export with.
263
     *
264
     * @return bool
265
     */
266
    public function set_user( $user_id ) {
267
        return update_option( self::EXPORT_USER_OPTION, (int) $user_id );
268
    }
269
270
    /**
271
     * Gets the commit message tag.
272
     *
273
     * @return string
274
     */
275
    protected function get_commit_msg_tag() {
276
        $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' );
277
278
        if ( ! $tag ) {
279
            throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) );
280
        }
281
282
        return ' - ' . $tag;
283
    }
284
}
285