|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace leinonen\Yii2Algolia; |
|
4
|
|
|
|
|
5
|
|
|
use AlgoliaSearch\Client; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
|
|
8
|
|
|
class AlgoliaFactory |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Makes a new Algolia Client. |
|
12
|
|
|
* |
|
13
|
|
|
* @param array $config |
|
14
|
|
|
* |
|
15
|
|
|
* @return Client |
|
16
|
|
|
*/ |
|
17
|
6 |
|
public function make(array $config) |
|
18
|
|
|
{ |
|
19
|
6 |
|
list($applicationId, $apiKey, $hostsArray, $options) = $this->getConfig($config); |
|
20
|
|
|
|
|
21
|
4 |
|
return $this->createClient($applicationId, $apiKey, $hostsArray, $options); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Returns a new instance for given class which must implement the SearchableInterface. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $className |
|
28
|
|
|
* |
|
29
|
|
|
* @return SearchableInterface |
|
30
|
|
|
*/ |
|
31
|
2 |
|
public function makeSearchableObject($className) |
|
32
|
|
|
{ |
|
33
|
2 |
|
if (! (new \ReflectionClass($className))->implementsInterface(SearchableInterface::class)) { |
|
34
|
1 |
|
throw new \InvalidArgumentException("Cannot initiate a class ({$className}) which doesn't implement leinonen\\Yii2Algolia\\SearchableInterface"); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
return new $className(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Creates an new Algolia Client. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $applicationId The application ID you have in your admin interface |
|
44
|
|
|
* @param string $apiKey A valid API key for the service |
|
45
|
|
|
* @param null|array $hostsArray The list of hosts that you have received for the service |
|
46
|
|
|
* @param array $options |
|
47
|
|
|
* |
|
48
|
|
|
* @return Client |
|
49
|
|
|
*/ |
|
50
|
4 |
|
protected function createClient($applicationId, $apiKey, $hostsArray = null, $options = []) |
|
51
|
|
|
{ |
|
52
|
4 |
|
return new Client($applicationId, $apiKey, $hostsArray, $options); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the configurations for the Algolia Client in correct format. |
|
57
|
|
|
* @param array $config |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
6 |
|
protected function getConfig(array $config) |
|
62
|
|
|
{ |
|
63
|
6 |
|
if (! array_key_exists('applicationId', $config) || ! array_key_exists('apiKey', $config)) { |
|
64
|
2 |
|
throw new InvalidArgumentException('Configuration keys applicationId and apiKey are required'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
$applicationId = $config['applicationId']; |
|
68
|
4 |
|
$apiKey = $config['apiKey']; |
|
69
|
4 |
|
$hostsArray = null; |
|
70
|
4 |
|
$options = []; |
|
71
|
|
|
|
|
72
|
4 |
|
if (array_key_exists('hostsArray', $config)) { |
|
73
|
3 |
|
$hostsArray = $config['hostsArray']; |
|
74
|
3 |
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
if (array_key_exists('options', $config)) { |
|
77
|
3 |
|
$options = $config['options']; |
|
78
|
3 |
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
return [$applicationId, $apiKey, $hostsArray, $options]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|