Passed
Push — master ( ac443d...cc4e96 )
by david
03:11
created

Sdk::getThreadJsonLdFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Wrapper API Graphcomment v2.2
4
 * @author ddtraceweb <[email protected]>
5
 * @copyright 2018 Graphcomment
6
 * Date: 12/11/2018
7
 * Time: 18:37
8
 */
9
10
namespace Graphcomment;
11
12
use GuzzleHttp\Client;
13
use GuzzleHttp\Exception\GuzzleException;
14
15
/**
16
 * Class Sdk
17
 * @package Graphcomment
18
 */
19
class Sdk
20
{
21
22
    /**
23
     * @var
24
     */
25
    protected $gcPublic;
26
    protected $gcSecret;
27
    protected $dir = 'https://graphcomment.com/api';
28
29
    /**
30
     * Sdk constructor.
31
     * @param $GC_PUBLIC
32
     * @param $GC_SECRET
33
     */
34
    public function __construct($GC_PUBLIC, $GC_SECRET)
35
    {
36
        $this->setGcPublic($GC_PUBLIC);
37
        $this->setGcSecret($GC_SECRET);
38
    }
39
40
    /**
41
     * @return mixed
42
     */
43
    public function getGcPublic()
44
    {
45
        return $this->gcPublic;
46
    }
47
48
    /**
49
     * @param mixed $gcPublic
50
     */
51
    public function setGcPublic($gcPublic)
52
    {
53
        $this->gcPublic = $gcPublic;
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59
    public function getGcSecret()
60
    {
61
        return $this->gcSecret;
62
    }
63
64
    /**
65
     * @param mixed $gcSecret
66
     */
67
    public function setGcSecret($gcSecret)
68
    {
69
        $this->gcSecret = $gcSecret;
70
    }
71
72
    /**
73
     * @return mixed
74
     */
75
    public function getDir()
76
    {
77
        return $this->dir;
78
    }
79
80
    /**
81
     * @param mixed $dir
82
     */
83
    public function setDir($dir)
84
    {
85
        $this->dir = $dir;
86
    }
87
88
    /**
89
     * registerUser() Register a user to Graphcomment.
90
     *
91
     * @param string $username required unique
92
     * @param string $email required unique
93
     * @param string $language (optionnal) default value : en (codes ISO 639-1)
94
     * @param string $picture (full url only example : https://graphcomment.com/image.jpg)
95
     *
96
     * @return object  json response gc_id to store in your database and do_sync which define date of synchronisation
97
     * @throws GuzzleException
98
     */
99
    public function registerUser($username, $email, $language = "en", $picture = '')
100
    {
101
        $client = new Client();
102
        $data = array(
103
            "username" => $username, // required unique
104
            "email" => $email, // required unique
105
            "language" => $language, //(optionnal) default value : en (codes ISO 639-1)
106
            "picture" => $picture // (optionnal) full url only
107
        );
108
109
        $res = $client->request('POST', $this->getDir() . '/pub/sso/registerUser/pubkey/' . $this->getGcPublic() . '/key/' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]);
110
111
        if ($res->getStatusCode() == "200") {
112
            return $res->getBody();
113
        } else {
114
            return $res->getBody();
115
        }
116
    }
117
118
    /**
119
     * loginUser() authenticate a user and return a token to login in graphcomment.
120
     *
121
     * @param string $gc_id
122
     *
123
     * @return object  json JWT response
124
     * @throws GuzzleException
125
     */
126
    public function loginUser($gc_id)
127
    {
128
        $client = new Client();
129
130
        $data = array(
131
            "gc_id" => $gc_id
132
        );
133
134
        $res = $client->request('POST', $this->getDir() . '/pub/sso/loginUser/pubkey/' . $this->getGcPublic(). '/key/' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]);
135
136
        return $res->getBody();
137
    }
138
139
140
    /**
141
     * getUser() return the informations that we have on the user
142
     *
143
     * @param $gc_id
144
     * @return object JSON with do_sync date, if changed, you must synchronise the informations.
145
     * @throws GuzzleException
146
     */
147
    public function getUser($gc_id)
148
    {
149
        $client = new Client();
150
151
        $data = array(
152
            "gc_id" => $gc_id
153
        );
154
155
        $res = $client->request('GET', $this->getDir() . '/pub/sso/getUser/pubkey/' . $this->getGcPublic(). '/key/' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]);
156
157
        return $res->getBody();
158
    }
159
160
161
    /**
162
     * updateUser() return the informations that we have on the user
163
     *
164
     * @param $gc_id
165
     * @param string $username required unique
166
     * @param string $email required unique
167
     * @param string $language (optionnal) default value : en (codes ISO 639-1)
168
     * @param string $picture (full url only example : https://graphcomment.com/image.jpg)
169
     *
170
     * @return object JSON {
171
     *   gc_id : data.gc_id,
172
     *   do_sync : date of synchronisation
173
     *  res :'updated'
174
     *  } or {
175
     *      gc_id : data.gc_id,
176
     *      res :'nothing updated'
177
     *  }
178
     * @throws GuzzleException
179
     */
