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