Completed
Push — develop ( c050d4...a5d0c7 )
by Vadim
02:45
created

Payload::setPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
18
    /**
19
     * @var Options
20
     */
21
    protected $options;
22
23
    /**
24
     * @var string
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());
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...
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->setPayload(json_encode($payloadArray));
71
        return $this;
72
    }
73
74
    /**
75
     * Set payload
76
     * @param $payload
77
     * @return $this
78
     */
79
    public function setPayload($payload)
80
    {
81
        $this->payload = $payload;
82
        return $this;
83
    }
84
85
    /**
86
     * Get post data for service
87
     * @return array
88
     */
89 1
    public function getPayload()
90
    {
91 1
        return $this->payload;
92
    }
93
94
    /**
95
     * Get url for service with service type
96
     * @return string
97
     */
98 1
    public function getServiceUrl()
99
    {
100 1
        return $this->serviceUrl . '/' . self::SERVICE_TYPE_SPEECH;
101
    }
102
103
    /**
104
     * Check available name for service
105
     * @param string $serviceType
106
     * @return string
107
     * @throw
108
     */
109 2
    protected function checkServiceType($serviceType)
110
    {
111 2
        $reflection = new \ReflectionObject($this);
112 2
        $constants = $reflection->getConstants();
113 2
        if (!in_array($serviceType, $constants)) {
114 1
            throw new RuntimeException('Service type does not supports: ' . $serviceType);
115
        }
116 1
        return $serviceType;
117
    }
118
119
    /**
120
     * @return Options
121
     */
122 2
    public function getOptions()
123
    {
124 2
        return $this->options;
125
    }
126
127
    /**
128
     * @param Options $options
129
     * @return $this
130
     */
131 2
    public function setOptions($options)
132
    {
133 2
        $this->options = $options;
134 2
        return $this;
135
    }
136
137
    /**
138
     * @return string
139
     */
140 1
    public function getQueryText()
141
    {
142 1
        if (empty($this->queryText)) {
143
            throw new \RuntimeException('Need set query text');
144
        }
145 1
        return $this->queryText;
146
    }
147
148
    /**
149
     * @param string $queryText
150
     */
151 1
    public function setQueryText($queryText)
152
    {
153 1
        $this->queryText = $queryText;
154 1
    }
155
}
156