Completed
Push — master ( b258b1...3deeb7 )
by Rigel Kent
01:30 queued 13s
created

PollyConverter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 176
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getClient() 0 4 1
A convert() 0 26 3
A synthesizeSpeech() 0 27 3
A mergeOutputs() 0 10 2
A isTextAboveLimit() 0 4 1
A getChunkText() 0 4 1
A voice() 0 6 1
A format() 0 6 1
A getResultContent() 0 4 1
1
<?php
2
3
namespace Cion\TextToSpeech\Converters;
4
5
use Aws\Polly\PollyClient;
6
use Aws\Result;
7
use Cion\TextToSpeech\Contracts\Converter;
8
use Cion\TextToSpeech\Traits\Sourceable;
9
use Cion\TextToSpeech\Traits\Storable;
10
use Illuminate\Support\Arr;
11
12
class PollyConverter implements Converter
13
{
14
    use Storable, Sourceable;
15
16
    /**
17
     * Client instance of Polly.
18
     *
19
     * @var \Aws\Polly\PollyClient
20
     */
21
    protected $client;
22
23
    /**
24
     * Construct converter.
25
     *
26
     * @param PollyClient $client
27
     */
28
    public function __construct(PollyClient $client)
29
    {
30
        $this->client = $client;
31
    }
32
33
    /**
34
     * Get the Polly Client.
35
     *
36
     * @return \Aws\Polly\PollyClient
37
     */
38
    public function getClient(): PollyClient
39
    {
40
        return $this->client;
41
    }
42
43
    /**
44
     * Converts the text to speech.
45
     *
46
     * @param string $data
47
     * @param array $options
48
     * @return string
49
     */
50
    public function convert(string $data, array $options = null)
51
    {
52
        $text = $this->getTextFromSource($data);
53
54
        if ($this->isTextAboveLimit($text))
55
        {
56
            $text = $this->getChunkText($text);
57
        }
58
59
        $result = $this->synthesizeSpeech($text, $options);
60
61
        if ($result instanceof Result)
62
        {
63
            // Store audio file to disk
64
            return $this->store(
65
                $this->getTextFromSource($data),
66
                $this->getResultContent($result)
67
            );
68
        }
69
70
        return $this->store(
71
            $this->getTextFromSource($data),
72
            $this->mergeOutputs($result)
73
        );
74
75
    }
76
77
    /**
78
     * Request to Amazon Polly to synthesize speech.
79
     *
80
     * @param string|array $text
81
     * @param array $options
82
     * @return array|\Aws\Result;
0 ignored issues
show
Documentation introduced by
The doc-type array|\Aws\Result; could not be parsed: Expected "|" or "end of type", but got ";" at position 17. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
83
     */
84
    protected function synthesizeSpeech($text, array $options = null)
85
    {
86
        if (is_string($text))
87
        {
88
            return $this->client->synthesizeSpeech([
89
                'VoiceId'      => $this->voice($options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 84 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...
90
                'OutputFormat' => $this->format($options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 84 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...
91
                'TextType'     => 'ssml',
92
                'Text'         => '<speak>' . $text . '</speak>',
93
            ]);
94
        }
95
96
        $results = [];
97
98
        foreach ($text as $item)
99
        {
100
            $result = $this->client->synthesizeSpeech([
101
                'VoiceId'      => $this->voice($options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 84 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...
102
                'OutputFormat' => $this->format($options),
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 84 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...
103
                'Text'         => $item,
104
            ]);
105
106
            array_push($results, $result);
107
        }
108
109
        return $results;
110
    }
111
112
    /**
113
     * Merges the output from amazon polly
114
     *
115
     * @return mixed
116
     */
117
    protected function mergeOutputs(array $results)
118
    {
119
        $mergedResult = null;
120
        foreach ($results as $result)
121
        {
122
            $mergedResult .= $this->getResultContent($result);
123
        }
124
125
        return $mergedResult;
126
    }
127
128
    /**
129
     * Checks the length of the text if more than 3000
130
     *
131
     * @param string $text
132
     * @return boolean
133
     */
134
    protected function isTextAboveLimit(string $text)
135
    {
136
        return strlen($text) > 2000;
137
    }
138
139
    /**
140
     * Chunk the given text into array
141
     *
142
     * @param string $text
143
     * @param int $size
144
     * @return array
145
     */
146
    protected function getChunkText(string $text, int $size = 2000)
147
    {
148
        return explode( "\n", wordwrap($text, $size));
149
    }
150
151
    /**
152
     * Get the text to speech voice ID.
153
     *
154
     * @param  array $options
155
     * @return string
156
     */
157
    protected function voice($options)
158
    {
159
        $default = config('tts.services.polly.voice_id', 'Amy');
160
161
        return Arr::get($options, 'voice', $default);
162
    }
163
164
    /**
165
     * Get the audio format.
166
     *
167
     * @param  array $options
168
     * @return string
169
     */
170
    protected function format($options)
171
    {
172
        $default = config('tts.output_format', 'mp3');
173
174
        return Arr::get($options, 'format', $default);
175
    }
176
177
    /**
178
     * Get the content of the result from AWS Polly.
179
     *
180
     * @param mixed $result
181
     * @return mixed
182
     */
183
    protected function getResultContent($result)
184
    {
185
        return $result->get('AudioStream')->getContents();
186
    }
187
}
188