Completed
Push — master ( 764ba2...8d4dc4 )
by François
03:10
created

WordpressClient::setApiKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Bouncer package.
5
 *
6
 * (c) François Hodierne <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bouncer\Http;
13
14
class WordpressClient
15
{
16
17
    protected $apiKey;
18
19
    protected $timeout = 2;
20
21
    public function __construct($apiKey = null)
22
    {
23
        $this->apiKey = $apiKey;
24
    }
25
26
    public function setApiKey($apiKey)
27
    {
28
        $this->apiKey = $apiKey;
29
    }
30
31
    public function get($url)
32
    {
33
        $headers = array(
34
            'Accept' => 'application/json',
35
        );
36
        if ($this->apiKey) {
37
            $headers['Api-Key'] = $this->apiKey;
38
        }
39
        $response = wp_remote_get($url, array(
40
            'headers' => $headers,
41
            'timeout' => $this->timeout,
42
        ));
43
        if (is_array($response)) {
44
            $response = json_decode($response['body'], true);
45
            return $response;
46
        }
47
    }
48
49
    public function post($url, $data = null)
50
    {
51
        $headers = array(
52
            'Accept'       => 'application/json',
53
            'Content-Type' => 'application/json',
54
        );
55
        if ($this->apiKey) {
56
            $headers['Api-Key'] = $this->apiKey;
57
        }
58
        $response = wp_remote_post($url, array(
59
            'headers' => $headers,
60
            'body'    => json_encode($data),
61
            'timeout' => $this->timeout,
62
        ));
63
        if (is_array($response)) {
64
            $response = json_decode($response['body'], true);
65
            return $response;
66
        }
67
    }
68
69
}
70