Skip to content

Commit d60e743

Browse files
Moved ClusterHosts inside ClientConfig and restore 2 constructors
1 parent 1c66a62 commit d60e743

File tree

6 files changed

+30
-57
lines changed

6 files changed

+30
-57
lines changed

src/Analytics.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ public function __construct(ApiWrapper $api)
2020
$this->api = $api;
2121
}
2222

23-
public static function create($appId, $apiKey)
23+
public static function create($appId = null, $apiKey = null)
2424
{
25-
$config = new ClientConfig(array(
26-
'appId' => $appId,
27-
'apiKey' => $apiKey,
28-
));
25+
$config = new ClientConfig($appId, $apiKey);
26+
$config->setHosts(ClusterHosts::createForAnalytics());
2927

3028
return static::createWithConfig($config);
3129
}
@@ -34,8 +32,7 @@ public static function createWithConfig(ClientConfig $config)
3432
{
3533
$apiWrapper = new ApiWrapper(
3634
Config::getHttpClient(),
37-
$config,
38-
ClusterHosts::createForAnalytics()
35+
$config
3936
);
4037

4138
return new static($apiWrapper);

src/Client.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,13 @@ public static function get()
3636
return static::$client;
3737
}
3838

39-
public static function create($appId = null, $apiKey = null, ClientConfig $config = null)
39+
public static function create($appId = null, $apiKey = null)
4040
{
41-
if (null === $config) {
42-
$config = new ClientConfig();
43-
}
44-
45-
if (null !== $appId) {
46-
$config->setAppId($appId);
47-
}
48-
49-
if (null !== $apiKey) {
50-
$config->setApiKey($apiKey);
51-
}
52-
53-
$hosts = $config->getHosts();
54-
if (!$hosts) {
55-
$hosts = ClusterHosts::createFromAppId($config->getAppId());
56-
} elseif (is_string($hosts)) {
57-
$hosts = new ClusterHosts(array($hosts));
58-
} elseif (is_array($hosts)) {
59-
$hosts = new ClusterHosts($hosts);
60-
}
41+
$config = new ClientConfig($appId, $apiKey);
6142

6243
$apiWrapper = new ApiWrapper(
6344
Config::getHttpClient(),
64-
$config,
65-
$hosts
45+
$config
6646
);
6747

6848
return new static($apiWrapper);

src/Internals/ApiWrapper.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ class ApiWrapper
2323
*/
2424
private $config;
2525

26-
/**
27-
* @var ClusterHosts
28-
*/
29-
private $clusterHosts;
30-
3126
/**
3227
* @var RequestOptionsFactory
3328
*/
@@ -36,12 +31,10 @@ class ApiWrapper
3631
public function __construct(
3732
HttpClientInterface $http,
3833
ClientConfig $config,
39-
ClusterHosts $clusterHosts,
4034
RequestOptionsFactory $RqstOptsFactory = null
4135
) {
4236
$this->http = $http;
4337
$this->config = $config;
44-
$this->clusterHosts = $clusterHosts;
4538
$this->requestOptionsFactory = $RqstOptsFactory ? $RqstOptsFactory : new RequestOptionsFactory($config);
4639
}
4740

@@ -57,7 +50,7 @@ public function read($method, $path, $requestOptions = array(), $defaults = arra
5750
$method,
5851
$path,
5952
$requestOptions,
60-
$this->clusterHosts->read(),
53+
$this->config->getHosts()->read(),
6154
$requestOptions->getReadTimeout()
6255
);
6356
}
@@ -75,7 +68,7 @@ public function write($method, $path, $data = array(), $requestOptions = array()
7568
$method,
7669
$path,
7770
$requestOptions,
78-
$this->clusterHosts->write(),
71+
$this->config->getHosts()->write(),
7972
$requestOptions->getWriteTimeout(),
8073
$data
8174
);
@@ -86,7 +79,7 @@ public function send($method, $path, $requestOptions = array(), $hosts = null)
8679
$requestOptions = $this->requestOptionsFactory->create($requestOptions);
8780

