From db5edd1cad30675f49a3488675135ccbd056247b Mon Sep 17 00:00:00 2001 From: Jefersson Nathan Date: Mon, 31 Jan 2022 18:05:31 +0100 Subject: [PATCH] Remove hardcoded api usage Signed-off-by: Jefersson Nathan --- .../Factory/HttpAuthorizedClientFactory.php | 14 ++++++-------- src/Soql/FetchDataUtility.php | 6 ++++-- src/Soql/SoqlDriver.php | 18 +++++++++++------- tests/Soql/QueryBuilderTest.php | 7 ++++++- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/Soql/Factory/HttpAuthorizedClientFactory.php b/src/Soql/Factory/HttpAuthorizedClientFactory.php index f33a29a..b97b6d9 100644 --- a/src/Soql/Factory/HttpAuthorizedClientFactory.php +++ b/src/Soql/Factory/HttpAuthorizedClientFactory.php @@ -11,20 +11,18 @@ final class HttpAuthorizedClientFactory implements AuthorizedClientFactory { - private AccessTokenFactory $accessTokenFactory; - - private string $salesforceInstance; - - public function __construct(AccessTokenFactory $accessTokenFactory, string $salesforceInstance) - { - $this->accessTokenFactory = $accessTokenFactory; - $this->salesforceInstance = $salesforceInstance; + public function __construct( + private AccessTokenFactory $accessTokenFactory, + private string $salesforceInstance, + private string $apiVersion + ) { } public function __invoke(): ClientInterface { return new Client([ 'base_uri' => $this->salesforceInstance, + 'apiVersion' => $this->apiVersion, 'headers' => [ 'Authorization' => sprintf('Bearer %s', $this->accessTokenFactory->__invoke()), 'X-PrettyPrint' => '1', diff --git a/src/Soql/FetchDataUtility.php b/src/Soql/FetchDataUtility.php index fc14112..4a50da0 100644 --- a/src/Soql/FetchDataUtility.php +++ b/src/Soql/FetchDataUtility.php @@ -10,6 +10,7 @@ use function assert; use function current; use function json_decode; +use function sprintf; use const JSON_THROW_ON_ERROR; @@ -28,8 +29,9 @@ public function fetchAll(ClientInterface $client, string $statement): Payload private function doFetch(ClientInterface $client, string $statement) { - // TODO: how to deal with different versions? Maybe `driverOptions`? - $request = $client->request('GET', '/services/data/v20.0/query?q=' . $statement); + $apiVersion = $client->getConfig('apiVersion'); + + $request = $client->request('GET', sprintf('/services/data/%s/query?q=%s', $apiVersion, $statement)); return json_decode($request->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); } diff --git a/src/Soql/SoqlDriver.php b/src/Soql/SoqlDriver.php index a3b381f..08a132e 100644 --- a/src/Soql/SoqlDriver.php +++ b/src/Soql/SoqlDriver.php @@ -79,13 +79,17 @@ private function getAuthorizedClientFactory(array $params): AuthorizedClientFact return $params['authorizedClientFactory']; } - return new HttpAuthorizedClientFactory(new HttpAccessTokenFactory( + return new HttpAuthorizedClientFactory( + new HttpAccessTokenFactory( + $params['salesforceInstance'], + $params['apiVersion'], + $params['consumerKey'], + $params['consumerSecret'], + $params['user'], + $params['password'] + ), $params['salesforceInstance'], - $params['apiVersion'], - $params['consumerKey'], - $params['consumerSecret'], - $params['user'], - $params['password'] - ), $params['salesforceInstance']); + $params['apiVersion'] + ); } } diff --git a/tests/Soql/QueryBuilderTest.php b/tests/Soql/QueryBuilderTest.php index 57a32ef..a991399 100644 --- a/tests/Soql/QueryBuilderTest.php +++ b/tests/Soql/QueryBuilderTest.php @@ -127,9 +127,14 @@ public function fetch_should_bind_values() : void ->method('getHttpClient') ->willReturn($httpClient = $this->createMock(Client::class)); + $httpClient->expects(self::once()) + ->method('getConfig') + ->with('apiVersion') + ->willReturn('v80.0'); + $httpClient->expects(self::once()) ->method('request') - ->with('GET', '/services/data/v20.0/query?q=SELECT Id, (SELECT Name FROM Contact WHERE Id = \'123\') FROM Account LIMIT 1') + ->with('GET', '/services/data/v80.0/query?q=SELECT Id, (SELECT Name FROM Contact WHERE Id = \'123\') FROM Account LIMIT 1') ->willReturn($response = $this->createMock(ResponseInterface::class)); $response->expects(self::once())