Customer::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FAPI\Fortnox\Api;
6
7
use FAPI\Fortnox\Model\ApiResponse;
8
use FAPI\Fortnox\Model\Customer\Customer as Model;
9
use FAPI\Fortnox\Model\Customer\CustomerCollection;
10
use Psr\Http\Message\ResponseInterface;
11
12
/**
13
 * {@link https://developer.fortnox.se/documentation/resources/customers/}.
14
 *
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
class Customer extends HttpApi
18
{
19
    /**
20
     * @throws \FAPI\Fortnox\Exception\DomainException
21
     *
22
     * @return CustomerCollection|ResponseInterface
23
     */
24
    public function all(array $params = [])
25
    {
26
        $response = $this->httpGet('/3/customers', $params);
27
28
        if (!$this->hydrator) {
29
            return $response;
30
        }
31
32
        // Use any valid status code here
33
        if (200 !== $response->getStatusCode()) {
34
            $this->handleErrors($response);
35
        }
36
37
        return $this->hydrator->hydrate($response, CustomerCollection::class);
38
    }
39
40
    /**
41
     * @throws \FAPI\Fortnox\Exception\DomainException
42
     *
43
     * @return Model|ResponseInterface
44
     */
45 View Code Duplication
    public function get(string $customer)
0 ignored issues
show
Duplication introduced by
This method 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...
46
    {
47
        $response = $this->httpGet('/3/customers/'.$customer);
48
49
        if (!$this->hydrator) {
50
            return $response;
51
        }
52
53
        // Use any valid status code here
54
        if (200 !== $response->getStatusCode()) {
55
            $this->handleErrors($response);
56
        }
57
58
        return $this->hydrator->hydrate($response, Model::class);
59
    }
60
61
    /**
62
     * @throws \FAPI\Fortnox\Exception\DomainException
63
     *
64
     * @return Model|ResponseInterface
65
     */
66
    public function create(array $data)
67
    {
68
        $response = $this->httpPost('/3/customers', ['Customer' => $data]);
69
70
        if (!$this->hydrator) {
71
            return $response;
72
        }
73
74
        // Use any valid status code here
75
        if (201 !== $response->getStatusCode()) {
76
            $this->handleErrors($response);
77
        }
78
79
        return $this->hydrator->hydrate($response, Model::class);
80
    }
81
82
    /**
83
     * @throws \FAPI\Fortnox\Exception\DomainException
84
     *
85
     * @return Model|ResponseInterface
86
     */
87
    public function update(string $customer, array $data)
88
    {
89
        $response = $this->httpPut('/3/customers/'.$customer, ['Customer' => $data]);
90
91
        if (!$this->hydrator) {
92
            return $response;
93
        }
94
95
        // Use any valid status code here
96
        if (200 !== $response->getStatusCode()) {
97
            $this->handleErrors($response);
98
        }
99
100
        return $this->hydrator->hydrate($response, Model::class);
101
    }
102
103
    /**
104
     * @throws \FAPI\Fortnox\Exception\DomainException
105
     *
106
     * @return ApiResponse|ResponseInterface
107
     */
108
    public function delete(string $customer)
109
    {
110
        $response = $this->httpDelete('/3/customers/'.$customer);
111
112
        if (!$this->hydrator) {
113
            return $response;
114
        }
115
116
        // Use any valid status code here
117
        if (204 !== $response->getStatusCode()) {
118
            $this->handleErrors($response);
119
        }
120
121
        return $this->hydrator->hydrate($response, ApiResponse::class);
122
    }
123
}
124