Passed
Push — master ( f2c58b...cf9c94 )
by Darío
36s queued 10s
created

PayPalService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 97.06%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 30
c 1
b 0
f 1
dl 0
loc 66
ccs 33
cts 34
cp 0.9706
rs 10
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setAuth() 0 4 1
A __construct() 0 4 1
A withHandler() 0 3 1
A getProducts() 0 6 1
A createProduct() 0 7 1
A getToken() 0 15 2
A updateProduct() 0 7 1
1
<?php
2
3
namespace PaymentGateway\PayPalSdk;
4
5
use EasyHttp\GuzzleLayer\GuzzleClient;
6
use EasyHttp\LayerContracts\Contracts\HttpClientResponse;
7
use PaymentGateway\PayPalSdk\Requests\StoreProductRequest;
8
use PaymentGateway\PayPalSdk\Requests\UpdateProductRequest;
9
10
class PayPalService
11
{
12
    protected $client;
13
    protected string $baseUri;
14
    protected string $username;
15
    protected string $password;
16
    protected array $token;
17
18 4
    public function __construct(string $baseUri)
19
    {
20 4
        $this->baseUri = $baseUri;
21 4
        $this->client = new GuzzleClient();
22 4
    }
23
24 4
    public function setAuth(string $username, string $password)
25
    {
26 4
        $this->username = $username;
27 4
        $this->password = $password;
28 4
    }
29
30 4
    public function withHandler(callable $handler)
31
    {
32 4
        $this->client->withHandler($handler);
33 4
    }
34
35 4
    public function getToken(): array
36
    {
37 4
        if ($this->token ?? null) {
38
            return $this->token;
39
        }
40
41 4
        $client = clone $this->client;
42
43 4
        $client->prepareRequest('POST', $this->baseUri . '/v1/oauth2/token');
44 4
        $client->getRequest()->setBasicAuth($this->username, $this->password);
0 ignored issues
show
Bug introduced by
The method setBasicAuth() does not exist on EasyHttp\LayerContracts\...racts\HttpClientRequest. It seems like you code against a sub-type of EasyHttp\LayerContracts\...racts\HttpClientRequest such as EasyHttp\GuzzleLayer\GuzzleRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $client->getRequest()->/** @scrutinizer ignore-call */ setBasicAuth($this->username, $this->password);
Loading history...
45 4
        $client->getRequest()->setQuery(['grant_type' => 'client_credentials']);
46
47 4
        $this->token = $client->execute()->parseJson();
48
49 4
        return $this->token;
50
    }
51
52 1
    public function createProduct(StoreProductRequest $product): HttpClientResponse
53
    {
54 1
        $this->client->prepareRequest('POST', $this->baseUri . '/v1/catalogs/products');
55 1
        $this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']);
56 1
        $this->client->getRequest()->setJson($product->toArray());
57
58 1
        return $this->client->execute();
59
    }
60
61 1
    public function getProducts(): HttpClientResponse
62
    {
63 1
        $this->client->prepareRequest('GET', $this->baseUri . '/v1/catalogs/products');
64 1
        $this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']);
65
66 1
        return $this->client->execute();
67
    }
68
69 1
    public function updateProduct(UpdateProductRequest $product): HttpClientResponse
70
    {
71 1
        $this->client->prepareRequest('PATCH', $this->baseUri . '/v1/catalogs/products/' . $product->getId());
72 1
        $this->client->getRequest()->setHeader('Authorization', 'Bearer ' . $this->getToken()['access_token']);
73 1
        $this->client->getRequest()->setJson($product->toArray());
74
75 1
        return $this->client->execute();
76
    }
77
}
78