Passed
Branch master (fc4881)
by Eugene
02:31
created

ActionPost::selectLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 1
b 0
f 0
ccs 0
cts 10
cp 0
crap 6
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';
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
14
    const VOICE_RU = 'Tatyana';
15
    const LANG_EN = 'en-US';
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
16
    const VOICE_EN = 'Salli';
17
18
    public function __construct(ResourceDO $resourceDO, FilesystemInterface $filesystem, Manager $manager)
19
    {
20
        parent::__construct($resourceDO, $filesystem, $manager, null);
0 ignored issues
show
Unused Code introduced by
The call to ActionPostAbstract::__construct() has too many arguments starting with null.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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;
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
31
        $alternative = $resourceDO->getNameAlternative();
32
        $voiceText = $alternative ?: $resourceDO->getName();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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
}