forked from james75/XeroBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XeroClient.php
65 lines (54 loc) · 1.63 KB
/
XeroClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace BlackOptic\Bundle\XeroBundle;
use Guzzle\Common\Collection;
use Guzzle\Plugin\Oauth\OauthPlugin;
use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
class XeroClient extends Client
{
/**
* @var string
*/
private $token;
/**
* @var string
*/
private $tokenSecret;
/**
* {@inheritDoc}
*/
public function __construct($config = array())
{
$required = array(
'base_url',
'consumer_key',
'consumer_secret',
'token',
'token_secret',
);
if (!array_key_exists('token', $config)) {
$config['token'] = & $this->token;
}
if (!array_key_exists('token_secret', $config)) {
$config['token_secret'] = & $this->tokenSecret;
}
$privateKey = file_get_contents($config['private_key']);
$config['signature_method'] = 'RSA-SHA1';
$config['signature_callback'] = function ($baseString) use ($privateKey) {
$signature = '';
$privateKeyId = openssl_pkey_get_private($privateKey);
openssl_sign($baseString, $signature, $privateKeyId);
openssl_free_key($privateKeyId);
return $signature;
};
$config = Collection::fromConfig($config, array(), $required);
parent::__construct($config->get('base_url'), $config);
$this->addSubscriber(new OauthPlugin($config->toArray()));
}
public function setToken($token, $tokenSecret)
{
$this->token = $token;
$this->tokenSecret = $tokenSecret;
return $this;
}
}