Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use factory class to create AkeneoPimClientBuilder instance #356

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions Helper/Authenticator.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php

declare(strict_types=1);

namespace Akeneo\Connector\Helper;

use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder;
use Akeneo\Connector\Model\AkeneoPimClientBuilderFactory;
use Akeneo\Connector\Helper\Config as ConfigHelper;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Http\Adapter\Guzzle6\Client;
use Http\Factory\Guzzle\StreamFactory;
use Http\Factory\Guzzle\RequestFactory;
Expand All @@ -21,22 +23,20 @@
*/
class Authenticator
{
/**
* This variable contains a ConfigHelper
*
* @var ConfigHelper $configHelper
*/
protected $configHelper;
/** @var ConfigHelper $configHelper */
private $configHelper;

/** @var AkeneoPimClientBuilderFactory $clientBuilderFactory */
private $clientBuilderFactory;

/**
* Authenticator constructor
*
* @param ConfigHelper $configHelper
* @param Config $configHelper
* @param AkeneoPimClientBuilderFactory $clientBuilderFactory
*/
public function __construct(
ConfigHelper $configHelper
) {
public function __construct(ConfigHelper $configHelper, AkeneoPimClientBuilderFactory $clientBuilderFactory)
{
$this->configHelper = $configHelper;
$this->clientBuilderFactory = $clientBuilderFactory;
}

/**
Expand All @@ -46,24 +46,17 @@ public function __construct(
*/
public function getAkeneoApiClient()
{
/** @var string $baseUri */
$baseUri = $this->configHelper->getAkeneoApiBaseUrl();
/** @var string $clientId */
$clientId = $this->configHelper->getAkeneoApiClientId();
/** @var string $secret */
$secret = $this->configHelper->getAkeneoApiClientSecret();
/** @var string $username */
$username = $this->configHelper->getAkeneoApiUsername();
/** @var string $password */
$password = $this->configHelper->getAkeneoApiPassword();

if (!$baseUri || !$clientId || !$secret || !$username || !$password) {
return false;
}

/** @var AkeneoPimClientBuilder $akeneoClientBuilder */
$akeneoClientBuilder = new AkeneoPimClientBuilder($baseUri);

$akeneoClientBuilder = $this->clientBuilderFactory->create(['baseUri' => $baseUri]);
$akeneoClientBuilder->setHttpClient(new Client());
$akeneoClientBuilder->setStreamFactory(new StreamFactory());
$akeneoClientBuilder->setRequestFactory(new RequestFactory());
Expand Down
40 changes: 40 additions & 0 deletions Model/AkeneoPimClientBuilderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Akeneo\Connector\Model;

use Akeneo\Pim\ApiClient\AkeneoPimClientBuilder;
use Akeneo\PimEnterprise\ApiClient\AkeneoPimEnterpriseClientBuilder;
use Magento\Framework\ObjectManagerInterface;

class AkeneoPimClientBuilderFactory
{
/** @var ObjectManagerInterface $objectManager */
private $objectManager = null;

/** @var string $instanceName */
private $instanceName = null;

/**
* @param ObjectManagerInterface $objectManager
* @param string $instanceName
*/
public function __construct(ObjectManagerInterface $objectManager, $instanceName = AkeneoPimClientBuilder::class)
{
$this->objectManager = $objectManager;
$this->instanceName = $instanceName;
}

/**
* Create class instance with specified parameters.
*
* @param array $data
*
* @return AkeneoPimClientBuilder|AkeneoPimEnterpriseClientBuilder
*/
public function create(array $data = [])
{
return $this->objectManager->create($this->instanceName, $data);
}
}