forked from oroinc/OroAkeneoBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAkeneoClientFactory.php
164 lines (133 loc) · 4.69 KB
/
AkeneoClientFactory.php
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Oro\Bundle\AkeneoBundle\Client;
use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Oro\Bundle\AkeneoBundle\Encoder\Crypter;
use Oro\Bundle\AkeneoBundle\Entity\AkeneoSettings;
use Oro\Bundle\EntityBundle\ORM\DoctrineHelper;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
/**
* Factory to create Akeneo PIM client instance.
*/
class AkeneoClientFactory
{
/** @var Crypter */
private $crypter;
/** @var string */
private $akeneoUrl;
/** @var string */
private $clientId;
/** @var string */
private $secret;
/** @var string */
private $userName;
/** @var string */
private $password;
/** @var string */
private $token;
/** @var string */
private $refreshToken;
/** @var AkeneoPimClientInterface */
private $client;
/** @var AkeneoSettings */
private $akeneoSettings;
/** @var DoctrineHelper */
private $doctrineHelper;
/** @var ClientInterface */
private $httpClient;
/** @var RequestFactoryInterface */
private $requestFactory;
/** @var StreamFactoryInterface */
private $streamFactory;
public function __construct(
DoctrineHelper $doctrineHelper,
Crypter $crypter,
ClientInterface $httpClient,
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory
) {
$this->doctrineHelper = $doctrineHelper;
$this->crypter = $crypter;
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory;
$this->streamFactory = $streamFactory;
}
public function getInstance(AkeneoSettings $akeneoSettings, bool $tokensEnabled = true): AkeneoPimClientInterface
{
$this->initProperties($akeneoSettings);
if (
$tokensEnabled
&& $akeneoSettings->getToken()
&& $akeneoSettings->getTokenExpiryDateTime()
&& $akeneoSettings->getTokenExpiryDateTime() > new \DateTime('now', new \DateTimeZone('UTC'))
) {
$this->createClientByToken();
} else {
$this->createClient();
}
return $this->client;
}
/**
* Set properties from AkeneoSettings entity.
*/
private function initProperties(AkeneoSettings $akeneoSettings): void
{
$this->akeneoSettings = $akeneoSettings;
$this->akeneoUrl = $akeneoSettings->getUrl();
$this->clientId = $akeneoSettings->getClientId();
$this->secret = $akeneoSettings->getSecret();
$this->userName = $akeneoSettings->getUsername();
$this->password = $this->crypter->getDecryptData($akeneoSettings->getPassword());
$this->token = $akeneoSettings->getToken();
$this->refreshToken = $akeneoSettings->getRefreshToken();
}
private function createClientByToken(): AkeneoPimClientInterface
{
$this->client = $this->getClientBuilder()->buildAuthenticatedByToken(
$this->clientId,
$this->secret,
$this->token,
$this->refreshToken
);
return $this->client;
}
private function getClientBuilder(): AkeneoPimClientBuilder
{
$clientBuilder = new AkeneoPimClientBuilder($this->akeneoUrl);
$clientBuilder->setHttpClient($this->httpClient);
$clientBuilder->setRequestFactory($this->requestFactory);
$clientBuilder->setStreamFactory($this->streamFactory);
return $clientBuilder;
}
private function createClient(): AkeneoPimClientInterface
{
$this->client = $this->getClientBuilder()->buildAuthenticatedByPassword(
$this->clientId,
$this->secret,
$this->userName,
$this->password
);
if ($this->akeneoSettings->getId()) {
$this->persistTokens();
}
return $this->client;
}
/**
* Persist authentication tokens.
* Sends request to get currencies. It's needed to fetch token.
*/
private function persistTokens(): void
{
$this->client->getCurrencyApi()->all();
$em = $this->doctrineHelper->getEntityManager($this->akeneoSettings);
$em->getUnitOfWork()->removeFromIdentityMap($this->akeneoSettings);
$em->refresh($this->akeneoSettings);
$this->akeneoSettings->setToken($this->client->getToken());
$this->akeneoSettings->setRefreshToken($this->client->getRefreshToken());
$this->akeneoSettings->setTokenExpiryDateTime(new \DateTime('now +3500 seconds'));
$em->flush($this->akeneoSettings);
$em->getUnitOfWork()->markReadOnly($this->akeneoSettings);
}
}