AbstractAdapter::setSpeed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace TTS\Driver;
3
4
use GuzzleHttp\Exception\RequestException;
5
6
abstract class AbstractAdapter
7
{
8
    const DEFAULT_LANG = 'en_GB';
9
10
    protected $name = null;
11
    protected $voice = null;
12
    /** @var \GuzzleHttp\Client $client */
13
    protected $client = null;
14
15
    protected $language = null;
16
    protected $speed = null;
17
18
    private $debug = false;
19
20
    protected $languageMap = [
21
    ];
22
23
24
    public function __construct($language = self::DEFAULT_LANG, $speed = '0.5')
0 ignored issues
show
Unused Code introduced by
The parameter $speed is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
25
    {
26
        if ($this->validateLanguage($language)) {
27
            throw new \Exception('The language is not supported');
28
        }
29
30
        $this->client = new \GuzzleHttp\Client();
31
        $this->language = $this->languageMap[$language];
32
    }
33
34
    public function setLanguage($language)
35
    {
36
        if ($this->validateLanguage($language)) {
37
            throw new \Exception('The language is not supported');
38
        }
39
40
        $this->language = $this->languageMap[$language];
41
        return $this;
42
    }
43
44
    protected function validateLanguage($language)
45
    {
46
        return !array_key_exists($language, $this->languageMap);
47
    }
48
49
    public function getLanguage()
50
    {
51
        return $this->language;
52
    }
53
54
    public function setSpeed($speed)
55
    {
56
        $this->speed = $speed;
57
        return $this;
58
    }
59
60
    public function getSpeed()
61
    {
62
        return $this->speed;
63
    }
64
65
    public function getName()
66
    {
67
        return $this->name;
68
    }
69
70
    public function getVoice()
71
    {
72
        return $this->voice;
73
    }
74
75
    public function reguestGet($params)
76
    {
77
        $headers = [
78
            'User-Agent'    => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.872.0 Safari/535.2'
79
        ];
80
81
        try {
82
            $response = $this->client->get($this->getUri(), ['debug' => $this->debug, 'query' => $params, 'headers' => $headers]);
83
        } catch (RequestException $e) {
84
            echo $e->getMessage();
85
86
            if ($e->hasResponse()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
87
            }
88
            die('error');
0 ignored issues
show
Coding Style Compatibility introduced by
The method reguestGet() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
89
        }
90
        return $response->getBody()->getContents();
91
    }
92
93
    abstract public function getUri();
94
    abstract public function make($text, $fileName);
95
}
96