TextToSpeech   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 109
Duplicated Lines 8.26 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
c 3
b 0
f 0
lcom 1
cbo 1
dl 9
loc 109
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 3
A setText() 9 9 1
A clear() 0 6 1
A process() 0 6 1
B textToFile() 0 26 6
A getTempFile() 0 4 1
A getFiles() 0 4 1
A getCacheDir() 0 6 1
A prepareFileName() 0 9 1
A prepareUiq() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace TTS;
3
4
use TTS\Driver\Google;
5
use TTS\Exceptions\Exception;
6
7
/**
8
 * Class TextToSpeech
9
 * @package TTS
10
 */
11
class TextToSpeech
12
{
13
    private $cacheDir = null;
14
    private $driver = null;
15
    private $voice = 'famale';
0 ignored issues
show
Unused Code introduced by
The property $voice is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
17
    private $lines = array();
18
    private $files = array();
19
20
    protected $text = '';
21
22
    public function __construct($options = null)
23
    {
24
        if ($options['cacheDir']) {
25
            $this->cacheDir = $options['cacheDir'];
26
        }
27
28
        if (isset($options['driver'])) {
29
            $adapterClass = __NAMESPACE__. '\\Driver\\' . $options['driver'];
30
        } else {
31
            $adapterClass = Google::class;
32
        }
33
34
        $this->driver = new $adapterClass('ru_RU', '0.5');
35
    }
36
37 View Code Duplication
    public function setText($text)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $text = trim(str_replace(["\r", "\n", '"', "'", '«', '»'], ' ', $text));
40
        $text = preg_replace('/([ ]{2,})/', ' ', $text);
41
        $text = mb_strtolower($text);
42
43
        $this->text = $text;
44
        return $this;
45
    }
46
47
    public function clear()
48
    {
49
        $this->lines = array();
50
        $this->files = array();
51
        return $this;
52
    }
53
54
    public function process()
55
    {
56
        $this->textToFile($this->text);
57
58
        return $this;
59
    }
60
61
    private function textToFile($text)
62
    {
63
        if (!$text) {
64
            return array();
65
        }
66
67
        $cacheDir = $this->getCacheDir();
68
        if (!is_dir($cacheDir) || !is_writable($cacheDir)) {
69
            throw new Exception('Can not write to ' . $cacheDir);
70
        }
71
        $cacheDir = dirname($this->prepareFileName($text));
72
        if (!is_dir($cacheDir)) {
73
            mkdir($cacheDir, 0755, true);
74
        }
75
76
        $fileName = $this->prepareFileName($text);
77
        if (!is_file($fileName)) {
78
            $content = $this->driver->make($text, $fileName);
79
            file_put_contents($fileName, $content);
80
        }
81
82
        $this->files[] = $fileName;
83
84
85
        return $this;
86
    }
87
88
    protected function getTempFile()
89
    {
90
        return tempnam(sys_get_temp_dir(), 'tts');
91
    }
92
93
    public function getFiles()
94
    {
95
        return $this->files;
96
    }
97
98
    private function getCacheDir()
99
    {
100
        return $this->cacheDir
101
        . '/' . strtolower($this->driver->getName())
102
        . '/' .$this->driver->getVoice() . '/';
103
    }
104
105
    private function prepareFileName($text)
106
    {
107
        $fileName = $this->prepareUiq($text);
108
        return $this->getCacheDir() . '/' . substr($fileName, 0, 2) . '/' . substr(
109
            $fileName,
110
            2,
111
            2
112
        ) . '/' . $fileName . '.mp3';
113
    }
114
115
    private function prepareUiq($text)
116
    {
117
        return sha1($text);
118
    }
119
}
120