Completed
Push — master ( 4b1d42...c7d7d2 )
by Vadim
05:23
created

Polly   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 6
dl 0
loc 53
ccs 0
cts 37
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B read() 0 28 2
A initOptions() 0 4 1
A getHeaders() 0 7 2
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
    public function getHeaders()
60
    {
61
        $headers = parent::getHeaders();
62
        return [
63
            'http_code' => isset($headers['statusCode']) ? $headers['statusCode']: 500,
64
        ];
65
    }
66
}
67