Webhooks   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 158
rs 10
c 0
b 0
f 0
wmc 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getGraphID() 0 3 1
A setWebhookHash() 0 5 1
A setType() 0 5 1
A invoke() 0 11 1
A get() 0 19 1
A create() 0 22 1
A setGraphID() 0 5 1
A getType() 0 3 1
A getWebhookHash() 0 3 1
A delete() 0 11 1
1
<?php
2
3
namespace Pixela\Api;
4
5
class Webhooks extends Api implements WebhooksInterface
6
{
7
    /**
8
     * @var string
9
     */
10
    private $graphID;
11
12
    /**
13
     * @var string
14
     */
15
    private $type;
16
17
    /**
18
     * @var string
19
     */
20
    private $webhookHash;
21
22
    /**
23
     * @throws \GuzzleHttp\Exception\GuzzleException
24
     */
25
    public function create()
26
    {
27
        $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/webhooks';
28
29
        $options = array(
30
            'headers' => array(
31
                'X-USER-TOKEN' => $this->getClient()->getToken()
32
            ),
33
            'body' => json_encode(
34
                array(
35
                    'graphID' => $this->getGraphID(),
36
                    'type' => $this->getType(),
37
                )
38
            )
39
        );
40
41
        $response = $this->getClient()->getHttpClient()->request('post', $uri, $options);
42
        $contents = json_decode($response->getBody()->getContents(), true);
43
44
        $this->setWebhookHash($contents['webhookHash']);
45
46
        return true;
47
    }
48
49
    /**
50
     * @return \Pixela\Api\WebhooksInterface[]
51
     * @throws \GuzzleHttp\Exception\GuzzleException
52
     */
53
    public function get()
54
    {
55
        $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/webhooks';
56
57
        $options = array(
58
            'headers' => array(
59
                'X-USER-TOKEN' => $this->getClient()->getToken()
60
            )
61
        );
62
63
        $response = $this->getClient()->getHttpClient()->request('get', $uri, $options);
64
        $contents = json_decode($response->getBody()->getContents(), true);
65
66
        return array_map(function ($data) {
67
            $webhook = new \Pixela\Api\Webhooks($this->getClient());
68
            return $webhook->setGraphID($data['graphID'])
69
                ->setType($data['type'])
70
                ->setWebhookHash($data['webhookHash']);
71
        }, $contents['webhooks']);
72
    }
73
74
    /**
75
     * @return mixed|\Psr\Http\Message\ResponseInterface
76
     * @throws \GuzzleHttp\Exception\GuzzleException
77
     */
78
    public function invoke()
79
    {
80
        $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/webhooks/' . $this->getWebhookHash();
81
82
        $options = array(
83
            'headers' => array(
84
                'Content-Length' => 0
85
            ),
86
        );
87
88
        return (bool)$this->getClient()->getHttpClient()->request('post', $uri, $options);
89
    }
90
91
    /**
92
     * @return bool
93
     * @throws \GuzzleHttp\Exception\GuzzleException
94
     */
95
    public function delete()
96
    {
97
        $uri = Api::API_BASE_ENDPOINT . '/users/' . $this->getClient()->getUsername() . '/webhooks/' . $this->getWebhookHash();
98
99
        $options = array(
100
            'headers' => array(
101
                'X-USER-TOKEN' => $this->getClient()->getToken()
102
            )
103
        );
104
105
        return (bool)$this->getClient()->getHttpClient()->request('delete', $uri, $options);
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getGraphID()
112
    {
113
        return $this->graphID;
114
    }
115
116
    /**
117
     * @param string $graphID
118
     * @return $this
119
     */
120
    public function setGraphID($graphID)
121
    {
122
        $this->graphID = $graphID;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getType()
131
    {
132
        return $this->type;
133
    }
134
135
    /**
136
     * @param string $type
137
     * @return $this
138
     */
139
    public function setType($type)
140
    {
141
        $this->type = $type;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getWebhookHash()
150
    {
151
        return $this->webhookHash;
152
    }
153
154
    /**
155
     * @param string $hash
156
     * @return $this
157
     */
158
    public function setWebhookHash($hash)
159
    {
160
        $this->webhookHash = $hash;
161
162
        return $this;
163
    }
164
}
165