Skip to content

Commit 925a2ab

Browse files
authored
Merge pull request #1 from vpg/19.34
merge eventfarm#19.34 - Add PHP 7.0 & bulk support
2 parents 0f3589b + f742603 commit 925a2ab

11 files changed

Lines changed: 966 additions & 118 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"codeclimate/php-test-reporter": "dev-master"
99
},
1010
"require": {
11-
"php": ">=7.1",
11+
"php": ">=7.0",
1212
"psr/http-message": "^1.0",
1313
"guzzlehttp/guzzle": "^6.3",
1414
"guzzlehttp/psr7": "^1.4"

src/Rest/GuzzleRestClient.php

Lines changed: 113 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
<?php
2-
namespace EventFarm\Restforce\Rest;
32

4-
use Psr\Http\Message\ResponseInterface;
3+
namespace EventFarm\Restforce\Rest;
54

5+
/**
6+
* Class GuzzleRestClient
7+
*
8+
* @package EventFarm\Restforce\Rest
9+
*/
610
class GuzzleRestClient implements RestClientInterface
711
{
812
const DEFAULT_TIMEOUT_SECONDS = 20.0;
@@ -12,6 +16,11 @@ class GuzzleRestClient implements RestClientInterface
1216
/** @var bool */
1317
private $enableDebugging;
1418

19+
/**
20+
* GuzzleRestClient constructor.
21+
* @param string $baseUri base uri
22+
* @param bool $enableDebugging debugging status
23+
*/
1524
public function __construct(
1625
string $baseUri,
1726
bool $enableDebugging = false
@@ -20,7 +29,14 @@ public function __construct(
2029
$this->setBaseUriForRestClient($baseUri);
2130
}
2231

23-
public function setBaseUriForRestClient(string $baseUri): void
32+
/**
33+
* Set base uri for client
34+
*
35+
* @param string $baseUri base uri
36+
*
37+
* @return void
38+
*/
39+
public function setBaseUriForRestClient(string $baseUri)
2440
{
2541
if (!$this->containsTrailingSlash($baseUri)) {
2642
$baseUri .= '/';
@@ -34,68 +50,108 @@ public function setBaseUriForRestClient(string $baseUri): void
3450
$this->client = new \GuzzleHttp\Client($config);
3551
}
3652

53+
/**
54+
* Get method
55+
*
56+
* @param string $path path
57+
* @param array $queryParameters parameters
58+
* @param array $headers headers
59+
* @param float|null $timeoutSeconds timeout
60+
*
61+
* @return mixed
62+
* @throws \GuzzleHttp\Exception\GuzzleException
63+
*/
3764
public function get(
3865
string $path,
3966
array $queryParameters = [],
4067
array $headers = [],
41-
?float $timeoutSeconds = null
42-
): ResponseInterface {
68+
float $timeoutSeconds = null
69+
) {
4370
return $this->client->request(
4471
'GET',
4572
$path,
4673
[
4774
'timeout' => $timeoutSeconds,
4875
'headers' => $headers,
49-
'query' => $queryParameters,
50-
'http_errors' => false,
76+
'query' => $queryParameters
5177
]
5278
);
5379
}
5480

81+
/**
82+
* Post method
83+
*
84+
* @param string $path path
85+
* @param array $formParameters parameters
86+
* @param array $headers headers
87+
* @param float|null $timeoutSeconds timeout
88+
*
89+
* @return mixed
90+
* @throws \GuzzleHttp\Exception\GuzzleException
91+
*/
5592
public function post(
5693
string $path,
5794
array $formParameters = [],
5895
array $headers = [],
59-
?float $timeoutSeconds = null
60-
): ResponseInterface {
96+
float $timeoutSeconds = null
97+
) {
6198
return $this->client->request(
6299
'POST',
63100
$path,
64101
[
65102
'timeout' => $timeoutSeconds,
66103
'headers' => $headers,
67-
'form_params' => $formParameters,
68-
'http_errors' => false,
104+
'form_params' => $formParameters
69105
]
70106
);
71107
}
72108

109+
/**
110+
* Post method JSON formatted
111+
*
112+
* @param string $path path
113+
* @param array $jsonArray parameters
114+
* @param array $headers headers
115+
* @param float|null $timeoutSeconds timeout
116+
*
117+
* @return mixed
118+
* @throws \GuzzleHttp\Exception\GuzzleException
119+
*/
73120
public function postJson(
74121
string $path,
75122
array $jsonArray = [],
76123
array $headers = [],
77-
?float $timeoutSeconds = null
78-
): ResponseInterface {
124+
float $timeoutSeconds = null
125+
) {
79126
$headers['Content-Type'] = 'application/json';
80-
81127
return $this->client->request(
82128
'POST',
83129
$path,
84130
[
85131
'timeout' => $timeoutSeconds,
86132
'headers' => $headers,
87-
'json' => $jsonArray,
88-
'http_errors' => false,
133+
'json' => $jsonArray
89134
]
90135
);
91136
}
92137

138+
/**
139+
* Patch method JSON formatted
140+
*
141+
* @param string $path path
142+
* @param array $jsonArray parameters
143+
* @param array $headers headers
144+
* @param float|null $timeoutSeconds timeout
145+
*
146+
* @return mixed
147+
* @throws \GuzzleHttp\Exception\GuzzleException
148+
*/
93149
public function patchJson(
94150
string $path,
95151
array $jsonArray = [],
96152
array $headers = [],
97-
?float $timeoutSeconds = null
98-
): ResponseInterface {
153+
float $timeoutSeconds = null
154+
) {
99155
$headers['Content-Type'] = 'application/json';
100156

101157
return $this->client->request(
@@ -104,13 +160,49 @@ public function patchJson(
104160
[
105161
'timeout' => $timeoutSeconds,
106162
'headers' => $headers,
107-
'json' => $jsonArray,
108-
'http_errors' => false,
163+
'json' => $jsonArray
164+
]
165+
);
166+
}
167+
168+
/**
169+
* Put method CSV formatted
170+
*
171+
* @param string $path path
172+
* @param string $filePath file path
173+
* @param array $headers headers
174+
* @param float|null $timeoutSeconds timeout
175+
*
176+
* @return mixed
177+
* @throws \GuzzleHttp\Exception\GuzzleException
178+
*/
179+
public function putCsv(
180+
string $path,
181+
string $filePath,
182+
array $headers = [],
183+
float $timeoutSeconds = null
184+
) {
185+
$headers['Content-Type'] = 'text/csv';
186+
187+
return $this->client->request(
188+
'PUT',
189+
$path,
190+
[
191+
'timeout' => $timeoutSeconds,
192+
'headers' => $headers,
193+
'body' => fopen($filePath, 'r')
109194
]
110195
);
111196
}
112197

113-
private function containsTrailingSlash(string $baseUri): bool
198+
/**
199+
* Check if contains trailing slash
200+
*
201+
* @param string $baseUri base uri
202+
*
203+
* @return bool
204+
*/
205+
private function containsTrailingSlash(string $baseUri)
114206
{
115207
return substr($baseUri, -1) === '/';
116208
}

src/Rest/OAuthAccessToken.php

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22
namespace EventFarm\Restforce\Rest;
33

4+
/**
5+
* Class OAuthAccessToken
6+
*
7+
* @package EventFarm\Restforce\Rest
8+
*/
49
class OAuthAccessToken
510
{
611
/** @var string */
@@ -16,13 +21,23 @@ class OAuthAccessToken
1621
/** @var int|null */
1722
private $expiresAt;
1823

24+
/**
25+
* OAuthAccessToken constructor.
26+
*
27+
* @param string $tokenType token type (password, etc.)
28+
* @param string $accessToken access token
29+
* @param string $instanceUrl instance url
30+
* @param string $resourceOwnerUrl resource owner url
31+
* @param string|null $refreshToken refresh token
32+
* @param int|null $expiresAt expires value
33+
*/
1934
public function __construct(
2035
string $tokenType,
2136
string $accessToken,
2237
string $instanceUrl,
2338
string $resourceOwnerUrl,
24-
?string $refreshToken = null,
25-
?int $expiresAt = null
39+
string $refreshToken = null,
40+
int $expiresAt = null
2641
) {
2742
$this->tokenType = $tokenType;
2843
$this->accessToken = $accessToken;
@@ -32,42 +47,82 @@ public function __construct(
3247
$this->expiresAt = $expiresAt;
3348
}
3449

35-
public function getTokenType(): string
50+
/**
51+
* Get token type
52+
*
53+
* @return string
54+
*/
55+
public function getTokenType()
3656
{
3757
return $this->tokenType;
3858
}
3959

40-
public function getAccessToken(): string
60+
/**
61+
* Get access token
62+
*
63+
* @return string
64+
*/
65+
public function getAccessToken()
4166
{
4267
return $this->accessToken;
4368
}
4469

45-
public function getRefreshToken(): ?string
70+
/**
71+
* Get refresh token
72+
*
73+
* @return null|string
74+
*/
75+
public function getRefreshToken()
4676
{
4777
return $this->refreshToken;
4878
}
4979

50-
public function getInstanceUrl(): string
80+
/**
81+
* Get instance url
82+
*
83+
* @return string
84+
*/
85+
public function getInstanceUrl()
5186
{
5287
return $this->instanceUrl;
5388
}
5489

55-
public function getHeaderString(): string
90+
/**
91+
* Get header string
92+
*
93+
* @return string
94+
*/
95+
public function getHeaderString()
5696
{
5797
return $this->tokenType . ' ' . $this->accessToken;
5898
}
5999

60-
public function getResourceOwnerUrl(): string
100+
/**
101+
* Get resource owner url
102+
*
103+
* @return string
104+
*/
105+
public function getResourceOwnerUrl()
61106
{
62107
return $this->resourceOwnerUrl;
63108
}
64109

65-
public function getExpiresAt(): ?int
110+
/**
111+
* Get expires at value
112+
*
113+
* @return int|null
114+
*/
115+
public function getExpiresAt()
66116
{
67117
return $this->expiresAt;
68118
}
69119

70-
public function isExpired(): bool
120+
/**
121+
* Check if expired
122+
*
123+
* @return bool
124+
*/
125+
public function isExpired()
71126
{
72127
if ($this->expiresAt === null) {
73128
return false;

0 commit comments

Comments
 (0)