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

Polly::read()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
ccs 0
cts 26
cp 0
rs 8.8571
c 1
b 0
f 0
cc 2
eloc 20
nc 2
nop 1
crap 6
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