Skip to content

Commit d7a730c

Browse files
FFWEB-2628: add config to AdapterFactory
1 parent 46e6f8c commit d7a730c

5 files changed

Lines changed: 72 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# Changelog
2+
## Unreleased
3+
### Add
4+
AdapterFactory
5+
- add config
6+
27
## [v0.9.4] - 2022.08.25
38
### Changed
49
Compatibility

src/Resource/AdapterFactory.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,37 @@ class AdapterFactory
1616
/** @var string */
1717
private $version;
1818

19-
public function __construct(ClientBuilder $clientBuilder, string $version = Version::NG)
20-
{
19+
/** @var array */
20+
private $config;
21+
22+
public function __construct(
23+
ClientBuilder $clientBuilder,
24+
string $version = Version::NG,
25+
string $apiVersion = 'v4'
26+
) {
2127
$this->clientBuilder = $clientBuilder;
2228
$this->version = $version;
29+
$this->config = [
30+
'api_version' => $apiVersion,
31+
];
2332
}
2433

2534
public function getImportAdapter(): Import
2635
{
2736
$class = $this->getAdapterClass(Import::class, $this->version);
28-
return new $class($this->getClient());
37+
return new $class($this->getClient(), $this->config);
2938
}
3039

3140
public function getSearchAdapter(): Search
3241
{
3342
$class = $this->getAdapterClass(Search::class, $this->version);
34-
return new $class($this->getClient());
43+
return new $class($this->getClient(), $this->config);
3544
}
3645

3746
public function getTrackingAdapter(): Tracking
3847
{
3948
$class = $this->getAdapterClass(Tracking::class, $this->version);
40-
return new $class($this->getClient());
49+
return new $class($this->getClient(), $this->config);
4150
}
4251

4352
private function getAdapterClass(string $resource, string $version): string

src/Resource/NG/ImportAdapter.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,34 @@ class ImportAdapter implements Import
1212
/** @var ClientInterface */
1313
private $client;
1414

15-
public function __construct(ClientInterface $client)
16-
{
15+
/** @var array */
16+
private $config;
17+
18+
public function __construct(
19+
ClientInterface $client,
20+
array $config = []
21+
) {
1722
$this->client = $client;
23+
$this->config = $config;
1824
}
1925

2026
public function import(string $channel, string $type, array $params = []): array
2127
{
22-
$params = ['channel' => $channel] + $params;
23-
$response = $this->client->request('POST', "rest/v4/import/{$type}", ['query' => $params]);
28+
$params = ['channel' => $channel] + $params;
29+
$apiVersion = $this->getApiVersion();
30+
$response = $this->client->request('POST', "rest/{$apiVersion}/import/{$type}", ['query' => $params]);
2431
return (array) json_decode((string) $response->getBody(), true);
2532
}
2633

2734
public function running(string $channel): bool
2835
{
29-
$response = $this->client->request('GET', 'rest/v4/import/running', ['query' => ['channel' => $channel]]);
36+
$apiVersion = $this->getApiVersion();
37+
$response = $this->client->request('GET', "rest/{$apiVersion}/import/running", ['query' => ['channel' => $channel]]);
3038
return (bool) json_decode((string) $response->getBody(), true);
3139
}
40+
41+
private function getApiVersion(): string
42+
{
43+
return (string) $this->config['api_version'] ?? 'v4';
44+
}
3245
}

src/Resource/NG/SearchAdapter.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,35 @@ class SearchAdapter implements Search
1212
/** @var ClientInterface */
1313
private $client;
1414

15-
public function __construct(ClientInterface $client)
16-
{
15+
/** @var array */
16+
private $config;
17+
18+
public function __construct(
19+
ClientInterface $client,
20+
array $config = []
21+
) {
1722
$this->client = $client;
23+
$this->config = $config;
1824
}
1925

2026
public function search(string $channel, string $query, array $params = []): array
2127
{
22-
$params = ['query' => $query] + $params;
23-
$response = $this->client->request('GET', "rest/v4/search/{$channel}", ['query' => $params]);
28+
$params = ['query' => $query] + $params;
29+
$apiVersion = $this->getApiVersion();
30+
$response = $this->client->request('GET', "rest/{$apiVersion}/search/{$channel}", ['query' => $params]);
2431
return (array) json_decode((string) $response->getBody(), true);
2532
}
2633

2734
public function suggest(string $channel, string $query, array $params = []): array
2835
{
29-
$params = ['query' => $query] + $params;
30-
$response = $this->client->request('GET', "rest/v4/suggest/{$channel}", ['query' => $params]);
36+
$params = ['query' => $query] + $params;
37+
$apiVersion = $this->getApiVersion();
38+
$response = $this->client->request('GET', "rest/{$apiVersion}/suggest/{$channel}", ['query' => $params]);
3139
return (array) json_decode((string) $response->getBody(), true);
3240
}
41+
42+
private function getApiVersion(): string
43+
{
44+
return (string) $this->config['api_version'] ?? 'v4';
45+
}
3346
}

src/Resource/NG/TrackingAdapter.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,27 @@ class TrackingAdapter implements Tracking
1212
/** @var ClientInterface */
1313
private $client;
1414

15-
public function __construct(ClientInterface $client)
16-
{
15+
/** @var array */
16+
private $config;
17+
18+
public function __construct(
19+
ClientInterface $client,
20+
array $config = []
21+
) {
1722
$this->client = $client;
23+
$this->config = $config;
1824
}
1925

2026
public function track(string $channel, string $event, array $eventData = []): array
2127
{
22-
$params = ['body' => json_encode($eventData), 'headers' => ['Content-Type' => 'application/json']];
23-
$response = $this->client->request('POST', "rest/v4/track/{$channel}/{$event}", $params);
28+
$params = ['body' => json_encode($eventData), 'headers' => ['Content-Type' => 'application/json']];
29+
$apiVersion = $this->getApiVersion();
30+
$response = $this->client->request('POST', "rest/{$apiVersion}/track/{$channel}/{$event}", $params);
2431
return json_decode((string) $response->getBody(), true) ?? [];
2532
}
33+
34+
private function getApiVersion(): string
35+
{
36+
return (string) $this->config['api_version'] ?? 'v4';
37+
}
2638
}

0 commit comments

Comments
 (0)