Skip to content

Commit

Permalink
Allow for a GuzzleFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
phpfui committed Nov 12, 2024
1 parent 5bff968 commit ff2494c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 18 deletions.
42 changes: 42 additions & 0 deletions Tests/FactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* This file is part of the PHPFUI\ConstantContact package
*
* (c) Bruce Wells
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source
* code
*/
class FactoryTest extends \PHPUnit\Framework\TestCase
{
public function testSetFactory() : void
{
$this->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);
}
}
4 changes: 2 additions & 2 deletions Tests/HydrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public function testHydration() : void
$this->assertEquals('[email protected]', $contacts->contacts[0]->email_address->address);
$this->assertEquals('[email protected]', $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();
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/"}
Expand Down
53 changes: 38 additions & 15 deletions src/ConstantContact/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Client

private string $body = '';

private static $guzzleFactory = null;

private \GuzzleHttp\HandlerStack $guzzleHandler;

private string $host = '';
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -281,18 +301,16 @@ 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),
'Accept-Encoding' => 'gzip, deflate',
'Host' => $this->host,
'Accept' => '*/*'
]
),
'handler' => $this->guzzleHandler,
'body' => $json, ]);

);
$response = $guzzle->request($method, $url);

return $this->process($response);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ff2494c

Please sign in to comment.