33
44use InvalidArgumentException ;
55use Guzzle \Service \Client ;
6+ use Guzzle \Common \Event ;
7+ use Guzzle \Http \Message \Request ;
68use Guzzle \Service \Description \ServiceDescription ;
9+ use Intercom \Exception \IntercomException ;
10+ use Guzzle \Common \Collection ;
711
812abstract 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}
0 commit comments