Skip to content

Commit

Permalink
Remove hardcoded api usage
Browse files Browse the repository at this point in the history
Signed-off-by: Jefersson Nathan <[email protected]>
  • Loading branch information
malukenho committed Jan 31, 2022
1 parent 71e6a5b commit db5edd1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
14 changes: 6 additions & 8 deletions src/Soql/Factory/HttpAuthorizedClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions src/Soql/FetchDataUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function assert;
use function current;
use function json_decode;
use function sprintf;

use const JSON_THROW_ON_ERROR;

Expand All @@ -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);
}
Expand Down
18 changes: 11 additions & 7 deletions src/Soql/SoqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
);
}
}
7 changes: 6 additions & 1 deletion tests/Soql/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit db5edd1

Please sign in to comment.