180
    public function updateUser($gc_id, $username, $email, $language, $picture)
181
    {
182
        $client = new Client();
183
184
        $data = array(
185
            "gc_id" => $gc_id,
186
            "username" => $username,
187
            "email" => $email,
188
            "language" => $language,
189
            "picture" => $picture
190
        );
191
192
        $res = $client->request('PUT', $this->getDir() . '/pub/sso/updateUser/pubkey/' . $this->getGcPublic(). '/key/' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]);
193
194
        return $res->getBody();
195
    }
196
197
198
    /**
199
     * deleteUser() delete a user and return ok confirmation.
200
     *
201
     * @param string $gc_id
202
     *
203
     * @return string ok
204
     * @throws GuzzleException
205
     */
206
    public function deleteUser($gc_id)
0 ignored issues
show
Unused Code introduced by
The parameter $gc_id is not used and could be removed. ( Ignorable by Annotation )

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

206
    public function deleteUser(/** @scrutinizer ignore-unused */ $gc_id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
207
    {
208
        $client = new Client();
209
210
        $res = $client->request('DELETE', $this->getDir() . '/pub/sso/deleteProfileByGcId/pubkey/' . $this->getGcPublic(). '/key/' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $data seems to be never defined.
Loading history...
211
212
        return $res->getBody();
213
    }
214
215
216
    /**
217
     * countComments() return the number thread's comment
218
     *
219
     * @param $url (full url only) required
0 ignored issues
show
Documentation Bug introduced by
The doc comment (full at position 1 could not be parsed: Expected ')' at position 1, but found 'full'.
Loading history...
220
     * @param string $uid (unique id of the thread) optionnal
221
     * @return object json {count: numberOfComments }
222
     * @throws GuzzleException
223
     */
224
    public function countComments($url, $uid='') {
225
        $client = new Client();
226
227
        $data = array(
228
            "url" => $url,
229
            "uid" => $uid
230
        );
231
232
        $res = $client->request('GET', $this->getDir() . '/pub/sso/numberOfComments/pubkey/' . $this->getGcPublic(). '/key/' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]);
233
234
        return $res->getBody();
235
    }
236
237
238
    public function getThreadJsonLdFormat($url, $uid='') {
239
        $client = new Client();
240
241
        $data = array(
242
            "url" => $url,
243
            "uid" => $uid
244
        );
245
246
        $res = $client->request('GET', $this->getDir() . '/pub/sso/thread-jsonld/pubkey/' . $this->getGcPublic(). '/key/' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]);
247
248
        return $res->getBody();
249
    }
250
251
    /**
252
     * exportComments() return the comments to import in your system, group by 20 comments
253
     *
254
     * @return array of object json [{commentObject}]
255
     * @throws GuzzleException
256
     */
257
    public function exportComments() {
258
        $client = new Client();
259
260
        $res = $client->request('GET', $this->getDir() . '/pub/export-comments/pubkey/' . $this->getGcPublic(). '/key/' . $this->generateSsoData('key'), ['http_errors' => false, 'timeout' => 5]);
261
262
        return $res->getBody();
263
    }
264
265
    /**
266
     * exportConfirmComments() send id
267
     *
268
     * @param array $commentIds
269
     *
270
     * @return array of object json [{commentObject}]
271
     * @throws GuzzleException
272
     */
273
    public function exportConfirmComments(Array $commentIds) {
274
        $client = new Client();
275
276
        $data = $commentIds;
277
278
        $res = $client->request('POST', $this->getDir() . '/pub/export-comments/pubkey/' . $this->getGcPublic(). '/key/' . $this->generateSsoData($data), ['http_errors' => false, 'timeout' => 5]);
279
280
        return $res->getBody();
281
    }
282
283
    /**
284
     * generateSsoData() generate sso Data
285
     *
286
     * @param $data array|string
287
     * @return string
288
     */
289
    private function generateSsoData($data) {
290
        $message = base64_encode(json_encode($data));
291
        $timestamp = time();
292
293
        $hexsig = $this->gcHmacsha1($message . ' ' . $timestamp, $this->getGcSecret());
294
295
        return $message . ' ' . $hexsig . ' ' . $timestamp;
296
    }
297
298
    /**
299
     * gcHmacsha1() encode datas
300
     *
301
     * @param $data
302
     * @param $key
303
     * @return string
304
     */
305
    private function gcHmacsha1($data, $key)
306
    {
307
308
        $blocksize = 64;
309
        $hashfunc = 'sha1';
310
311
        if (strlen($key) > $blocksize)
312
            $key = pack('H*', $hashfunc($key));
313
314
        $key = str_pad($key, $blocksize, chr(0x00));
315
        $ipad = str_repeat(chr(0x36), $blocksize);
316
        $opad = str_repeat(chr(0x5c), $blocksize);
317
        $hmac = pack(
318
            'H*', $hashfunc(
319
                ($key ^ $opad) . pack(
320
                    'H*', $hashfunc(
321
                        ($key ^ $ipad) . $data
322
                    )
323
                )
324
            )
325
        );
326
327
        return bin2hex($hmac);
328
    }
329
}