VoiceAdapterFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
namespace App\Actions\Voice;
4
5
use AudioManager\Adapter\Google;
6
use AudioManager\Adapter\Ivona;
7
use Staticus\Config\ConfigInterface;
8
use Staticus\Config\Config;
9
use Staticus\Exceptions\ErrorException;
10
11
class VoiceAdapterFactory
12
{
13
    const VOICE_PROVIDER_GOOGLE = 'google';
14
    const VOICE_PROVIDER_IVONA = 'ivona';
15
    /**
16
     * @var ConfigInterface|Config
17
     */
18
    protected $config;
19
20
    public function __construct(ConfigInterface $config)
21
    {
22
        $this->config = $config;
23
    }
24
25
    /**
26
     * @return \AudioManager\Adapter\AdapterInterface
27
     * @throws RuntimeException
28
     */
29
    public function __invoke()
30
    {
31
        $adapterName = strtolower($this->config->get('voice.provider', self::VOICE_PROVIDER_GOOGLE));
32
        switch ($adapterName) {
33
            case self::VOICE_PROVIDER_GOOGLE:
34
                $adapter = new Google();
35
                $adapter->getOptions()->setLanguage('en');
36
                $adapter->getOptions()->setEncoding('UTF-8');
37
                break;
38
            case self::VOICE_PROVIDER_IVONA:
39
                $secretKey = $this->config->get('voice.' . $adapterName . '.secret_key', '');
40
                $accessKey = $this->config->get('voice.' . $adapterName . '.access_key', '');
41
                $adapter = new Ivona();
42
                $adapter->getOptions()->setSecretKey($secretKey);
43
                $adapter->getOptions()->setAccessKey($accessKey);
44
                break;
45
            default:
46
                throw new ErrorException('Not implemented functionality for voice provider: ' . $adapterName);
47
        }
48
49
        return $adapter;
50
    }
51
}