8881
if (null === $hosts) {
89-
$hosts = $this->clusterHosts->write();
82+
$hosts = $this->config->getHosts()->write();
9083
} elseif (!is_array($hosts)) {
9184
$hosts = array($hosts);
9285
}
@@ -131,7 +124,7 @@ private function request($method, $path, RequestOptions $requestOptions, $hosts,
131124
Debug::handle("The host [$host] failed, retrying with another host.");
132125
}
133126

134-
$this->clusterHosts->failed($host);
127+
$this->config->getHosts()->failed($host);
135128
} catch (BadRequestException $e) {
136129
if (Debug::isEnabled()) {
137130
Debug::handle("The following request returned a 4xx error: ", $request);

src/Places.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ final class Places
1212
/**
1313
* @var ApiWrapper
1414
*/
15-
private $apiWrapper;
15+
private $api;
1616

1717
public function __construct(ApiWrapper $apiWrapper)
1818
{
19-
$this->apiWrapper = $apiWrapper;
19+
$this->api = $apiWrapper;
2020
}
2121

2222
public static function create($appId = null, $apiKey = null)
2323
{
24-
$config = new ClientConfig(array(
25-
'appId' => $appId,
26-
'apiKey' => $apiKey,
27-
));
24+
$config = new ClientConfig($appId, $apiKey);
25+
$config->setHosts(ClusterHosts::createForPlaces());
2826

2927
return static::createWithConfig($config);
3028
}
@@ -33,8 +31,7 @@ public static function createWithConfig(ClientConfig $config)
3331
{
3432
$apiWrapper = new ApiWrapper(
3533
Config::getHttpClient(),
36-
$config,
37-
ClusterHosts::createForPlaces()
34+
$config
3835
);
3936

4037
return new static($apiWrapper);

src/Support/ClientConfig.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,36 @@
22

33
namespace Algolia\AlgoliaSearch\Support;
44

5+
use Algolia\AlgoliaSearch\Internals\ClusterHosts;
6+
57
class ClientConfig
68
{
79
private $config;
810

911
public function __construct($appId = null, $apiKey = null)
1012
{
11-
$config = array();
13+
$config = $this->getDefaultConfig();
14+
1215
if (null !== $appId) {
1316
$config['appId'] = $appId;
1417
}
1518
if (null !== $apiKey) {
1619
$config['apiKey'] = $apiKey;
1720
}
1821

19-
$this->config = $config + $this->getDefaultConfig();
22+
if (null === $config['hosts']) {
23+
$config['hosts'] = ClusterHosts::createFromAppId($config['appId']);
24+
}
25+
26+
$this->config = $config;
2027
}
2128

2229
private function getDefaultConfig()
2330
{
2431
return array(
2532
'appId' => getenv('ALGOLIA_APP_ID'),
2633
'apiKey' => getenv('ALGOLIA_API_KEY'),
27-
'hosts' => array(),
34+
'hosts' => null,
2835
'waitTaskRetry' => Config::$waitTaskRetry,
2936
'readTimeout' => Config::getReadTimeout(),
3037
'writeTimeout' => Config::getWriteTimeout(),
@@ -59,7 +66,7 @@ public function getHosts()
5966
return $this->config['hosts'];
6067
}
6168

62-
public function setHosts($hosts)
69+
public function setHosts(ClusterHosts $hosts)
6370
{
6471
$this->config['hosts'] = $hosts;
6572
return $this;

tests/Unit/RequestOptionsFactoryTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ class RequestOptionsFactoryTest extends TestCase
1414

1515
public function setUp()
1616
{
17-
$this->factory = new RequestOptionsFactory(new ClientConfig(array(
18-
'appId' => 'Algolia-Id',
19-
'apiKey' => 'Algolia-Key',
20-
)));
17+
$this->factory = new RequestOptionsFactory(
18+
new ClientConfig('Algolia-Id', 'Algolia-Key'
19+
));
2120
}
2221

2322
/**

0 commit comments

Comments
 (0)