Passed
Branch master (6aefea)
by litefeel
02:56
created

Writing_On_GitHub_Response::error()   C

Complexity

Conditions 8
Paths 3

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 11
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 27
ccs 0
cts 16
cp 0
crap 72
rs 5.3846
1
<?php
2
/**
3
 * Response management object.
4
 * @package Writing_On_GitHub
5
 */
6
7
/**
8
 * Class Writing_On_GitHub_Response
9
 */
10
class Writing_On_GitHub_Response {
11
12
    /**
13
     * Application container.
14
     *
15
     * @var Writing_On_GitHub
16
     */
17
    protected $app;
18
19
    /**
20
     * Writing_On_GitHub_Response constructor.
21
     *
22
     * @param Writing_On_GitHub $app Application container.
23
     */
24
    public function __construct( Writing_On_GitHub $app ) {
25
        $this->app = $app;
26
    }
27
28
    /**
29
     * Writes to the log and returns the error response.
30
     *
31
     * @param WP_Error $error Error to respond with.
32
     *
33
     * @return false
34
     */
35
    public function error( WP_Error $error ) {
0 ignored issues
show
Bug introduced by
The type WP_Error 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...
36
        global $wp_version;
37
38
        $this->log( $error );
39
40
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX && defined( 'WOGH_AJAX' ) && WOGH_AJAX ) {
0 ignored issues
show
Bug introduced by
The constant DOING_AJAX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
41
            /**
42
             * WordPress 4.1.0 introduced allowing WP_Error objects to be
43
             * passed directly into `wp_send_json_error`. This shims in
44
             * compatibility for older versions. We're currently supporting 3.9+.
45
             */
46
            if ( version_compare( $wp_version, '4.1.0', '<' ) ) {
47
                $result = array();
48
49
                foreach ( $error->errors as $code => $messages ) {
50
                    foreach ( $messages as $message ) {
51
                        $result[] = array( 'code' => $code, 'message' => $message );
52
                    }
53
                }
54
55
                $error = $result;
56
            }
57
58
            wp_send_json_error( $error );
0 ignored issues
show
Bug introduced by
The function wp_send_json_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

58
            /** @scrutinizer ignore-call */ 
59
            wp_send_json_error( $error );
Loading history...
59
        }
60
61
        return false;
62
    }
63
64
    /**
65
     * Writes to the log and returns the success response.
66
     *
67
     * @param string $success Success message to respond with.
68
     *
69
     * @return true
70
     */
71
    public function success( $success ) {
72
        $this->log( $success );
73
74
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX && defined( 'WOGH_AJAX' ) && WOGH_AJAX ) {
0 ignored issues
show
Bug introduced by
The constant DOING_AJAX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
75
            wp_send_json_success( $success );
0 ignored issues
show
Bug introduced by
The function wp_send_json_success was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

75
            /** @scrutinizer ignore-call */ 
76
            wp_send_json_success( $success );
Loading history...
76
        }
77
78
        return true;
79
    }
80
81
    /**
82
     * Writes a log message.
83
     *
84
     * Can extract a message from WP_Error object.
85
     *
86
     * @param string|WP_Error $msg Message to log.
87
     */
88
    protected function log( $msg ) {
89
        if ( is_wp_error( $msg ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

89
        if ( /** @scrutinizer ignore-call */ is_wp_error( $msg ) ) {
Loading history...
90
            $msg = $msg->get_error_message();
91
        }
92
93
        Writing_On_GitHub::write_log( $msg );
94
    }
95
}
96