Completed
Branch master (995814)
by Juuso
11:40 queued 06:36
created

AlgoliaFactory::getConfig()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 21
ccs 14
cts 14
cp 1
rs 8.7624
cc 5
eloc 12
nc 5
nop 1
crap 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 AlgoliaConfig $config
14
     *
15
     * @return Client
16
     */
17 7
    public function make(AlgoliaConfig $config)
18
    {
19 7
        return new Client(
20
            $config->getApplicationId(),
21 5
            $config->getApiKey(),
22
            $config->getHostsArray(),
23
            $config->getOptions()
24
        );
25
    }
26
27
    /**
28
     * Returns a new instance for given class which must implement the SearchableInterface.
29
     *
30
     * @param string $className
31 3
     *
32
     * @return SearchableInterface
33 3
     */
34 1
    public function makeSearchableObject($className)
35
    {
36
        if (! (new \ReflectionClass($className))->implementsInterface(SearchableInterface::class)) {
37 2
            throw new \InvalidArgumentException("Cannot initiate a class ({$className}) which doesn't implement leinonen\\Yii2Algolia\\SearchableInterface");
38
        }
39
40
        return new $className();
41
    }
42
}
43