Completed
Push — master ( ead051...fb750b )
by Rigel Kent
19:53 queued 13s
created

PollyConverter::getCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Cion\TextToSpeech\Converters;
4
5
use Aws\Polly\PollyClient;
6
use Cion\TextToSpeech\Contracts\Converter;
7
use Cion\TextToSpeech\Traits\Sourceable;
8
use Cion\TextToSpeech\Traits\Storable;
9
use Illuminate\Support\Arr;
10
11
class PollyConverter implements Converter
12
{
13
    use Storable, Sourceable;
14
15
    /**
16
     * Client instance of Polly.
17
     *
18
     * @var \Aws\Polly\PollyClient
19
     */
20
    protected $client;
21
22
    /**
23
     * Construct converter.
24
     *
25
     * @param PollyClient $client
26
     */
27
    public function __construct(PollyClient $client)
28
    {
29
        $this->client = $client;
30
    }
31
32
    
33
34
    /**
35
     * Get the Polly Client.
36
     *
37
     * @return \Aws\Polly\PollyClient
38
     */
39
    public function getClient(): PollyClient
40
    {
41
        return $this->client;
42
    }
43
44
    /**
45
     * Converts the text to speech.
46
     *
47
     * @param string $data
48
     * @param array $options
49
     * @return string
50
     */
51
    public function convert(string $data, array $options = null)
52
    {
53
        $result = $this->client->synthesizeSpeech([
54
            'VoiceId'      => $this->voice($options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 51 can also be of type null; however, Cion\TextToSpeech\Conver...PollyConverter::voice() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
55
            'OutputFormat' => $this->format($options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 51 can also be of type null; however, Cion\TextToSpeech\Conver...ollyConverter::format() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
56
            'Text'         => $this->getTextFromSource($data),
57
        ]);
58
59
        // Store audio file to disk
60
        return $this->store(
61
            $this->getTextFromSource($data),
62
            $this->getResultContent($result)
63
        );
64
    }
65
66
    /**
67
     * Get the text to speech voice ID.
68
     *
69
     * @param  array $options
70
     * @return string
71
     */
72
    protected function voice($options)
73
    {
74
        $default = config('tts.services.polly.voice_id', 'Amy');
75
76
        return Arr::get($options, 'voice', $default);
77
    }
78
79
    /**
80
     * Get the audio format.
81
     *
82
     * @param  array $options
83
     * @return string
84
     */
85
    protected function format($options)
86
    {
87
        $default = config('tts.output_format', 'mp3');
88
89
        return Arr::get($options, 'format', $default);
90
    }
91
92
    /**
93
     * Get the content of the result from AWS Polly.
94
     *
95
     * @param mixed $result
96
     * @return mixed
97
     */
98
    protected function getResultContent($result)
99
    {
100
        return $result->get('AudioStream')->getContents();
101
    }
102
}
103