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

Ivona::initPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4286
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
3
namespace AudioManager\Adapter;
4
5
use AudioManager\Adapter\Options\Ivona as Options;
6
use AudioManager\Adapter\Ivona\Payload;
7
8
/**
9
 * Adapter for Ivona TTS
10
 * @package AudioManager\Adapter
11
 * @method Options getOptions()
12
 */
13
class Ivona extends AbstractAdapter implements AdapterInterface
14
{
15
    /**
16
     * @var Payload
17
     */
18
    protected $payload;
19
20
    /**
21
     * Init options
22
     * @return Options
23
     */
24 3
    protected function initOptions()
25
    {
26 3
        return new Options;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \AudioManager\Adapter\Options\Ivona(); (AudioManager\Adapter\Options\Ivona) is incompatible with the return type declared by the abstract method AudioManager\Adapter\AbstractAdapter::initOptions of type AudioManager\Adapter\Options\OptionsInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
27
    }
28
29
    /**
30
     * Read audio from Ivona service
31
     * @param string $text
32
     * @return mixed
33
     */
34 1
    public function read($text)
35
    {
36 1
        $payload = $this->getPayload($text);
37
38 1
        $handle = $this->getHandle();
39 1
        $handle->setUrl($payload->getServiceUrl());
40 1
        $handle->setOption(CURLOPT_RETURNTRANSFER, 1);
41 1
        $handle->setOption(CURLOPT_POST, true);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string|integer.

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...
42
43 1
        $handle->setOption(CURLOPT_POSTFIELDS, $payload->getPayload());
0 ignored issues
show
Documentation introduced by
$payload->getPayload() is of type array, but the function expects a string|integer.

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...
44 1
        $handle->setOption(CURLOPT_HTTPHEADER, $payload->getHeaders());
0 ignored issues
show
Documentation introduced by
$payload->getHeaders() is of type array<integer,string,{"0..."string","4":"string"}>, but the function expects a string|integer.

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...
45 1
        $handle->setOption(CURLOPT_SSL_VERIFYPEER, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|integer.

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...
46
47 1
        $response = $handle->execute();
48 1
        $this->setHeaders($handle->getInfo());
49 1
        return $response;
50
    }
51
52
    /**
53
     * Initialize payload
54
     * @param $text
55
     * @return Payload
56
     */
57
    private function initPayload($text)
58
    {
59
        $payload = new Payload();
60
        $payload->setOptions($this->getOptions());
61
        $payload->setQueryText($text);
62
        return $payload->createPayload();
63
    }
64
65
    /**
66
     * @param $text
67
     * @return Payload
68
     */
69 1
    public function getPayload($text)
70
    {
71 1
        if (!$this->payload instanceof Payload) {
72
            $this->payload = $this->initPayload($text);
73
        }
74 1
        return $this->payload;
75
    }
76
77
    /**
78
     * @param Payload $payload
79
     */
80 1
    public function setPayload(Payload $payload)
81
    {
82 1
        $this->payload = $payload;
83 1
    }
84
}
85