Passed
Push — master ( d68831...58f816 )
by Bruce Pinheiro de
04:28
created

CustomerClient::getToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
    public function __construct(ContextInterface $context, ?array $options = [])
32
	{
33
		$this->context = $context;
34
        $this->options = $options;
35
	}
36
37
    /**
38
     * @inheritDoc
39
     * @throws BadRequestException
40
     */
41
    public function create(CustomerInterface $customer): ?CustomerInterface
42
    {
43
        return $this->request('post', $customer) ? $customer : null;
44
    }
45
46
    /**
47
     * @param $customer
48
     * @param string|null $type
49
     * @return array
50
     */
51
    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
        if (!$customer instanceof CustomerInterface) {
54
            throw new InvalidCustomerException('Invalid customer or $customer does not implement CustomerInterface!');
55
        }
56
57
        $body = [
58
            'customer_id' => $customer->getCustomerId(),
59
            'personal_details' => [
60
                'name' => $customer->getName(),
61
                'phone' => $customer->getPhone(),
62
                'address' => [
63
                    'line1' => $customer->getAddress()->getLine1(),
64
                    'line2' => $customer->getAddress()->getLine2(),
65
                    'country' => $customer->getAddress()->getCountry(),
66
                    'postal_code' => $customer->getAddress()->getPostalCode(),
67
                    'city' => $customer->getAddress()->getCity(),
68
                    'state' => $customer->getAddress()->getState(),
69
                ],
70
            ],
71
        ];
72
73
        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
    public static function getScopes(): array
85
    {
86
        return [
87
            'payment_instruments',
88
        ];
89
    }
90
91
	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
		return $this->lastResponse;
94
	}
95
96
97
	/**
98
	 * return the context used to created the client.
99
	 * @return ContextInterface
100
	 */
101
	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
        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
    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
        $instrumentClient = new PaymentInstrumentClient($this->getContext(), $this->getOptions());
116
        $instrumentClient->setCustomer($customer);
117
        $instrumentClient->setToken($this->getToken());
118
        $response = $instrumentClient->disable($paymentInstrument);
119
        $this->setLastResponse($instrumentClient->getLastResponse());
120
121
        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
    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
        $instrumentClient = new PaymentInstrumentClient($this->getContext(), $this->getOptions());
133
        $instrumentClient->setCustomer($customer);
134
        $instrumentClient->setToken($this->getToken());
135
        $response = $instrumentClient->get();
136
        $this->setLastResponse($instrumentClient->getLastResponse());
137
138
        return $response;
139
    }
140
141
    /**
142
     * @param ResponseInterface $response
143
     * @return SumUpClientInterface
144
     */
145
    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
        $this->lastResponse = $response;
148
149
        return $this;
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    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
        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
    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
        return 'customers';
166
    }
167
168
    /**
169
     * @param AccessToken $token
170
     * @return SumUpClientInterface
171
     */
172
    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
        $this->token = $token;
175
176
        return $this;
177
    }
178
179
    /**
180
     * @return AccessToken
181
     */
182
    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
        return $this->token;
185
	}
186
}
187