Writing_On_GitHub_CLI::export()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 15
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 22
rs 9.7666
1
<?php
2
/**
3
 * WP_CLI Commands
4
 * @package Writing_On_GitHub
5
 */
6
7
/**
8
 * Class Writing_On_GitHub_CLI
9
 */
10
class Writing_On_GitHub_CLI extends WP_CLI_Command {
0 ignored issues
show
Bug introduced by
The type WP_CLI_Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
    /**
13
     * Application container.
14
     *
15
     * @var Writing_On_GitHub
16
     */
17
    protected $app;
18
19
    /**
20
     * Grab the Application container on instantiation.
21
     */
22
    public function __construct() {
23
        $this->app = Writing_On_GitHub::$instance;
24
    }
25
26
    /**
27
     * Exports an individual post
28
     * all your posts to GitHub
29
     *
30
     * ## OPTIONS
31
     *
32
     * <post_id|all>
33
     * : The post ID to export or 'all' for full site
34
     *
35
     * <user_id>
36
     * : The user ID you'd like to save the commit as
37
     *
38
     * ## EXAMPLES
39
     *
40
     *     wp wogh export all 1
41
     *     wp wogh export 1 1
42
     *
43
     * @synopsis <post_id|all> <user_id>
44
     *
45
     * @param array $args Command arguments.
46
     */
47
    public function export( $args ) {
48
        list( $post_id, $user_id ) = $args;
49
50
        if ( ! is_numeric( $user_id ) ) {
51
            WP_CLI::error( __( 'Invalid user ID', 'writing-on-github' ) );
0 ignored issues
show
Bug introduced by
The type WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
        }
53
54
        $this->app->export()->set_user( $user_id );
55
56
        if ( 'all' === $post_id ) {
57
            WP_CLI::line( __( 'Starting full export to GitHub.', 'writing-on-github' ) );
58
            $this->app->controller()->export_all();
59
        } elseif ( is_numeric( $post_id ) ) {
60
            WP_CLI::line(
61
                sprintf(
62
                    __( 'Exporting post ID to GitHub: %d', 'writing-on-github' ),
63
                    $post_id
64
                )
65
            );
66
            $this->app->controller()->export_post( (int) $post_id );
67
        } else {
68
            WP_CLI::error( __( 'Invalid post ID', 'writing-on-github' ) );
69
        }
70
    }
71
72
    /**
73
     * Imports the post in your GitHub repo
74
     * into your WordPress blog
75
     *
76
     * ## OPTIONS
77
     *
78
     * <user_id>
79
     * : The user ID you'd like to save the commit as
80
     *
81
     * ## EXAMPLES
82
     *
83
     *     wp wogh import 1
84
     *
85
     * @synopsis <user_id>
86
     *
87
     * @param array $args Command arguments.
88
     */
89
    public function import( $args ) {
90
        list( $user_id ) = $args;
91
92
        if ( ! is_numeric( $user_id ) ) {
93
            WP_CLI::error( __( 'Invalid user ID', 'writing-on-github' ) );
94
        }
95
96
        update_option( '_wogh_export_user_id', (int) $user_id );
97
98
        WP_CLI::line( __( 'Starting import from GitHub.', 'writing-on-github' ) );
99
100
        $this->app->controller()->import_master();
101
    }
102
103
    /**
104
     * Fetches the provided sha or the repository's
105
     * master branch and caches it.
106
     *
107
     * ## OPTIONS
108
     *
109
     * <user_id>
110
     * : The user ID you'd like to save the commit as
111
     *
112
     * ## EXAMPLES
113
     *
114
     *     wp wogh prime --branch=master
115
     *     wp wogh prime --sha=<commit_sha>
116
     *
117
     * @synopsis [--sha=<commit_sha>] [--branch]
118
     *
119
     * @param array $args Command arguments.
120
     * @param array $assoc_args Command associated arguments.
121
     */
122
    public function prime( $args, $assoc_args ) {
123
        if ( isset( $assoc_args['branch'] ) ) {
124
            WP_CLI::line( __( 'Starting branch import.', 'writing-on-github' ) );
125
126
            $commit = $this->app->api()->fetch()->master();
0 ignored issues
show
Bug introduced by
The method master() does not exist on Writing_On_GitHub_Fetch_Client. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

126
            $commit = $this->app->api()->fetch()->/** @scrutinizer ignore-call */ master();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
128
            if ( is_wp_error( $commit ) ) {
129
                WP_CLI::error(
130
                    sprintf(
131
                        __( 'Failed to import and cache branch with error: %s', 'writing-on-github' ),
132
                        $commit->get_error_message()
133
                    )
134
                );
135
            } else {
136
                WP_CLI::success(
137
                    sprintf(
138
                        __( 'Successfully imported and cached commit %s from branch.', 'writing-on-github' ),
139
                        $commit->sha()
140
                    )
141
                );
142
            }
143
        } else if ( isset( $assoc_args['sha'] ) ) {
144
            WP_CLI::line( 'Starting sha import.' );
145
146
            $commit = $this->app->api()->fetch()->commit( $assoc_args['sha'] );
0 ignored issues
show
Bug introduced by
The method commit() does not exist on Writing_On_GitHub_Fetch_Client. Did you maybe mean commit_endpoint()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
            $commit = $this->app->api()->fetch()->/** @scrutinizer ignore-call */ commit( $assoc_args['sha'] );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
148
            WP_CLI::success(
149
                sprintf(
150
                    __( 'Successfully imported and cached commit %s.', 'writing-on-github' ),
151
                    $commit->sha()
152
                )
153
            );
154
        } else {
155
            WP_CLI::error( 'Invalid fetch.' );
156
        }
157
    }
158
}
159