Completed
Push — develop ( ccfaa9...e7de42 )
by Vadim
04:18
created

Payload::setQueryText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
18
    /**
19
     * @var Options
20
     */
21
    protected $options;
22
23
    /**
24
     * @var array
25
     */
26
    protected $payload = [];
27
28
    protected $queryText;
29
    protected $serviceUrl = "https://tts.eu-west-1.ivonacloud.com";
30
    protected $outputFormatCodec = 'MP3';
31
    protected $outputSampleRate = '22050';
32
    protected $parametersRate = 'slow';
33
34
    /**
35
     * Get service headers
36
     * @return array
37
     */
38 1
    public function getHeaders()
39
    {
40 1
        $this->getOptions()->getAuthenticate()->setPostData($this->getPayload());
41
42
        $headers = [
43 1
            'Content-Type: application/json',
44 1
            'Host: tts.eu-west-1.ivonacloud.com',
45 1
            'User-Agent: ' . $this->getOptions()->getUserAgent()
46 1
        ];
47
48 1
        return array_merge(
49 1
            $headers,
50 1
            $this->getOptions()->getAuthenticate()->getHeader(self::SERVICE_TYPE_SPEECH)
51 1
        );
52
    }
53
54
    /**
55
     * Create json object with post parameters
56
     * @return $this
57
     */
58
    public function createPayload()
59
    {
60
        $payloadArray = (object)array();
61
        $payloadArray->Input['Data'] = $this->getQueryText();
62
        $payloadArray->Input['Type'] = 'text/plain';
63
64
        $payloadArray->OutputFormat['Codec'] = $this->outputFormatCodec;
65
        $payloadArray->OutputFormat['SampleRate'] = (int) $this->outputSampleRate;
66
        $payloadArray->Voice['Language'] = 'en-US';
67
        $payloadArray->Voice['Name'] = 'Salli';
68
        $payloadArray->Parameters['Rate'] = $this->parametersRate;
69
70
        $this->payload = json_encode($payloadArray);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_encode($payloadArray) of type string is incompatible with the declared type array of property $payload.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
        return $this;
72
    }
73
74
    /**
75
     * Get post data for service
76
     * @return array
77
     */
78 1
    public function getPayload()
79
    {
80 1
        return $this->payload;
81
    }
82
83
    /**
84
     * Get url for service with service type
85
     * @return string
86
     */
87 1
    public function getServiceUrl()
88
    {
89 1
        return $this->serviceUrl . '/' . self::SERVICE_TYPE_SPEECH;
90
    }
91
92
    /**
93
     * Check available name for service
94
     * @param string $serviceType
95
     * @return string
96
     * @throw
97
     */
98 2
    protected function checkServiceType($serviceType)
99
    {
100 2
        $reflection = new \ReflectionObject($this);
101 2
        $constants = $reflection->getConstants();
102 2
        if (!in_array($serviceType, $constants)) {
103 1
            throw new RuntimeException('Service type does not supports: ' . $serviceType);
104
        }
105 1
        return $serviceType;
106
    }
107
108
    /**
109
     * @return Options
110
     */
111 2
    public function getOptions()
112
    {
113 2
        return $this->options;
114
    }
115
116
    /**
117
     * @param Options $options
118
     * @return $this
119
     */
120 2
    public function setOptions($options)
121
    {
122 2
        $this->options = $options;
123 2
        return $this;
124
    }
125
126
    /**
127
     * @return string
128
     */
129 1
    public function getQueryText()
130
    {
131 1
        if (empty($this->queryText)) {
132
            throw new \RuntimeException('Need set query text');
133
        }
134 1
        return $this->queryText;
135
    }
136
137
    /**
138
     * @param string $queryText
139
     */
140 1
    public function setQueryText($queryText)
141
    {
142 1
        $this->queryText = $queryText;
143 1
    }
144
}
145