Completed
Pull Request — master (#13)
by Vadim
06:54
created

Polly   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 45
ccs 0
cts 30
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B read() 0 28 2
A initOptions() 0 4 1
1
<?php
2
3
namespace AudioManager\Adapter;
4
5
use AudioManager\Adapter\Options\OptionsInterface;
6
use Aws\Polly\PollyClient;
7
use AudioManager\Adapter\Options\Polly as Options;
8
9
/**
10
 * Class Polly
11
 * @package AudioManager\Adapter
12
 * @method Options getOptions()
13
 */
14
class Polly extends AbstractAdapter implements AdapterInterface
15
{
16
    /**
17
     * Get audio for text from adapter
18
     * @param string $text
19
     * @return mixed
20
     */
21
    public function read($text)
22
    {
23
        $initializeOptions = $this->getOptions()->getInitializeOptions();
24
        $pollyClient = new PollyClient([
25
            'version' => $initializeOptions->getVersion(),
26
            'region' => $initializeOptions->getRegion(),
27
            'credentials' => [
28
                'key' => $initializeOptions->getCredentials()->getKey(),
29
                'secret' => $initializeOptions->getCredentials()->getSecret()
30
            ]
31
        ]);
32
33
        $synthesizeOptions = [
34
            'OutputFormat' => $this->getOptions()->getOutputFormat(),
35
            'Text' => $text,
36
            'TextType' => $this->getOptions()->getTextType(),
37
            'VoiceId' => $this->getOptions()->getVoiceId(),
38
            'SampleRate' => $this->getOptions()->getSampleRate(),
39
        ];
40
        if (!empty($this->getOptions()->getLexiconNames())) {
41
            $synthesizeOptions['LexiconNames'] = $this->getOptions()->getLexiconNames();
42
        }
43
44
        $awsResult = $pollyClient->synthesizeSpeech($synthesizeOptions);
45
        $this->setHeaders($awsResult->get('@metadata'));
46
        $stream = $awsResult->get('AudioStream');
47
        return $stream->getContents();
48
    }
49
50
    /**
51
     * Init options
52
     * @return OptionsInterface
53
     */
54
    protected function initOptions()
55
    {
56
        return new Options();
57
    }
58
}
59