Completed
Push — master ( a1ce72...051ac9 )
by Tobias
33:52 queued 23:54
created

SyliusClient::customer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace FAPI\Sylius;
11
12
use FAPI\Sylius\Api\Products;
13
use FAPI\Sylius\Http\AuthenticationPlugin;
14
use FAPI\Sylius\Http\Authenticator;
15
use FAPI\Sylius\Http\ClientConfigurator;
16
use FAPI\Sylius\Hydrator\Hydrator;
17
use FAPI\Sylius\Hydrator\ModelHydrator;
18
use Http\Client\HttpClient;
19
20
/**
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
final class SyliusClient
24
{
25
    /**
26
     * @var HttpClient
27
     */
28
    private $httpClient;
29
30
    /**
31
     * @var Hydrator
32
     */
33
    private $hydrator;
34
35
    /**
36
     * @var RequestBuilder
37
     */
38
    private $requestBuilder;
39
40
    /**
41
     * @var ClientConfigurator
42
     */
43
    private $clientConfigurator;
44
45
    /**
46
     * @var string|null
47
     */
48
    private $clientId;
49
50
    /**
51
     * @var string|null
52
     */
53
    private $clientSecret;
54
55
    /**
56
     * @var Authenticator
57
     */
58
    private $authenticator;
59
60
    /**
61
     * The constructor accepts already configured HTTP clients.
62
     * Use the configure method to pass a configuration to the Client and create an HTTP Client.
63
     */
64
    public function __construct(
65
        ClientConfigurator $clientConfigurator,
66
        string $clientId,
67
        string $clientSecret,
68
        Hydrator $hydrator = null,
69
        RequestBuilder $requestBuilder = null
70
    ) {
71
        $this->clientConfigurator = $clientConfigurator;
72
        $this->hydrator = $hydrator ?: new ModelHydrator();
73
        $this->requestBuilder = $requestBuilder ?: new RequestBuilder();
74
        $this->authenticator = new Authenticator($this->requestBuilder, $this->clientConfigurator->createConfiguredClient(), $clientId, $clientSecret);
75
    }
76
77
    public static function create(string $endpoint, string $clientId, string $clientSecret): self
78
    {
79
        $clientConfigurator = new ClientConfigurator();
80
        $clientConfigurator->setEndpoint($endpoint);
81
82
        return new SyliusClient($clientConfigurator, $clientId, $clientSecret);
83
    }
84
85
    /**
86
     * Autnenticate a user with the API. This will return an access token.
87
     * Warning, this will remove the current access token.
88
     */
89
    public function createNewAccessToken(string $username, string $password): string
90
    {
91
        $this->clientConfigurator->removePlugin(AuthenticationPlugin::class);
92
93
        return $this->authenticator->createAccessToken($username, $password);
94
    }
95
96
    /**
97
     * Autenticate the client with an access token. This should be the full access token object with
98
     * refresh token and expirery timestamps.
99
     *
100
     * ```php
101
     *   $accessToken = $client->createNewAccessToken('foo', 'bar');
102
     *   $client->authenticate($accessToken);
103
     *```
104
     */
105
    public function authenticate(string $accessToken): void
106
    {
107
        $this->clientConfigurator->removePlugin(AuthenticationPlugin::class);
108
        $this->clientConfigurator->appendPlugin(new AuthenticationPlugin($this->authenticator, $accessToken));
109
    }
110
111
    /**
112
     * The access token may have been refreshed during the requests. Use this function to
113
     * get back the (possibly) refreshed access token.
114
     */
115
    public function getAccessToken(): string
116
    {
117
        return $this->authenticator->getAccessToken();
118
    }
119
120
121
    public function customer(): Api\Customer
122
    {
123
        return new Api\Customer($this->getHttpClient(), $this->hydrator, $this->requestBuilder);
124
    }
125
126
    public function cart(): Api\Cart
127
    {
128
        return new Api\Cart($this->getHttpClient(), $this->hydrator, $this->requestBuilder);
129
    }
130
131
    public function product(): Api\Product
132
    {
133
        return new Api\Product($this->getHttpClient(), $this->hydrator, $this->requestBuilder);
134
    }
135
136
    public function checkout(): Api\Checkout
137
    {
138
        return new Api\Checkout($this->getHttpClient(), $this->hydrator, $this->requestBuilder);
139
    }
140
141
    private function getHttpClient(): HttpClient
142
    {
143
        return $this->clientConfigurator->createConfiguredClient();
144
    }
145
}
146