Completed
Push — master ( 2bbac1...dfe03e )
by Kirill
04:01
created

Saytext::say()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 27
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 21
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("http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=".
36
                urlencode($text)."&tl=De_de");
37
            file_put_contents($file, $res);
38
39
            $result= 'Saved in cache';
40
        }
41
42
        $res = exec('which mplayer');
43
44
        exec($res." -really-quiet -noconsolecontrols ".$file);
45
46
        return $result;
47
    }
48
}
49