1
|
|
|
<?php |
2
|
|
|
namespace App\Actions\Voice; |
3
|
|
|
|
4
|
|
|
use League\Flysystem\FilesystemInterface; |
5
|
|
|
use Staticus\Exceptions\ErrorException; |
6
|
|
|
use Staticus\Middlewares\ActionPostAbstract; |
7
|
|
|
use Staticus\Resources\Mpeg\ResourceDO; |
8
|
|
|
use Staticus\Resources\ResourceDOInterface; |
9
|
|
|
use AudioManager\Manager; |
10
|
|
|
|
11
|
|
|
class ActionPost extends ActionPostAbstract |
12
|
|
|
{ |
13
|
|
|
const LANG_RU = 'ru-RU'; |
|
|
|
|
14
|
|
|
const VOICE_RU = 'Tatyana'; |
15
|
|
|
const LANG_EN = 'en-US'; |
|
|
|
|
16
|
|
|
const VOICE_EN = 'Salli'; |
17
|
|
|
|
18
|
|
|
public function __construct(ResourceDO $resourceDO, FilesystemInterface $filesystem, Manager $manager) |
19
|
|
|
{ |
20
|
|
|
parent::__construct($resourceDO, $filesystem, $manager, null); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
/** |
23
|
|
|
* @param ResourceDOInterface $resourceDO |
24
|
|
|
* @return mixed |
25
|
|
|
* @throws ErrorException |
26
|
|
|
*/ |
27
|
|
|
protected function generate(ResourceDOInterface $resourceDO) |
28
|
|
|
{ |
29
|
|
|
/** @var Manager $generator */ |
30
|
|
|
$generator = $this->generator; |
|
|
|
|
31
|
|
|
$alternative = $resourceDO->getNameAlternative(); |
32
|
|
|
$voiceText = $alternative ?: $resourceDO->getName(); |
|
|
|
|
33
|
|
|
$this->selectLanguage($voiceText); |
34
|
|
|
$content = $generator->read($voiceText); |
35
|
|
|
$headers = $generator->getHeaders(); |
36
|
|
|
if (!array_key_exists('http_code', (array)$headers) || $headers['http_code'] != 200) { |
37
|
|
|
throw new ErrorException( |
38
|
|
|
'Wrong http response code from voice provider ' |
39
|
|
|
. get_class($this->generator->getAdapter()) |
40
|
|
|
. ': ' . $headers['http_code'] . '; Requested text: ' |
41
|
|
|
. $resourceDO->getName()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $content; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isRussian($text) |
48
|
|
|
{ |
49
|
|
|
$matches = []; |
50
|
|
|
preg_match('/[а-яё]+/ui', $text, $matches); |
51
|
|
|
|
52
|
|
|
return !empty($matches); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $voiceText |
57
|
|
|
* @todo LoD violation |
58
|
|
|
*/ |
59
|
|
|
protected function selectLanguage($voiceText) |
60
|
|
|
{ |
61
|
|
|
$adapter = $this->generator->getAdapter(); |
62
|
|
|
/** @var \AudioManager\Adapter\Options\OptionsInterface $options */ |
63
|
|
|
$options = $adapter->getOptions(); |
64
|
|
|
if ($this->isRussian($voiceText)) { |
65
|
|
|
$options->setLanguage(self::LANG_RU); |
66
|
|
|
$options->setVoice(self::VOICE_RU); |
67
|
|
|
} else { |
68
|
|
|
$options->setLanguage(self::LANG_EN); |
69
|
|
|
$options->setVoice(self::VOICE_EN); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.