Passed
Pull Request — 1.11.x (#4629)
by Angel Fernando Quiroz
09:41 queued 01:48
created

Text2SpeechPlugin::create()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 1
nop 0
1
<?php
2
/* For license terms, see /license.txt */
3
4
/**
5
 * Description of Text2SpeechPlugin.
6
 *
7
 * @author Francis Gonzales <[email protected]>
8
 */
9
class Text2SpeechPlugin extends Plugin
10
{
11
    public const MOZILLATTS_API = 'mozillatts';
12
    public const PATH_TO_SAVE_FILES = __DIR__.'/../../app/upload/plugins/text2speech/';
13
14
    protected function __construct()
15
    {
16
        $version = '0.1';
17
        $author = 'Francis Gonzales';
18
19
        $message = 'Description';
20
21
        $settings = [
22
            $message => 'html',
23
            'tool_enable' => 'boolean',
24
            'api_name' => [
25
                'type' => 'select',
26
                'options' => $this->getApiList(),
27
            ],
28
            'api_key' => 'text',
29
            'url' => 'text',
30
            'tool_lp_enable' => 'boolean',
31
        ];
32
33
        parent::__construct($version, $author, $settings);
34
    }
35
36
    /**
37
     * Get the list of apis availables.
38
     *
39
     * @return array
40
     */
41
    public function getApiList()
42
    {
43
        return [
44
            self::MOZILLATTS_API => 'MozillaTTS',
45
        ];
46
    }
47
48
    /**
49
     * Get the completion text from openai.
50
     *
51
     * @return string
52
     */
53
    public function convert(string $text)
54
    {
55
        $path = '/app/upload/plugins/text2speech/';
56
        switch ($this->get('api_name')) {
57
            case self::MOZILLATTS_API:
58
                require_once __DIR__.'/src/mozillatts/MozillaTTS.php';
59
60
                $mozillaTTS = new MozillaTTS($this->get('url'), $this->get('api_key'), self::PATH_TO_SAVE_FILES);
61
                $path .= $mozillaTTS->convert($text);
62
            break;
63
        }
64
65
        return $path;
66
    }
67
68
    /**
69
     * Get the plugin directory name.
70
     */
71
    public function get_name(): string
72
    {
73
        return 'text2speech';
74
    }
75
76
    /**
77
     * Get the class instance.
78
     *
79
     * @staticvar Text2SpeechPlugin $result
80
     */
81
    public static function create(): Text2SpeechPlugin
82
    {
83
        static $result = null;
84
85
        return $result ?: $result = new self();
86
    }
87
88
    /**
89
     * Install the plugin. create folder to save files.
90
     */
91
    public function install()
92
    {
93
        if (!file_exists(self::PATH_TO_SAVE_FILES)) {
94
            mkdir(self::PATH_TO_SAVE_FILES);
95
        }
96
    }
97
98
    /**
99
     * Unistall plugin. Clear the folder.
100
     */
101
    public function uninstall()
102
    {
103
        if (file_exists(self::PATH_TO_SAVE_FILES)) {
104
            array_map('unlink', glob(self::PATH_TO_SAVE_FILES.'/*.*'));
105
            rmdir(self::PATH_TO_SAVE_FILES);
106
        }
107
    }
108
}
109