-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathAuthenticator.php
80 lines (68 loc) · 2.3 KB
/
Authenticator.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
<?php
declare(strict_types=1);
namespace Akeneo\Connector\Helper;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder;
use Akeneo\Connector\Helper\Config as ConfigHelper;
use Exception;
use Http\Factory\Guzzle\StreamFactory;
use Http\Factory\Guzzle\RequestFactory;
use Symfony\Component\HttpClient\Psr18Client;
/**
* Class Authenticator
*
* @package Akeneo\Connector\Helper
* @author Agence Dn'D <[email protected]>
* @copyright 2004-present Agence Dn'D
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.dnd.fr/
*/
class Authenticator
{
/**
* This variable contains a ConfigHelper
*
* @var ConfigHelper $configHelper
*/
protected Config $configHelper;
/**
* @var AkeneoPimClientInterface|null $akeneoClient
*/
private ?AkeneoPimClientInterface $akeneoClient = null;
/**
* Authenticator constructor
*
* @param Config $configHelper
*/
public function __construct(
ConfigHelper $configHelper
) {
$this->configHelper = $configHelper;
}
/**
* Retrieve an authenticated akeneo php client
*
* @return AkeneoPimClientInterface|null
* @throws Exception
*/
public function getAkeneoApiClient(): ?AkeneoPimClientInterface
{
if ($this->akeneoClient !== null) {
return $this->akeneoClient;
}
$baseUri = $this->configHelper->getAkeneoApiBaseUrl();
$clientId = $this->configHelper->getAkeneoApiClientId();
$secret = $this->configHelper->getAkeneoApiClientSecret();
$username = $this->configHelper->getAkeneoApiUsername();
$password = $this->configHelper->getAkeneoApiPassword();
if (!$baseUri || !$clientId || !$secret || !$username || !$password) {
return null;
}
$akeneoClientBuilder = new AkeneoPimClientBuilder($baseUri);
$akeneoClientBuilder->setHttpClient(new Psr18Client());
$akeneoClientBuilder->setStreamFactory(new StreamFactory());
$akeneoClientBuilder->setRequestFactory(new RequestFactory());
$this->akeneoClient = $akeneoClientBuilder->buildAuthenticatedByPassword($clientId, $secret, $username, $password);
return $this->akeneoClient;
}
}