|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace CommerceLeague\ActiveCampaignApi; |
|
7
|
|
|
|
|
8
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\AbandonedCartApi; |
|
9
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\ConnectionApi; |
|
10
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\ContactApi; |
|
11
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\CustomerApi; |
|
12
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\DealApi; |
|
13
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\ListsApi; |
|
14
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\OrderApi; |
|
15
|
|
|
use CommerceLeague\ActiveCampaignApi\Api\TagsApi; |
|
16
|
|
|
use CommerceLeague\ActiveCampaignApi\Client\AuthenticatedCommonClient; |
|
17
|
|
|
use CommerceLeague\ActiveCampaignApi\Client\HttpClient; |
|
18
|
|
|
use CommerceLeague\ActiveCampaignApi\Client\CommonResourceClient; |
|
19
|
|
|
use CommerceLeague\ActiveCampaignApi\Configuration\CommonConfiguration; |
|
20
|
|
|
use CommerceLeague\ActiveCampaignApi\Paginator\PageFactory; |
|
21
|
|
|
use CommerceLeague\ActiveCampaignApi\Paginator\ResourceCursorFactory; |
|
22
|
|
|
use CommerceLeague\ActiveCampaignApi\Routing\UriGenerator; |
|
23
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
|
24
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
|
25
|
|
|
use Psr\Http\Client\ClientInterface; |
|
26
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
27
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class ClientBuilder |
|
31
|
|
|
*/ |
|
32
|
|
|
class ClientBuilder |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var null|ClientInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $httpClient; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var null|RequestFactoryInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $requestFactory; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var null|StreamFactoryInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
private $streamFactory; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return ClientInterface |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getHttpClient(): ClientInterface |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->httpClient === null) { |
|
56
|
|
|
$this->httpClient = Psr18ClientDiscovery::find(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return $this->httpClient; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param ClientInterface $httpClient |
|
64
|
|
|
* |
|
65
|
|
|
* @return ClientBuilder |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setHttpClient(ClientInterface $httpClient): self |
|
68
|
|
|
{ |
|
69
|
|
|
$this->httpClient = $httpClient; |
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return RequestFactoryInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getRequestFactory(): RequestFactoryInterface |
|
77
|
|
|
{ |
|
78
|
|
|
if ($this->requestFactory === null) { |
|
79
|
|
|
$this->requestFactory = Psr17FactoryDiscovery::findRequestFactory(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->requestFactory; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param RequestFactoryInterface $requestFactory |
|
87
|
|
|
* |
|
88
|
|
|
* @return ClientBuilder |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setRequestFactory(RequestFactoryInterface $requestFactory): self |
|
91
|
|
|
{ |
|
92
|
|
|
$this->requestFactory = $requestFactory; |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return StreamFactoryInterface |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getStreamFactory(): StreamFactoryInterface |
|
100
|
|
|
{ |
|
101
|
|
|
if (null === $this->streamFactory) { |
|
102
|
|
|
$this->streamFactory = Psr17FactoryDiscovery::findStreamFactory(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->streamFactory; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param StreamFactoryInterface $streamFactory |
|
110
|
|
|
* |
|
111
|
|
|
* @return ClientBuilder |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setStreamFactory(StreamFactoryInterface $streamFactory): self |
|
114
|
|
|
{ |
|
115
|
|
|
$this->streamFactory = $streamFactory; |
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $baseUri |
|
121
|
|
|
* @param string $token |
|
122
|
|
|
* |
|
123
|
|
|
* @return CommonClient |
|
124
|
|
|
*/ |
|
125
|
|
|
public function buildCommonClient(string $baseUri, string $token): CommonClient |
|
126
|
|
|
{ |
|
127
|
|
|
$configuration = CommonConfiguration::build($baseUri, $token); |
|
128
|
|
|
list($resourceClient, $pageFactory, $cursorFactory) = $this->setUpCommonClient($configuration); |
|
129
|
|
|
|
|
130
|
|
|
return new CommonClient( |
|
131
|
|
|
new AbandonedCartApi($resourceClient), |
|
132
|
|
|
new ConnectionApi($resourceClient, $pageFactory, $cursorFactory), |
|
133
|
|
|
new ContactApi($resourceClient, $pageFactory, $cursorFactory), |
|
134
|
|
|
new CustomerApi($resourceClient, $pageFactory, $cursorFactory), |
|
135
|
|
|
new DealApi($resourceClient, $pageFactory, $cursorFactory), |
|
136
|
|
|
new OrderApi($resourceClient, $pageFactory, $cursorFactory), |
|
137
|
|
|
new TagsApi($resourceClient, $pageFactory, $cursorFactory), |
|
138
|
|
|
new ListsApi($resourceClient, $pageFactory, $cursorFactory) |
|
139
|
|
|
); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param CommonConfiguration $configuration |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
private function setUpCommonClient(CommonConfiguration $configuration): array |
|
148
|
|
|
{ |
|
149
|
|
|
$uriGenerator = new UriGenerator($configuration->getBaseUri()); |
|
150
|
|
|
$httpClient = new HttpClient( |
|
151
|
|
|
$this->getHttpClient(), |
|
152
|
|
|
$this->getRequestFactory(), |
|
153
|
|
|
$this->getStreamFactory() |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
$authenticatedHttpClient = new AuthenticatedCommonClient($configuration, $httpClient); |
|
157
|
|
|
$resourceClient = new CommonResourceClient($authenticatedHttpClient, $uriGenerator); |
|
158
|
|
|
$pageFactory = new PageFactory($authenticatedHttpClient); |
|
159
|
|
|
$cursorFactory = new ResourceCursorFactory(); |
|
160
|
|
|
|
|
161
|
|
|
return [$resourceClient, $pageFactory, $cursorFactory]; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|