Passed
Pull Request — 1.11.x (#4629)
by Angel Fernando Quiroz
15:45 queued 06:27
created

MozillaTTS::convert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
require_once __DIR__.'/../IProvider.php';
4
5
class MozillaTTS implements IProvider
6
{
7
    private $url;
8
    private $apiKey;
9
    private $filePath;
10
11
    public function __construct(string $url, string $apiKey, string $filePath)
12
    {
13
        $this->url = $url;
14
        $this->apiKey = $apiKey;
15
        $this->filePath = $filePath;
16
    }
17
18
    public function convert(string $text): string
19
    {
20
        return $this->request($text);
21
    }
22
23
    private function request(string $data): string
24
    {
25
        $filename = uniqid().'.wav';
26
        $filePath = $this->filePath.$filename;
27
//        $resource = fopen(realpath($filePath), 'w');
28
29
        $client = new GuzzleHttp\Client();
30
        $client->get($this->url.'?api_key='.urlencode($this->apiKey).
31
            '&text='.str_replace('%0A', '+', urlencode($data)), [
32
            'headers' => [
33
                'Cache-Control' => 'no-cache',
34
                'Content-Type' => 'audio/wav',
35
            ],
36
            'sink' => $filePath,
37
        ]);
38
39
        return $filename;
40
    }
41
}
42