-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClientBuilder.php
More file actions
100 lines (84 loc) · 3.06 KB
/
ClientBuilder.php
File metadata and controls
100 lines (84 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare(strict_types=1);
namespace ClickAndMortar\MagentoApiClient;
use ClickAndMortar\MagentoApiClient\Api\Customers;
use ClickAndMortar\MagentoApiClient\Api\Orders;
use ClickAndMortar\MagentoApiClient\Api\Products;
use ClickAndMortar\MagentoApiClient\Api\ProductAttributes;
use ClickAndMortar\MagentoApiClient\Api\StoreViews;
use ClickAndMortar\MagentoApiClient\Client\HttpClient;
use ClickAndMortar\MagentoApiClient\Client\ResourceClient;
use ClickAndMortar\MagentoApiClient\Pagination\PageFactory;
use ClickAndMortar\MagentoApiClient\Pagination\ResourceCursorFactory;
use ClickAndMortar\MagentoApiClient\Security\Authentication;
use ClickAndMortar\MagentoApiClient\Utils\UriGenerator;
use Http\Discovery\Psr17FactoryDiscovery;
use Http\Discovery\Psr18ClientDiscovery;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
class ClientBuilder
{
private ?\Psr\Http\Client\ClientInterface $httpClient = null;
private ?RequestFactoryInterface $requestFactory = null;
private ?StreamFactoryInterface $streamFactory = null;
public function __construct(
protected string $baseUrl,
protected array $options = []
)
{
}
public function buildAuthenticatedByOauth(
string $consumerKey,
string $consumerSecret,
string $accessToken,
string $accessTokenSecret
): Client
{
$authentication = Authentication::fromOauthCredentials(
$consumerKey,
$consumerSecret,
$accessToken,
$accessTokenSecret
);
$uriGenerator = new UriGenerator($this->baseUrl);
$httpClient = new HttpClient(
$this->getHttpClient(),
$this->getRequestFactory(),
$this->getStreamFactory(),
$authentication
);
$resourceClient = new ResourceClient(
$httpClient,
$uriGenerator
);
$resourceCursorFactory = new ResourceCursorFactory();
return new Client(
new Orders($resourceClient, $resourceCursorFactory),
new Customers($resourceClient, $resourceCursorFactory),
new StoreViews($resourceClient, $resourceCursorFactory),
new Products($resourceClient, $resourceCursorFactory),
new ProductAttributes($resourceClient, $resourceCursorFactory)
);
}
private function getHttpClient(): \Psr\Http\Client\ClientInterface
{
if (null === $this->httpClient) {
$this->httpClient = Psr18ClientDiscovery::find();
}
return $this->httpClient;
}
private function getRequestFactory(): RequestFactoryInterface
{
if (null === $this->requestFactory) {
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory();
}
return $this->requestFactory;
}
private function getStreamFactory(): StreamFactoryInterface
{
if (null === $this->streamFactory) {
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory();
}
return $this->streamFactory;
}
}