Passed
Push — master ( 7e761f...6aefea )
by litefeel
03:13
created

Writing_On_GitHub_Controller::pull_posts()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 52
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 29
nc 7
nop 0
dl 0
loc 52
ccs 0
cts 32
cp 0
crap 56
rs 7.2396
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Controller object manages tree retrieval, manipulation and publishing
4
 * @package Writing_On_GitHub
5
 */
6
7
/**
8
 * Class Writing_On_GitHub_Controller
9
 */
10
class Writing_On_GitHub_Controller {
11
12
    /**
13
     * Application container.
14
     *
15
     * @var Writing_On_GitHub
16
     */
17
    public $app;
18
19
    /**
20
     * Instantiates a new Controller object
21
     *
22
     * @param Writing_On_GitHub $app Applicatio container.
23
     */
24
    public function __construct( Writing_On_GitHub $app ) {
25
        $this->app = $app;
26
    }
27
28
    /**
29
     * Webhook callback as triggered from GitHub push.
30
     *
31
     * Reads the Webhook payload and syncs posts as necessary.
32
     *
33
     * @return boolean
34
     */
35
    public function pull_posts() {
36
        $this->set_ajax();
37
        if ( ! $this->app->semaphore()->is_open() ) {
38
            return $this->app->response()->error( new WP_Error(
39
                'semaphore_locked',
40
                sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' )
41
            ) );
42
        }
43
44
        if ( ! $this->app->request()->is_secret_valid() ) {
45
            return $this->app->response()->error( new WP_Error(
46
                'invalid_headers',
47
                __( 'Failed to validate secret.', 'writing-on-github' )
48
            ) );
49
        }
50
51
        // ping
52
        if ( $this->app->request()->is_ping() ) {
53
            return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) );
54
        }
55
56
        // push
57
        if ( ! $this->app->request()->is_push() ) {
58
            return $this->app->response()->error( new WP_Error(
59
                'invalid_headers',
60
                sprintf( 'Failed to validate webhook event: %s.',
61
                    $this->app->request()->webhook_event() )
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 20.
Loading history...
62
            ) );
63
        }
64
        $payload = $this->app->request()->payload();
65
66
        $error = $payload->should_import();
67
        if ( is_wp_error( $error ) ) {
68
            /* @var WP_Error $error */
69
            return $this->app->response()->error( $error );
70
        }
71
72
        $this->app->semaphore()->lock();
73
        remove_action( 'save_post', array( $this, 'export_post' ) );
74
        remove_action( 'delete_post', array( $this, 'delete_post' ) );
75
76
        $result = $this->app->import()->payload( $payload );
77
78
        $this->app->semaphore()->unlock();
79
80
        if ( is_wp_error( $result ) ) {
81
            /* @var WP_Error $result */
82
            return $this->app->response()->error( $result );
83
        }
84
85
        return $this->app->response()->success( $result );
86
    }
87
88
    /**
89
     * Imports posts from the current master branch.
90
     * @param  integer $user_id
91
     * @param  boolean $force
92
     * @return boolean
93
     */
94
    public function import_master( $user_id = 0, $force = false ) {
95
        if ( ! $this->app->semaphore()->is_open() ) {
96
            return $this->app->response()->error( new WP_Error(
97
                'semaphore_locked',
98
                sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' )
99
            ) );
100
        }
101
102
        $this->app->semaphore()->lock();
103
        remove_action( 'save_post', array( $this, 'export_post' ) );
104
        remove_action( 'save_post', array( $this, 'delete_post' ) );
105
106
        if ( $user_id ) {
107
            wp_set_current_user( $user_id );
108
        }
109
110
        $result = $this->app->import()->master( $force );
111
112
        $this->app->semaphore()->unlock();
113
114
        if ( is_wp_error( $result ) ) {
115
            /* @var WP_Error $result */
116
            update_option( '_wogh_import_error', $result->get_error_message() );
117
118
            return $this->app->response()->error( $result );
119
        }
120
121
        update_option( '_wogh_import_complete', 'yes' );
122
123
        return $this->app->response()->success( $result );
124
    }
125
126
    /**
127
     * Export all the posts in the database to GitHub.
128
     *
129
     * @param  int        $user_id
130
     * @param  boolean    $force
131
     * @return boolean
132
     */
133
    public function export_all( $user_id = 0, $force = false ) {
134
        if ( ! $this->app->semaphore()->is_open() ) {
135
            return $this->app->response()->error( new WP_Error(
136
                'semaphore_locked',
137
                sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' )
138
            ) );
139
        }
140
141
        $this->app->semaphore()->lock();
142
143
        if ( $user_id ) {
144
            wp_set_current_user( $user_id );
145
        }
146
147
        $result = $this->app->export()->full($force);
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...
148
        $this->app->semaphore()->unlock();
149
150
        // Maybe move option updating out of this class/upgrade message display?
151
        if ( is_wp_error( $result ) ) {
152
            /* @var WP_Error $result */
153
            update_option( '_wogh_export_error', $result->get_error_message() );
154
155
            return $this->app->response()->error( $result );
156
        } else {
157
            update_option( '_wogh_export_complete', 'yes' );
158
            update_option( '_wogh_fully_exported', 'yes' );
159
160
            return $this->app->response()->success( $result );
161
        }
162
    }
163
164
    /**
165
     * Exports a single post to GitHub by ID.
166
     *
167
     * Called on the save_post hook.
168
     *
169
     * @param int $post_id Post ID.
170
     *
171
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be null|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...
172
     */
173 View Code Duplication
    public function export_post( $post_id ) {
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...
174
        if ( wp_is_post_revision( $post_id ) ) {
175
            return;
176
        }
177
178
        if ( ! $this->app->semaphore()->is_open() ) {
179
            return $this->app->response()->error( new WP_Error(
180
                'semaphore_locked',
181
                sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' )
182
            ) );
183
        }
184
185
        $this->app->semaphore()->lock();
186
        $result = $this->app->export()->update( $post_id );
187
        $this->app->semaphore()->unlock();
188
189
        if ( is_wp_error( $result ) ) {
190
            /* @var WP_Error $result */
191
            return $this->app->response()->error( $result );
192
        }
193
194
        return $this->app->response()->success( $result );
195
    }
196
197
    /**
198
     * Removes the post from the tree.
199
     *
200
     * Called the delete_post hook.
201
     *
202
     * @param int $post_id Post ID.
203
     *
204
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be null|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...
205
     */
206 View Code Duplication
    public function delete_post( $post_id ) {
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...
207
        if ( wp_is_post_revision( $post_id ) ) {
208
            return;
209
        }
210
211
        if ( ! $this->app->semaphore()->is_open() ) {
212
            return $this->app->response()->error( new WP_Error(
213
                'semaphore_locked',
214
                sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' )
215
            ) );
216
        }
217
218
        $this->app->semaphore()->lock();
219
        $result = $this->app->export()->delete( $post_id );
220
        $this->app->semaphore()->unlock();
221
222
        if ( is_wp_error( $result ) ) {
223
            /* @var WP_Error $result */
224
            return $this->app->response()->error( $result );
225
        }
226
227
        return $this->app->response()->success( $result );
228
    }
229
230
    /**
231
     * Indicates we're running our own AJAX hook
232
     * and thus should respond with JSON, rather
233
     * than just returning data.
234
     */
235
    protected function set_ajax() {
236
        if ( ! defined( 'WOGH_AJAX' ) ) {
237
            define( 'WOGH_AJAX', true );
238
        }
239
    }
240
}
241