Completed
Push — master ( 26e407...24bafe )
by Juuso
04:14
created

AlgoliaFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 78.95%

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 15
cts 19
cp 0.7895
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 3
    public function make(array $config)
18
    {
19 3
        list($applicationId, $apiKey, $hostsArray, $options) = $this->getConfig($config);
20
21 1
        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
    public function makeSearchableObject($className)
32
    {
33
        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 1
37
        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 3
     * @param null|array $hostsArray The list of hosts that you have received for the service
46
     * @param array $options
47 3
     *
48 2
     * @return Client
49
     */
50
    protected function createClient($applicationId, $apiKey, $hostsArray = null, $options = [])
51 1
    {
52 1
        return new Client($applicationId, $apiKey, $hostsArray, $options);
53 1
    }
54 1
55
    /**
56 1
     * Returns the configurations for the Algolia Client in correct format.
57
     * @param array $config
58
     *
59
     * @return array
60 1
     */
61
    protected function getConfig(array $config)
62
    {
63
        if (! array_key_exists('applicationId', $config) || ! array_key_exists('apiKey', $config)) {
64 1
            throw new InvalidArgumentException('Configuration keys applicationId and apiKey are required');
65
        }
66
67
        $applicationId = $config['applicationId'];
68
        $apiKey = $config['apiKey'];
69
        $hostsArray = null;
70
        $options = [];
71
72
        if (array_key_exists('hostsArray', $config)) {
73
            $hostsArray = $config['hostsArray'];
74
        }
75
76
        if (array_key_exists('options', $config)) {
77
            $options = $config['options'];
78
        }
79
80
        return [$applicationId, $apiKey, $hostsArray, $options];
81
    }
82
}
83