Completed
Pull Request — master (#215)
by
unknown
03:21
created

Client::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * WooCommerce REST API Client
4
 *
5
 * @category Client
6
 * @package  Automattic/WooCommerce
7
 */
8
9
namespace Automattic\WooCommerce;
10
11
use Automattic\WooCommerce\HttpClient\HttpClient;
12
13
/**
14
 * REST API Client class.
15
 *
16
 * @package Automattic/WooCommerce
17
 */
18
class Client
19
{
20
21
    /**
22
     * WooCommerce REST API Client version.
23
     */
24
    const VERSION = '2.0.1';
25
26
    /**
27
     * HttpClient instance.
28
     *
29
     * @var HttpClient
30
     */
31
    public $http;
32
33
    /**
34
     * Initialize client.
35
     *
36
     * @param string $url            Store URL.
37
     * @param string $consumerKey    Consumer key.
38
     * @param string $consumerSecret Consumer secret.
39
     * @param array  $options        Options (version, timeout, verify_ssl).
40
     */
41
    public function __construct($url, $consumerKey, $consumerSecret, $options = [])
42
    {
43
        $this->http = new HttpClient($url, $consumerKey, $consumerSecret, $options);
44
    }
45
46
    /**
47
     * POST method.
48
     *
49
     * @param string $endpoint API endpoint.
50
     * @param array  $data     Request data.
51
     *
52
     * @return array
53
     */
54
    public function post($endpoint, $data)
55
    {
56
        return $this->http->request($endpoint, 'POST', $data);
57
    }
58
59
    /**
60
     * PUT method.
61
     *
62
     * @param string $endpoint API endpoint.
63
     * @param array  $data     Request data.
64
     *
65
     * @return array
66
     */
67
    public function put($endpoint, $data)
68
    {
69
        return $this->http->request($endpoint, 'PUT', $data);
70
    }
71
72
    /**
73
     * GET method.
74
     *
75
     * @param string $endpoint   API endpoint.
76
     * @param array  $parameters Request parameters.
77
     * @param \Closure $middleware custom filter for result
78
     * @return array
79
     */
80
    public function get($endpoint, $parameters = [],$middleware=null)
81
    {
82
        $res =$this->http->request($endpoint, 'GET', [], $parameters);
83
84
        if ($middleware instanceof \Closure){
85
86
            return $middleware($res);
87
        }
88
        return $res;
89
    }
90
91
    /**
92
     * DELETE method.
93
     *
94
     * @param string $endpoint   API endpoint.
95
     * @param array  $parameters Request parameters.
96
     *
97
     * @return array
98
     */
99
    public function delete($endpoint, $parameters = [])
100
    {
101
        return $this->http->request($endpoint, 'DELETE', [], $parameters);
102
    }
103
104
    /**
105
     * OPTIONS method.
106
     *
107
     * @param string $endpoint API endpoint.
108
     *
109
     * @return array
110
     */
111
    public function options($endpoint)
112
    {
113
        return $this->http->request($endpoint, 'OPTIONS', [], []);
114
    }
115
116
    /**
117
     * Set http Client
118
     *
119
     * @param HttpClient $http $httpClient
120
     *
121
     * @return void
122
     */
123
    public function setHttp($http) {
124
        $this->http = $http;
125
    }
126
127
}
128