-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClient.php
147 lines (129 loc) · 2.95 KB
/
Client.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* Copyright (c) 2016. Francois Raubenheimer
*/
namespace Peach\Oppwa;
/**
* Class Client
* @package Peach\Oppwa
*/
class Client
{
// client version
const CLIENT_VERSION = '1.0.0';
// api location and version
const API_URI_TEST = 'https://test.oppwa.com/';
const API_URI_LIVE = 'https://oppwa.com/';
const API_URI_VERSION = 'v1';
// exceptions
const EXCEPTION_BAD_CONFIG = 100;
/**
* Configuration object.
*
* @var Configuration $config
*/
private $config;
/**
* Is test mode.
*
* Defaults to live mode.
*
* @var boolean
*/
private $isTestMode = false;
/**
* Guzzle Http client.
*
* @var \GuzzleHttp\Client
*/
private $client;
/**
* Client constructor.
*
* @param Configuration|array $config
* @throws \Exception
*/
public function __construct($config)
{
// config is Configuration object
if ($config instanceof Configuration) {
$this->config = $config;
}
// config is array
if (is_array($config) && count($config) === 3) {
$this->config = new Configuration($config[0], $config[1], $config[2]);
}
// config is from function arguments
$configArgs = func_get_args();
if (count($configArgs) === 3) {
$this->config = new Configuration($configArgs[0], $configArgs[1], $configArgs[2]);
}
// can not find the configuration
if (!isset($this->config)) {
throw new \Exception("Please configure the client correctly", self::EXCEPTION_BAD_CONFIG);
}
// Setup client;
$this->client = new \GuzzleHttp\Client;
}
/**
* Check if we are currently in test mode.
*
* @return mixed
*/
public function isTestMode()
{
return $this->isTestMode;
}
/**
* Set the client to use test mode.
*
* @param mixed $isTestMode
* @return $this
*/
public function setTestMode($isTestMode)
{
$this->isTestMode = $isTestMode;
return $this;
}
/**
* Get configuration object.
*
* @return Configuration
*/
public function getConfig()
{
return $this->config;
}
/**
* Provide the ability to use an alternative entity id.
*
* @param $entityId
* @return $this
*/
public function setEntityId($entityId)
{
$this->config->setEntityId($entityId);
return $this;
}
/**
* Get Guzzle Client.
*
* @return \GuzzleHttp\Client
*/
public function getClient()
{
return $this->client;
}
/**
* Get api uri.
*
* @return string
*/
public function getApiUri()
{
if ($this->isTestMode()) {
return self::API_URI_TEST . self::API_URI_VERSION;
}
return self::API_URI_LIVE . self::API_URI_VERSION;
}
}