Client::get_api_key()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 6
1
<?php
2
3
/**
4
 * The dashboard-specific functionality of the plugin.
5
 *
6
 * @link       http://example.com
7
 * @since      1.0.0
8
 *
9
 * @package    PluginName
10
 * @subpackage PluginName/admin
11
 */
12
13
namespace logoscon\WP\RedmineEmbed\Redmine;
14
15
use logoscon\WP\RedmineEmbed\Plugin;
16
17
/**
18
 * The dashboard-specific functionality of the plugin.
19
 *
20
 * Defines the plugin name, version, and two examples hooks for how to
21
 * enqueue the dashboard-specific stylesheet and JavaScript.
22
 *
23
 * @package    PluginName
24
 * @subpackage PluginName/admin
25
 * @author     Your Name <[email protected]>
26
 */
27
class Client {
28
29
    /**
30
     * The plugin's instance.
31
     *
32
     * @since  1.0.0
33
     * @access private
34
     * @var    Plugin $plugin This plugin's instance.
35
     */
36
    private $plugin;
37
38
    /**
39
     * Root URL for Redmine.
40
     * @access private
41
     * @var string
42
     */
43
    private $root_url = '';
44
45
    /**
46
     * REST API key for Redmine.
47
     * @var string
48
     */
49
    private $api_key = '';
50
51
    /**
52
     * Initialize the class and set its properties.
53
     *
54
     * @since 1.0.0
55
     *
56
     * @param Plugin $plugin This plugin's instance.
57
     */
58
    public function __construct( Plugin $plugin ) {
59
        $this->plugin   = $plugin;
60
        $this->api_key  = $this->get_api_key();
61
        $this->root_url = \trailingslashit( $plugin->get_option( 'root_url' ) );
62
        $this->url      = new UrlBuilder( $plugin );
0 ignored issues
show
Bug introduced by
The property url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
    }
64
65
    /**
66
     * Fetch an issue.
67
     * @param  string $id      Issue ID.
68
     * @param  array  $options Request options.
69
     * @param  array  $expires Cache TTL, in seconds (defaults to 3600).
0 ignored issues
show
Documentation introduced by
Should the type for parameter $expires not be integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
70
     * @return mixed           Response data.
71
     */
72
    public function get_issue( $id, $options = array(), $expires = 3600  ) {
73
        $url = $this->url->get_json_resource_url( 'issues', $id );
74
75
        return json_decode( $this->get( $url, $options, $expires ) );
76
    }
77
78
    /**
79
     * Handle GET requests.
80
     * @param  string $resource Resource to fetch
0 ignored issues
show
Bug introduced by
There is no parameter named $resource. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
81
     * @param  array  $options  Request options.
82
     * @param  array  $expires  Cache TTL, in seconds (defaults to 3600).
0 ignored issues
show
Documentation introduced by
Should the type for parameter $expires not be integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
83
     * @return mixed            Response data.
84
     */
85
    public function get( $url, $options = array(), $expires = 3600 ) {
86
        $options = $this->add_credentials( $options );
87
88
        if ( $expires === false ) {
89
            // Bypass cache
90
            return $this->get_body( $url, $options );
91
        }
92
93
        $cache_key = sha1( $url . $this->api_key );
94
95
        return \tlc_transient( $cache_key )
96
            ->updates_with( array( $this, 'get_body' ), array( $url, $options ) )
97
            ->expires_in( $expires )
98
            ->get();
99
    }
100
101
    /**
102
     * Fetch the response body for a GET request.
103
     * @param  string $resource Resource to fetch
0 ignored issues
show
Bug introduced by
There is no parameter named $resource. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
     * @param  array  $options  Request options.
105
     * @return string           Request body.
106
     */
107
    public function get_body( $url, $options ) {
108
        $response = \wp_remote_get( $url, $options );
109
        $code     = \wp_remote_retrieve_response_code( $response );
110
        $message  = \wp_remote_retrieve_response_message( $response );
111
112
        if ( substr( $code, 0, 1 ) !== '2' ) {
113
            // A non-2xx class status code means there was an error.
114
            throw new \Exception( $message, (int) $code );
115
        }
116
117
        return \wp_remote_retrieve_body( $response );
118
    }
119
120
    /**
121
     * Add credentials to a set of request options.
122
     * @param array $options Request options.
123
     */
124
    private function add_credentials( $options = array() ) {
125
        if ( ! isset( $options['headers'] ) ) {
126
            $options['headers'] = array();
127
        }
128
129
        $options['headers']['X-Redmine-API-Key'] = $this->api_key;
130
131
        return $options;
132
    }
133
134
    /**
135
     * Get the configured Redmine API key.
136
     *
137
     * Looks for the user key first and falls back to the globally configured one.
138
     *
139
     * @return string Configured Redmine API key.
140
     */
141
    private function get_api_key() {
142
        $user_id = \get_current_user_id();
143
        $api_key = \get_user_option( 'redmine_embed_api_key', $user_id );
144
145
        if ( empty( $api_key ) ) {
146
            $api_key = $this->plugin->get_option( 'api_key' );
147
        }
148
149
        return \sanitize_key( $api_key );
150
    }
151
152
}
153