Passed
Push — master ( 519b06...806fd9 )
by Bruce Pinheiro de
02:47
created

CustomerClient::getLastResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace BPCI\SumUp\Customer;
3
4
use BPCI\SumUp\ContextInterface;
5
use BPCI\SumUp\Customer\PaymentInstrument\PaymentInstrumentClient;
6
use BPCI\SumUp\Customer\PaymentInstrument\PaymentInstrumentInterface;
7
use BPCI\SumUp\Exception\BadRequestException;
8
use BPCI\SumUp\Exception\InvalidCustomerException;
9
use BPCI\SumUp\OAuth\AccessToken;
10
use BPCI\SumUp\SumUpClientInterface;
11
use BPCI\SumUp\Traits\Client;
12
use BPCI\SumUp\Traits\ClientInterface;
13
use BPCI\SumUp\Utils\Hydrator;
0 ignored issues
show
Bug introduced by
The type BPCI\SumUp\Utils\Hydrator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Psr\Http\Message\ResponseInterface;
15
16
class CustomerClient implements CustomerClientInterface, ClientInterface
17
{
18
    use Client;
19
20
    const ENDPOINT = 'customers';
21
    protected $context;
22
    protected $token;
23
    protected $lastResponse;
24
    protected $options = [];
25
26
    /**
27
	 * CheckoutClientInterface constructor.
28
	 * @param ContextInterface $context
29
     * @param array $options
30
     */
31 3
    public function __construct(ContextInterface $context, ?array $options = [])
32
	{
33 3
		$this->context = $context;
34 3
        $this->options = $options;
35 3
	}
36
37
    /**
38
     * @inheritDoc
39
     * @throws BadRequestException
40
     */
41 1
    public function create(CustomerInterface $customer): ?CustomerInterface
42
    {
43 1
        return $this->request('post', $customer) ? $customer : null;
44
    }
45
46
    /**
47
     * @param $customer
48
     * @param string|null $type
49
     * @return array
50
     */
51 1
    static function getBody($customer, string $type = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
    {
53 1
        if (!$customer instanceof CustomerInterface) {
54
            throw new InvalidCustomerException('Invalid customer or $customer does not implement CustomerInterface!');
55
        }
56
57
        $body = [
58 1
            'customer_id' => $customer->getCustomerId(),
59
            'personal_details' => [
60 1
                'name' => $customer->getName(),
61 1
                'phone' => $customer->getPhone(),
62
                'address' => [
63 1
                    'line1' => $customer->getAddress()->getLine1(),
64 1
                    'line2' => $customer->getAddress()->getLine2(),
65 1
                    'country' => $customer->getAddress()->getCountry(),
66 1
                    'postal_code' => $customer->getAddress()->getPostalCode(),
67 1
                    'city' => $customer->getAddress()->getCity(),
68 1
                    'state' => $customer->getAddress()->getState(),
69
                ],
70
            ],
71
        ];
72
73 1
        return $body;
74
    }
75
76
    public function setContext(ContextInterface $context): CustomerClientInterface
77
	{
78
		$this->context = $context;
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return BPCI\SumUp\Customer\CustomerClientInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
79
	}
80
81
    /**
82
     * @inheritDoc
83
     */
84 1
    public static function getScopes(): array
85
    {
86
        return [
87 1
            'payment_instruments',
88
        ];
89
    }
90
91 1
	function getLastResponse(): ResponseInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
92
	{
93 1
		return $this->lastResponse;
94
	}
95
96
97
	/**
98
	 * return the context used to created the client.
99
	 * @return ContextInterface
100
	 */
101 3
	function getContext(): ContextInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
102
	{
103 3
        return $this->context;
104
    }
105
106
    /**
107
     * Delete a customer card.
108
     *
109
     * @param CustomerInterface $customer
110
     * @param PaymentInstrumentInterface $paymentInstrument
111
     * @return bool
112
     */
113 1
    function disablePaymentInstrument(CustomerInterface $customer, PaymentInstrumentInterface $paymentInstrument): bool
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
114
    {
115 1
        $instrumentClient = new PaymentInstrumentClient($this->getContext(), $this->getOptions());
116 1
        $instrumentClient->setCustomer($customer);
117 1
        $instrumentClient->setToken($this->getToken());
118 1
        $response = $instrumentClient->disable($paymentInstrument);
119 1
        $this->setLastResponse($instrumentClient->getLastResponse());
120
121 1
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
122
    }
123
124
    /**
125
     * This must return an Array of PaymentInstrumentInterface
126
     *
127
     * @param CustomerInterface $customer
128
     * @return array
129
     */
130 1
    function getPaymentInstruments(CustomerInterface $customer): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
131
    {
132 1
        $instrumentClient = new PaymentInstrumentClient($this->getContext(), $this->getOptions());
133 1
        $instrumentClient->setCustomer($customer);
134 1
        $instrumentClient->setToken($this->getToken());
135 1
        $response = $instrumentClient->get();
136 1
        $this->setLastResponse($instrumentClient->getLastResponse());
137
138 1
        return $response;
139
    }
140
141
    /**
142
     * @param ResponseInterface $response
143
     * @return SumUpClientInterface
144
     */
145 3
    function setLastResponse(ResponseInterface $response): SumUpClientInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
146
    {
147 3
        $this->lastResponse = $response;
148
149 3
        return $this;
150
    }
151
152
    /**
153
     * @return array
154
     */
155 3
    function getOptions(): array
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
156
    {
157 3
        return $this->options;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->options could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
158
    }
159
160
    /**
161
     * @return string
162
     */
163 1
    function getEndPoint(): string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
164
    {
165 1
        return 'customers';
166
    }
167
168
    /**
169
     * @param AccessToken $token
170
     * @return SumUpClientInterface
171
     */
172 3
    function setToken(AccessToken $token): SumUpClientInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
173
    {
174 3
        $this->token = $token;
175
176 3
        return $this;
177
    }
178
179
    /**
180
     * @return AccessToken
181
     */
182 3
    function getToken():? AccessToken
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
    {
184 3
        return $this->token;
185
	}
186
}
187