Completed
Push — master ( 70d624...58e61e )
by Vadim
24s
created

Payload::isRussian()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
3
namespace AudioManager\Adapter\Ivona;
4
5
use AudioManager\Adapter\Options\Ivona as Options;
6
use AudioManager\Exception\RuntimeException;
7
8
/***
9
 * Class Payload
10
 * @package AudioManager\Adapter\Ivona
11
 */
12
class Payload
13
{
14
15
    const SERVICE_TYPE_LIST = 'ListVoices';
16
    const SERVICE_TYPE_SPEECH = 'CreateSpeech';
17
    const DEFAULT_EN_VOICE = 'Salli';
18
    const DEFAULT_RU_VOICE = 'Tatyana';
19
20
    /**
21
     * @var Options
22
     */
23
    protected $options;
24
25
    /**
26
     * @var string
27
     */
28
    protected $payload;
29
30
    protected $queryText;
31
    protected $serviceUrl = "https://tts.eu-west-1.ivonacloud.com";
32
    protected $outputFormatCodec = 'MP3';
33
    protected $outputSampleRate = '22050';
34
    protected $parametersRate = 'slow';
35
36
    /**
37
     * Get service headers
38
     * @return array
39
     */
40 1
    public function getHeaders()
41
    {
42 1
        $this->getOptions()->getAuthenticate()->setPostData($this->getPayload());
0 ignored issues
show
Documentation introduced by
$this->getPayload() is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
43
44
        $headers = [
45 1
            'Content-Type: application/json',
46 1
            'Host: tts.eu-west-1.ivonacloud.com',
47 1
            'User-Agent: ' . $this->getOptions()->getUserAgent()
48 1
        ];
49
50 1
        return array_merge(
51 1
            $headers,
52 1
            $this->getOptions()->getAuthenticate()->getHeader(self::SERVICE_TYPE_SPEECH)
53 1
        );
54
    }
55
56
    /**
57
     * Create json object with post parameters
58
     * @return $this
59
     */
60
    public function createPayload()
61
    {
62
        $payloadArray = (object)array();
63
        $payloadArray->Input['Data'] = $this->getQueryText();
64
        $payloadArray->Input['Type'] = 'text/plain';
65
66
        $payloadArray->OutputFormat['Codec'] = $this->outputFormatCodec;
67
        $payloadArray->OutputFormat['SampleRate'] = (int) $this->outputSampleRate;
68
        $lang = 'en-US';
69
        $voice = static::DEFAULT_EN_VOICE;
70
        if ($this->isRussian()) {
71
            $lang = 'ru-RU';
72
            $voice = static::DEFAULT_RU_VOICE;
73
        }
74
        $payloadArray->Voice['Language'] = $lang;
75
        $payloadArray->Voice['Name'] = $voice;
76
        $payloadArray->Parameters['Rate'] = $this->parametersRate;
77
78
        $this->setPayload(json_encode($payloadArray));
79
        return $this;
80
    }
81
82
    /**
83
     * Set payload
84
     * @param $payload
85
     * @return $this
86
     */
87
    public function setPayload($payload)
88
    {
89
        $this->payload = $payload;
90
        return $this;
91
    }
92
93
    /**
94
     * Get post data for service
95
     * @return array
96
     */
97 1
    public function getPayload()
98
    {
99 1
        return $this->payload;
100
    }
101
102
    /**
103
     * Get url for service with service type
104
     * @return string
105
     */
106 1
    public function getServiceUrl()
107
    {
108 1
        return $this->serviceUrl . '/' . self::SERVICE_TYPE_SPEECH;
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function isRussian()
115
    {
116
        $matches = [];
117
        preg_match('/[а-яё]+/ui', $this->queryText, $matches);
118
119
        return !empty($matches);
120
    }
121
122
    /**
123
     * Check available name for service
124
     * @param string $serviceType
125
     * @return string
126
     * @throw
127
     */
128 2
    protected function checkServiceType($serviceType)
129
    {
130 2
        $reflection = new \ReflectionObject($this);
131 2
        $constants = $reflection->getConstants();
132 2
        if (!in_array($serviceType, $constants)) {
133 1
            throw new RuntimeException('Service type does not supports: ' . $serviceType);
134
        }
135 1
        return $serviceType;
136
    }
137
138
    /**
139
     * @return Options
140
     */
141 2
    public function getOptions()
142
    {
143 2
        return $this->options;
144
    }
145
146
    /**
147
     * @param Options $options
148
     * @return $this
149
     */
150 2
    public function setOptions($options)
151
    {
152 2
        $this->options = $options;
153 2
        return $this;
154
    }
155
156
    /**
157
     * @return string
158
     */
159 1
    public function getQueryText()
160
    {
161 1
        if (empty($this->queryText)) {
162
            throw new \RuntimeException('Need set query text');
163
        }
164 1
        return $this->queryText;
165
    }
166
167
    /**
168
     * @param string $queryText
169
     */
170 1
    public function setQueryText($queryText)
171
    {
172 1
        $this->queryText = $queryText;
173 1
    }
174
}
175