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

src/Api/Customer.php (2 issues)

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\NotFoundException;
8
use Billogram\Exception\Domain\ValidationException;
9
use Billogram\Model\Customer\Customer as Model;
10
use Billogram\Model\Customer\CustomerCollection;
11
use Psr\Http\Message\ResponseInterface;
12
13
/**
14
 * @author Ibrahim Hizeoui <[email protected]>
15
 */
16 View Code Duplication
class Customer extends HttpApi
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
17
{
18
    /**
19
     * @param array $param
20
     *
21
     * @return string|array
22
     *
23
     * @see https://billogram.com/api/documentation#customers_list
24
     */
25 1
    public function search(array $param = [])
26
    {
27 1
        $param = array_merge(['page' => 1, 'page_size' => 100], $param);
28 1
        $response = $this->httpGet('/customer', $param);
29 1
        if (!$this->hydrator) {
30
            return $response;
0 ignored issues
show
Bug Best Practice introduced by Ibrahimhizeoui
The return type of return $response; (Psr\Http\Message\ResponseInterface) is incompatible with the return type documented by Billogram\Api\Customer::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...
31
        }
32 1
        if ($response->getStatusCode() !== 200) {
33
            $this->handleErrors($response);
34
        }
35
36 1
        return $this->hydrator->hydrate($response, CustomerCollection::class);
37
    }
38
39
    /**
40
     * @param int   $customerNo
41
     * @param array $param
42
     *
43
     * @return Model|ResponseInterface
44
     *
45
     * @throws NotFoundException
46
     *
47
     * @see https://billogram.com/api/documentation#customers_fetch
48
     */
49 2
    public function fetch(int $customerNo, array $param = [])
50
    {
51 2
        $response = $this->httpGet('/customer/'.$customerNo, $param);
52 2
        if (!$this->hydrator) {
53
            return $response;
54
        }
55
        // Use any valid status code here
56 2
        if ($response->getStatusCode() !== 200) {
57
            $this->handleErrors($response);
58
        }
59
60 2
        return $this->hydrator->hydrate($response, Model::class);
61
    }
62
63
    /**
64
     * @param Model $customer
65
     *
66
     * @return Model|ResponseInterface
67
     *
68
     * @throws ValidationException
69
     *
70
     * @see https://billogram.com/api/documentation#customers_create
71
     */
72 1
    public function create(Model $customer)
73
    {
74 1
        $response = $this->httpPost('/customer', $customer->toArray());
75 1
        if (!$this->hydrator) {
76
            return $response;
77
        }
78
        // Use any valid status code here
79 1
        if ($response->getStatusCode() !== 200) {
80
            $this->handleErrors($response);
81
        }
82
83 1
        return $this->hydrator->hydrate($response, Model::class);
84
    }
85
86
    /**
87
     * @param int   $customerNo
88
     * @param Model $costumer
89
     *
90
     * @return Model|ResponseInterface
91
     *
92
     * @throws NotFoundException
93
     * @throws ValidationException
94
     *
95
     * @see https://billogram.com/api/documentation#customers_edit
96
     */
97 1
    public function update(int $customerNo, Model $costumer)
98
    {
99 1
        $response = $this->httpPut('/customer/'.$customerNo, $costumer->toArray());
100 1
        if (!$this->hydrator) {
101
            return $response;
102
        }
103
        // Use any valid status code here
104 1
        if ($response->getStatusCode() !== 200) {
105
            $this->handleErrors($response);
106
        }
107
108 1
        return $this->hydrator->hydrate($response, Model::class);
109
    }
110
}
111