diff --git a/Tests/FactoryTest.php b/Tests/FactoryTest.php new file mode 100644 index 0000000..cb59cf1 --- /dev/null +++ b/Tests/FactoryTest.php @@ -0,0 +1,42 @@ +assertNull(\PHPFUI\ConstantContact\Client::getGuzzleFactory()); + $callable = [GrahamCampbell\GuzzleFactory\GuzzleFactory::class, 'make']; + \PHPFUI\ConstantContact\Client::setGuzzleFactory($callable); + $this->assertEquals($callable, \PHPFUI\ConstantContact\Client::getGuzzleFactory()); + \PHPFUI\ConstantContact\Client::setGuzzleFactory(null); + $client = new \PHPFUI\ConstantContact\Client('clientAPIKey', 'clientSecret', 'redirectURI'); + $guzzle = $client->getGuzzleClient('body', ['header1' => 'HEADER1']); + $this->assertTrue($guzzle instanceof \GuzzleHttp\Client); + $config = $guzzle->getConfig('body'); + $this->assertEquals('body', $config); + $config = $guzzle->getConfig('headers'); + $this->assertIsArray($config); + $this->assertArrayHasKey('header1', $config); + $this->assertContains('HEADER1', $config); + $this->assertArrayHasKey('Cache-Control', $config); + \PHPFUI\ConstantContact\Client::setGuzzleFactory($callable); + $guzzle = $client->getGuzzleClient('body', ['header1' => 'HEADER1']); + $this->assertTrue($guzzle instanceof \GuzzleHttp\Client); + $config = $guzzle->getConfig('body'); + $this->assertEquals('body', $config); + $config = $guzzle->getConfig('headers'); + $this->assertIsArray($config); + $this->assertArrayHasKey('header1', $config); + $this->assertContains('HEADER1', $config); + $this->assertArrayHasKey('Cache-Control', $config); + } + } diff --git a/Tests/HydrateTest.php b/Tests/HydrateTest.php index 05195ca..9200045 100644 --- a/Tests/HydrateTest.php +++ b/Tests/HydrateTest.php @@ -67,8 +67,8 @@ public function testHydration() : void $this->assertEquals('xxxx@mailinator.com', $contacts->contacts[0]->email_address->address); $this->assertEquals('xxxx@mailinator.com', $contacts->contacts[1]->email_address->address); $time = new \PHPFUI\ConstantContact\DateTime("2024-11-08T10:54:32Z"); - $this->assertEquals("{$time}", "{$contacts->contacts[0]->created_at}"); - $this->assertEquals("{$time}", "{$contacts->contacts[1]->created_at}"); + $this->assertEquals((string)$time, (string)$contacts->contacts[0]->created_at); + $this->assertEquals((string)$time, (string)$contacts->contacts[1]->created_at); $newJson = $contacts->getJSON(); $this->assertJson($newJson); $newDataArray = $contacts->toArray(); diff --git a/composer.json b/composer.json index 0a810d0..f2ef839 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "roave/security-advisories": "dev-latest", "friendsofphp/php-cs-fixer": "^3.0", "symfony/yaml": "^7.0", - "phpstan/phpstan": "^2.0" + "phpstan/phpstan": "^2.0", + "graham-campbell/guzzle-factory": "^7.0" }, "autoload": { "psr-4": {"PHPFUI\\ConstantContact\\": "src/ConstantContact/"} diff --git a/src/ConstantContact/Client.php b/src/ConstantContact/Client.php index fb8f105..db2c4e3 100644 --- a/src/ConstantContact/Client.php +++ b/src/ConstantContact/Client.php @@ -20,6 +20,8 @@ class Client private string $body = ''; + private static $guzzleFactory = null; + private \GuzzleHttp\HandlerStack $guzzleHandler; private string $host = ''; @@ -124,8 +126,7 @@ public function delete(string $url) : ?array { try { - $guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]); - $response = $guzzle->request('DELETE', $url); + $response = $this->getGuzzleClient()->request('DELETE', $url); return $this->process($response); } @@ -156,8 +157,7 @@ public function get(string $url, array $parameters) : ?array } } - $guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]); - $response = $guzzle->request('GET', $url); + $response = $this->getGuzzleClient()->request('GET', $url); return $this->process($response); } @@ -213,11 +213,35 @@ public function getBody() : string return $this->body; } + public function getGuzzleClient(string $body = '', array $headers = []) : \GuzzleHttp\Client + { + $config = [ + 'headers' => $this->getHeaders($headers), + 'handler' => $this->guzzleHandler, ]; + + if (\strlen($body)) + { + $config['body'] = $body; + } + + return self::$guzzleFactory ? \call_user_func(self::$guzzleFactory, $config) : new \GuzzleHttp\Client($config); + } + + public static function getGuzzleFactory() : ?callable + { + return self::$guzzleFactory; + } + public function getLastError() : string { return $this->lastError; } + public function getSessionCallback() : ?callable + { + return $this->sessionCallback; + } + public function getStatusCode() : int { return $this->statusCode; @@ -235,8 +259,7 @@ public function next() : array return []; } - $guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), 'handler' => $this->guzzleHandler, ]); - $response = $guzzle->request('GET', 'https://api.cc.email' . $this->next); + $response = $this->getGuzzleClient()->request('GET', 'https://api.cc.email' . $this->next); return $this->process($response); } @@ -257,10 +280,7 @@ public function post(string $url, array $parameters) : ?array try { $json = \json_encode($parameters['body'], JSON_PRETTY_PRINT); - $guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders(), - 'handler' => $this->guzzleHandler, - 'body' => $json, ]); - $response = $guzzle->request('POST', $url); + $response = $this->getGuzzleClient()->request('POST', $url); return $this->process($response); } @@ -281,7 +301,8 @@ public function put(string $url, array $parameters, string $method = 'PUT') : ?a try { $json = \json_encode($parameters['body'], JSON_PRETTY_PRINT); - $guzzle = new \GuzzleHttp\Client(['headers' => $this->getHeaders( + $guzzle = $this->getGuzzleClient( + $json, [ 'Connection' => 'keep-alive', 'Content-Length' => \strlen($json), @@ -289,10 +310,7 @@ public function put(string $url, array $parameters, string $method = 'PUT') : ?a 'Host' => $this->host, 'Accept' => '*/*' ] - ), - 'handler' => $this->guzzleHandler, - 'body' => $json, ]); - + ); $response = $guzzle->request($method, $url); return $this->process($response); @@ -341,6 +359,11 @@ public function removeScope(string $scope) : self return $this; } + public static function setGuzzleFactory(?callable $factory) : void + { + self::$guzzleFactory = $factory; + } + public function setHost(string $host) : self { $this->host = $host;