ActionPost::isRussian()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
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);
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
32
        // If a body is exist, it will be used as the text for voicing
33
        $body = $resourceDO->getBody();
34
35
        // If an alternative name is exist, it will be used when body is empty
36
        $alternative = $resourceDO->getNameAlternative();
37
38
        // The main resource name will be used, if no body and no alt exist
39
        $voiceText = $body ?: (
40
            $alternative ?: $resourceDO->getName()
41
        );
42
43
        $this->selectLanguage($voiceText);
44
        $content = $generator->read($voiceText);
45
        $headers = $generator->getHeaders();
46
        if (!array_key_exists('http_code', (array)$headers) || $headers['http_code'] != 200) {
47
            throw new ErrorException(
48
                'Wrong http response code from voice provider '
49
                . get_class($this->generator->getAdapter())
50
                . ': ' . $headers['http_code'] . '; Requested text: '
51
                . $resourceDO->getName());
52
        }
53
54
        return $content;
55
    }
56
57
    public function isRussian($text)
58
    {
59
        $matches = [];
60
        preg_match('/[а-яё]+/ui', $text, $matches);
61
62
        return !empty($matches);
63
    }
64
65
    /**
66
     * @param $voiceText
67
     * @todo LoD violation
68
     */
69
    protected function selectLanguage($voiceText)
70
    {
71
        $adapter = $this->generator->getAdapter();
72
        /** @var \AudioManager\Adapter\Options\OptionsInterface $options */
73
        $options = $adapter->getOptions();
74
        if ($this->isRussian($voiceText)) {
75
            $options->setLanguage(self::LANG_RU);
76
            $options->setVoice(self::VOICE_RU);
77
        } else {
78
            $options->setLanguage(self::LANG_EN);
79
            $options->setVoice(self::VOICE_EN);
80
        }
81
    }
82
}