AlgoliaFactory::makeSearchableObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace leinonen\Yii2Algolia;
4
5
use AlgoliaSearch\Client;
6
use leinonen\Yii2Algolia\ActiveRecord\ActiveQueryChunker;
7
use leinonen\Yii2Algolia\ActiveRecord\ActiveRecordFactory;
8
9
class AlgoliaFactory
10
{
11
    /**
12
     * Makes a new Algolia Client.
13
     *
14
     * @param AlgoliaConfig $config
15
     *
16 29
     * @return AlgoliaManager
17
     *
18 29
     * @throws \Exception
19 29
     */
20 29
    public function make(AlgoliaConfig $config)
21 29
    {
22 29
        return new AlgoliaManager(
23 29
            new Client(
24
                $config->getApplicationId(),
25
                $config->getApiKey(),
26
                $config->getHostsArray(),
27
                $config->getOptions()
28
            ),
29
            new ActiveRecordFactory(),
30
            new ActiveQueryChunker()
31
        );
32
    }
33 2
34
    /**
35 2
     * Returns a new instance for given class which must implement the SearchableInterface.
36 1
     *
37
     * @param string $className
38
     *
39 1
     * @return SearchableInterface
40
     *
41
     * @throws \InvalidArgumentException
42
     */
43
    public function makeSearchableObject($className)
44
    {
45
        if (! (new \ReflectionClass($className))->implementsInterface(SearchableInterface::class)) {
46
            throw new \InvalidArgumentException("Cannot initiate a class ({$className}) which doesn't implement leinonen\\Yii2Algolia\\SearchableInterface");
47
        }
48
49
        return new $className();
50
    }
51
}
52