Saytext::say()   B
last analyzed

Complexity

Conditions 5
Paths 10

Size

Total Lines 40
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 29
cp 0
rs 8.439
cc 5
eloc 22
nc 10
nop 1
crap 30
1
<?php
2
3
namespace AppBundle\Action\Executor;
4
5
use AppBundle\Entity\Action;
6
use AppBundle\Entity\Variable;
7
8
class Saytext extends BaseExecutor implements ExecutorInterface
9
{
10
    public function say(Action $action)
11
    {
12
        $args = json_decode($action->getArguments(), true);
13
14
        if (isset($this->parameters['text'])) {
15
            $text = $this->parameters['text'];
16
        } else {
17
            $text = $args['text'];
18
        }
19
20
        if (!$text) {
21
            return;
22
        }
23
24
        $textKey = md5($text);
25
26
        if (!file_exists($this->getContainer()->getParameter('kernel.cache_dir').'/voice')) {
27
            mkdir($this->getContainer()->getParameter('kernel.cache_dir').'/voice');
28
        }
29
30
        $file = $this->getContainer()->getParameter('kernel.cache_dir').'/voice/'.$textKey.'.mp3';
31
32
        $result = 'Played from cache';
33
34
        if (!file_exists($file)) {
35
            $res = file_get_contents(
36
                "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=".
37
                urlencode($text)."&tl=De_de"
38
            );
39
            file_put_contents($file, $res);
40
41
            $result = 'Saved in cache';
42
        }
43
44
        $res = exec('which mplayer');
45
46
        exec($res." -really-quiet -noconsolecontrols ".$file." >/dev/null 2>&1");
47
48
        return $result;
49
    }
50
}
51