Completed
Push — master ( 6fd31a...1e3a6a )
by Vadim
02:17
created

Payload   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 67.39%

Importance

Changes 9
Bugs 2 Features 5
Metric Value
wmc 12
c 9
b 2
f 5
lcom 2
cbo 3
dl 0
loc 140
ccs 31
cts 46
cp 0.6739
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaders() 0 15 1
A createPayload() 0 15 1
A setPayload() 0 5 1
A getPayload() 0 4 1
A getServiceUrl() 0 4 1
A checkServiceType() 0 9 2
A getOptions() 0 4 1
A setOptions() 0 5 1
A getQueryText() 0 7 2
A setQueryText() 0 4 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
    const SERVICE_TYPE_LIST = 'ListVoices';
15
    const SERVICE_TYPE_SPEECH = 'CreateSpeech';
16
17
    /**
18
     * @var Options
19
     */
20
    protected $options;
21
22
    /**
23
     * @var string
24
     */
25
    protected $payload;
26
27
    protected $queryText;
28
    protected $serviceUrl = "https://tts.eu-west-1.ivonacloud.com";
29
30
    /**
31
     * Get service headers
32
     * @return array
33
     */
34 1
    public function getHeaders()
35
    {
36 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...
37
38
        $headers = [
39 1
            'Content-Type: application/json',
40 1
            'Host: tts.eu-west-1.ivonacloud.com',
41 1
            'User-Agent: ' . $this->getOptions()->getUserAgent()
42 1
        ];
43
44 1
        return array_merge(
45 1
            $headers,
46 1
            $this->getOptions()->getAuthenticate()->getHeader(self::SERVICE_TYPE_SPEECH)
47 1
        );
48
    }
49
50
    /**
51
     * Create json object with post parameters
52
     * @return $this
53
     */
54
    public function createPayload()
55
    {
56
        $payloadArray = (object)array();
57
        $payloadArray->Input['Data'] = $this->getQueryText();
58
        $payloadArray->Input['Type'] = 'text/plain';
59
60
        $payloadArray->OutputFormat['Codec'] = $this->getOptions()->getOutputFormatCodec();
61
        $payloadArray->OutputFormat['SampleRate'] = (int)$this->getOptions()->getOutputSampleRate();
62
        $payloadArray->Voice['Language'] = $this->getOptions()->getLanguage();
63
        $payloadArray->Voice['Name'] = $this->getOptions()->getVoice();
64
        $payloadArray->Parameters['Rate'] = $this->getOptions()->getParametersRate();
65
66
        $this->setPayload(json_encode($payloadArray));
67
        return $this;
68
    }
69
70
    /**
71
     * Set payload
72
     * @param $payload
73
     * @return $this
74
     */
75
    public function setPayload($payload)
76
    {
77
        $this->payload = $payload;
78
        return $this;
79
    }
80
81
    /**
82
     * Get post data for service
83
     * @return array
84
     */
85 1
    public function getPayload()
86
    {
87 1
        return $this->payload;
88
    }
89
90
    /**
91
     * Get url for service with service type
92
     * @return string
93
     */
94 1
    public function getServiceUrl()
95
    {
96 1
        return $this->serviceUrl . '/' . self::SERVICE_TYPE_SPEECH;
97
    }
98
99
    /**
100
     * Check available name for service
101
     * @param string $serviceType
102
     * @return string
103
     * @throw
104
     */
105 2
    protected function checkServiceType($serviceType)
106
    {
107 2
        $reflection = new \ReflectionObject($this);
108 2
        $constants = $reflection->getConstants();
109 2
        if (!in_array($serviceType, $constants)) {
110 1
            throw new RuntimeException('Service type does not supports: ' . $serviceType);
111
        }
112 1
        return $serviceType;
113
    }
114
115
    /**
116
     * @return Options
117
     */
118 2
    public function getOptions()
119
    {
120 2
        return $this->options;
121
    }
122
123
    /**
124
     * @param Options $options
125
     * @return $this
126
     */
127 2
    public function setOptions($options)
128
    {
129 2
        $this->options = $options;
130 2
        return $this;
131
    }
132
133
    /**
134
     * @return string
135
     */
136 1
    public function getQueryText()
137
    {
138 1
        if (empty($this->queryText)) {
139
            throw new \RuntimeException('Need set query text');
140
        }
141 1
        return $this->queryText;
142
    }
143
144
    /**
145
     * @param string $queryText
146
     */
147 1
    public function setQueryText($queryText)
148
    {
149 1
        $this->queryText = $queryText;
150 1
    }
151
}
152