Completed
Push — master ( 24bafe...e75277 )
by Juuso
05:45
created

AlgoliaFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 9
c 4
b 1
f 1
lcom 0
cbo 1
dl 0
loc 75
ccs 23
cts 23
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 6 1
A makeSearchableObject() 0 8 2
A createClient() 0 4 1
B getConfig() 0 21 5
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