Completed
Push — master ( c13142...c0435c )
by Tobias
02:34
created

src/Api/Item.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Billogram\Api;
6
7
use Billogram\Exception\Domain\ValidationException;
8
use Billogram\Model\Item\Item as Model;
9
use Billogram\Model\Item\CollectionItem;
10
11
/**
12
 * @author Ibrahim Hizeoui <[email protected]>
13
 */
14
class Item extends HttpApi
15
{
16
    /**
17
     * @param array $param
18
     *
19
     * @return string|array
20
     *
21
     * @see https://billogram.com/api/documentation#items_list
22
     */
23 1
    public function search(array $param = [])
24
    {
25 1
        $param = array_merge(['page' => 1, 'page_size' => 100], $param);
26 1
        $response = $this->httpGet('/item', $param);
27 1
        if (!$this->hydrator) {
28
            return $response;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $response; (Psr\Http\Message\ResponseInterface) is incompatible with the return type documented by Billogram\Api\Item::search of type string|array.

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...
29
        }
30
        // Use any valid status code here
31 1
        if ($response->getStatusCode() !== 200) {
32
            $this->handleErrors($response);
33
        }
34
35 1
        return $this->hydrator->hydrate($response, CollectionItem::class);
36
    }
37
38
    /**
39
     * @param int   $itemNo
40
     * @param array $param
41
     *
42
     * @return mixed|\Psr\Http\Message\ResponseInterface
43
     *
44
     * @see https://billogram.com/api/documentation#items_fetch
45
     */
46 2
    public function fetch(int $itemNo, array $param = [])
47
    {
48 2
        $response = $this->httpGet('/item/'.$itemNo, $param);
49 2
        if (!$this->hydrator) {
50
            return $response;
51
        }
52
        // Use any valid status code here
53 2
        if ($response->getStatusCode() !== 200) {
54
            $this->handleErrors($response);
55
        }
56
57 2
        return $this->hydrator->hydrate($response, Model::class);
58
    }
59
60
    /**
61
     * @param Model $item
62
     *
63
     * @return mixed|\Psr\Http\Message\ResponseInterface
64
     *
65
     * @throws ValidationException
66
     *
67
     * @see https://billogram.com/api/documentation#items_create
68
     */
69 1
    public function create(Model $item)
70
    {
71 1
        $response = $this->httpPost('/item', $item->toArray());
72 1
        if (!$this->hydrator) {
73
            return $response;
74
        }
75
        // Use any valid status code here
76 1
        if ($response->getStatusCode() !== 200) {
77
            $this->handleErrors($response);
78
        }
79
80 1
        return $this->hydrator->hydrate($response, Model::class);
81
    }
82
83
    /**
84
     * @param int   $itemNo
85
     * @param Model $item
86
     *
87
     * @return mixed|\Psr\Http\Message\ResponseInterface
88
     *
89
     * @throws ValidationException
90
     *
91
     * @see https://billogram.com/api/documentation#items_edit
92
     */
93 1
    public function update(int $itemNo, Model $item)
94
    {
95 1
        $response = $this->httpPut('/item/'.$itemNo, $item->toArray());
96 1
        if (!$this->hydrator) {
97
            return $response;
98
        }
99
        // Use any valid status code here
100 1
        if ($response->getStatusCode() !== 200) {
101
            $this->handleErrors($response);
102
        }
103
104 1
        return $this->hydrator->hydrate($response, Model::class);
105
    }
106
107
    /**
108
     * @param int $itemNo
109
     *
110
     * @return mixed|\Psr\Http\Message\ResponseInterface
111
     *
112
     * @throws ValidationException
113
     *
114
     * @see https://billogram.com/api/documentation#items_delete
115
     */
116 1
    public function delete(int $itemNo)
117
    {
118 1
        $response = $this->httpDelete('/item/'.$itemNo);
119 1
        if (!$this->hydrator) {
120
            return $response;
121
        }
122
        // Use any valid status code here
123 1
        if ($response->getStatusCode() !== 200) {
124
            $this->handleErrors($response);
125
        }
126
127 1
        return $this->hydrator->hydrate($response, Model::class);
128
    }
129
}
130