Skip to content

Commit b72fc4e

Browse files
committed
Move common logic to the abstract client class, fix non-throwing of 400s in error handler
1 parent 09117dd commit b72fc4e

File tree

2 files changed

+85
-40
lines changed

2 files changed

+85
-40
lines changed

src/intercom/IntercomAbstractClient.php

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
use InvalidArgumentException;
55
use Guzzle\Service\Client;
6+
use Guzzle\Common\Event;
7+
use Guzzle\Http\Message\Request;
68
use Guzzle\Service\Description\ServiceDescription;
9+
use Intercom\Exception\IntercomException;
10+
use Guzzle\Common\Collection;
711

812
abstract class IntercomAbstractClient extends Client
913
{
@@ -13,14 +17,25 @@ abstract class IntercomAbstractClient extends Client
1317
/** @var string */
1418
const DEFAULT_ACCEPT_HEADER = 'application/vnd.intercom.3+json';
1519

20+
/**
21+
* Configures the client by setting the appropriate headers, service description and error handling
22+
*
23+
* @param Collection $config
24+
*/
25+
protected function configure($config) {
26+
$this->setDefaultOption('headers', $config->get('headers'));
27+
$this->setDescription($this->getServiceDescriptionFromFile($config->get('service_description')));
28+
$this->setErrorHandler();
29+
}
30+
1631
/**
1732
* Loads the service description from the service description file
1833
*
1934
* @param string $description_file The service description file
2035
* @return ServiceDescription
2136
* @throws InvalidArgumentException If the description file doesn't exist or cannot be read
2237
*/
23-
public function getServiceDescriptionFromFile($description_file)
38+
private function getServiceDescriptionFromFile($description_file)
2439
{
2540
if (!file_exists($description_file) || !is_readable($description_file)) {
2641
throw new InvalidArgumentException('Unable to read API definition schema');
@@ -29,6 +44,62 @@ public function getServiceDescriptionFromFile($description_file)
2944
return ServiceDescription::factory($description_file);
3045
}
3146

47+
/**
48+
* Overrides the error handling in Guzzle so that when errors are encountered we throw
49+
* Intercom errors, not Guzzle ones.
50+
*
51+
*/
52+
private function setErrorHandler()
53+
{
54+
$this->getEventDispatcher()->addListener(
55+
'request.error',
56+
function (Event $event) {
57+
// Stop other events from firing when you override 401 responses
58+
$event->stopPropagation();
59+
60+
if ($event['response']->getStatusCode() >= 400 && $event['response']->getStatusCode() < 600) {
61+
$e = IntercomException::factory($event['request'], $event['response']);
62+
$event['request']->setState(Request::STATE_ERROR, array('exception' => $e) + $event->toArray());
63+
throw $e;
64+
}
65+
}
66+
);
67+
}
68+
69+
/**
70+
* Sets the username and password for basic auth on the client
71+
*
72+
* @param string $user
73+
* @param string $password
74+
*/
75+
public function setBasicAuth($user, $password)
76+
{
77+
$this->setDefaultOption(
78+
'auth',
79+
[
80+
$user,
81+
$password,
82+
'Basic'
83+
]
84+
);
85+
}
86+
87+
/**
88+
* Gets the default configuration options for the client
89+
*
90+
* @return array
91+
*/
92+
public static function getDefaultConfig()
93+
{
94+
return [
95+
'service_description' => __DIR__ . '/Service/config/intercom.json',
96+
'headers' => [
97+
'Content-Type' => self::DEFAULT_CONTENT_TYPE,
98+
'Accept' => self::DEFAULT_ACCEPT_HEADER
99+
]
100+
];
101+
}
102+
32103
/**
33104
* A wrapper around two API calls to emulate the ping call from the other APIs
34105
*
@@ -37,7 +108,7 @@ public function getServiceDescriptionFromFile($description_file)
37108
*/
38109
public function ping($user_details)
39110
{
40-
$user = $this->saveUser($user_details);
111+
$this->saveUser($user_details);
41112
return $this->getUnreadUserConversations($user_details);
42113
}
43114
}

src/intercom/IntercomBasicAuthClient.php

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,33 @@
22
namespace Intercom;
33

44
use Guzzle\Common\Collection;
5-
use Guzzle\Common\Event;
65
use Guzzle\Service\Client;
7-
use Guzzle\Http\Message\Request;
8-
use Intercom\Exception\IntercomException;
9-
106

117
class IntercomBasicAuthClient extends IntercomAbstractClient
128
{
139
/** @var array The required config variables for this type of client */
14-
private static $required = ['app_id', 'api_key', 'headers', 'service_description'];
10+
private static $required = [
11+
'app_id',
12+
'api_key',
13+
'headers',
14+
'service_description'
15+
];
1516

1617
/**
17-
* Creates a Basic Auth Client with the supplied configuration options
18+
* Creates a basic auth client with the supplied configuration options
1819
*
1920
* @param array $config
2021
* @return Client|IntercomBasicAuthClient
2122
*/
2223
public static function factory($config = [])
2324
{
24-
$default = [
25-
'service_description' => __DIR__ . '/Service/config/intercom.json',
26-
'headers' => [
27-
'Content-Type' => self::DEFAULT_CONTENT_TYPE,
28-
'Accept' => self::DEFAULT_ACCEPT_HEADER
29-
]
30-
];
31-
32-
$config = Collection::fromConfig($config, $default, static::$required);
3325
$client = new self();
3426

35-
$client->setDefaultOption('headers', $config->get('headers'));
36-
37-
$client->setDefaultOption(
38-
'auth',
39-
[
40-
$config->get('app_id'),
41-
$config->get('api_key'),
42-
'Basic'
43-
]
44-
);
45-
46-
$client->setDescription(static::getServiceDescriptionFromFile($config->get('service_description')));
47-
48-
$client->getEventDispatcher()->addListener('request.error', function(Event $event) {
49-
// Stop other events from firing when you override 401 responses
50-
$event->stopPropagation();
51-
52-
if ($event['response']->getStatusCode() > 400 && $event['response']->getStatusCode() < 600) {
53-
$e = IntercomException::factory($event['request'], $event['response']);
54-
$event['request']->setState(Request::STATE_ERROR, array('exception' => $e) + $event->toArray());
55-
throw $e;
56-
}
57-
});
27+
$config = Collection::fromConfig($config, $client->getDefaultConfig(), static::$required);
28+
29+
$client->configure($config);
30+
31+
$client->setBasicAuth($config->get('app_id'), $config->get('api_key'));
5832

5933
return $client;
6034
}

0 commit comments

Comments
 (0)