Skip to content

Commit

Permalink
Merge branch 'release/103.5.0' into 'master'
Browse files Browse the repository at this point in the history
release/103.5.0 into master

See merge request agence-dnd/marketplace/magento-2/external/magento2-connector-community!97
  • Loading branch information
magentix committed Jul 6, 2023
2 parents a942321 + 88030aa commit e68eeda
Show file tree
Hide file tree
Showing 9 changed files with 590 additions and 336 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,17 @@
* Rolled back previous modification to avoid Undefined array key line 3013
* Avoid to erase swatch configuration
* Fix image mapping for scope global

### Version 103.5.0 :
* Select category trees to import instead of trees to exclude
* Select families to import instead of families to exclude
* Assets compatibility warning removed
* Allow to override default attribute values (like tax_class_id)
* Allow attribute named visibility in Akeneo
* Fix import products without name grid error
* Fix status assignation with virtual products
* Fix visibility attribute error
* Fix association when category does not exist
* Prevent import from crashing if the tmp table is missing sort_order column
* Keep the old sort_order for options if the order from Akeneo is null
* setCategory step improvement
37 changes: 23 additions & 14 deletions Helper/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
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)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.dnd.fr/
*/
class Authenticator
Expand All @@ -24,12 +28,17 @@ class Authenticator
*
* @var ConfigHelper $configHelper
*/
protected $configHelper;
protected Config $configHelper;

/**
* @var AkeneoPimClientInterface|null $akeneoClient
*/
private ?AkeneoPimClientInterface $akeneoClient = null;

/**
* Authenticator constructor
*
* @param ConfigHelper $configHelper
* @param Config $configHelper
*/
public function __construct(
ConfigHelper $configHelper
Expand All @@ -40,32 +49,32 @@ public function __construct(
/**
* Retrieve an authenticated akeneo php client
*
* @return AkeneoPimClientInterface|false
* @return AkeneoPimClientInterface|null
* @throws Exception
*/
public function getAkeneoApiClient()
public function getAkeneoApiClient(): ?AkeneoPimClientInterface
{
/** @var string $baseUri */
if ($this->akeneoClient !== null) {
return $this->akeneoClient;
}

$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;
return null;
}

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

$akeneoClientBuilder->setHttpClient(new Psr18Client());
$akeneoClientBuilder->setStreamFactory(new StreamFactory());
$akeneoClientBuilder->setRequestFactory(new RequestFactory());

return $akeneoClientBuilder->buildAuthenticatedByPassword($clientId, $secret, $username, $password);
$this->akeneoClient = $akeneoClientBuilder->buildAuthenticatedByPassword($clientId, $secret, $username, $password);

return $this->akeneoClient;
}
}
20 changes: 2 additions & 18 deletions Helper/CategoryFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,7 @@ public function getParentFilters()
*/
public function getCategoriesToImport()
{
/** @var string $excludedCategories */
$excludedCategories = $this->configHelper->getCategoriesFilter();
/** @var string[] $allParentCategories */
$allParentCategories = array_keys($this->categoryFilterSourceModel->getCategories());
/** @var string[] $categoriesToImport */
$categoriesToImport = [];
if ($excludedCategories) {
/** @var string[] $explodedCategories */
$explodedCategories = explode(',', $excludedCategories ?? '');
$categoriesToImport = array_diff($allParentCategories, $explodedCategories);
} else {
$categoriesToImport = $allParentCategories;
}

$categoriesToImport = array_map('strval', $categoriesToImport);

return $categoriesToImport;
return explode(',', $this->configHelper->getCategoriesFilter() ?? '');
}

/**
Expand All @@ -144,7 +128,7 @@ public function getChildFilters(array $parent)

$this->searchBuilder->addFilter('parent', '=', $parent['code']);
/** @var string[] $search */
$search = $this->searchBuilder->getFilters();
$search = $this->searchBuilder->getFilters();
$filters = [
'search' => $search,
];
Expand Down
44 changes: 38 additions & 6 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ class Config
* @var string PRODUCTS_FILTERS_FAMILIES
*/
public const PRODUCTS_FILTERS_FAMILIES = 'akeneo_connector/products_filters/families';
/**
* Product filters families config path
*
* @var string PRODUCTS_FILTERS_INCLUDED_FAMILIES
*/
public const PRODUCTS_FILTERS_INCLUDED_FAMILIES = 'akeneo_connector/products_filters/included_families';
/**
* Product filters updated mode config path
*
Expand Down Expand Up @@ -215,6 +221,12 @@ class Config
* @var string PRODUCTS_CATEGORY_CATEGORIES
*/
public const PRODUCTS_CATEGORY_CATEGORIES = 'akeneo_connector/category/categories';
/**
* Categories to import config path
*
* @var string PRODUCTS_CATEGORY_INCLUDED_CATEGORIES
*/
public const PRODUCTS_CATEGORY_INCLUDED_CATEGORIES = 'akeneo_connector/category/included_categories';
/**
* Categories does override content staging
*
Expand Down Expand Up @@ -913,15 +925,25 @@ public function getAttributeFilterByCode()
}

/**
* Retrieve the families to filter the products on
* Retrieve the excluded families to filter the products on
*
* @return string
*/
public function getFamiliesFilter()
public function getFamiliesExcludedFilter()
{
return $this->scopeConfig->getValue(self::PRODUCTS_FILTERS_FAMILIES);
}

/**
* Retrieve the included families to filter the products on
*
* @return string
*/
public function getFamiliesFilter()
{
return $this->scopeConfig->getValue(self::PRODUCTS_FILTERS_INCLUDED_FAMILIES);
}

/**
* Retrieve the advance filters
*
Expand Down Expand Up @@ -981,11 +1003,21 @@ public function getIsCategoryAnchor()
*
* @return string
*/
public function getCategoriesFilter()
public function getCategoriesExcludedFilter()
{
return $this->scopeConfig->getValue(self::PRODUCTS_CATEGORY_CATEGORIES);
}

/**
* Retrieve the categories to filter the category import
*
* @return string
*/
public function getCategoriesFilter()
{
return $this->scopeConfig->getValue(self::PRODUCTS_CATEGORY_INCLUDED_CATEGORIES);
}

/**
* Retrieve the categories does override content staging
*
Expand Down Expand Up @@ -1618,17 +1650,17 @@ public function isProductVisibilityEnabled(): bool

public function getProductDefaultVisibility(): string
{
return $this->scopeConfig->getValue(self::PRODUCT_DEFAULT_VISIBILITY);
return (string)$this->scopeConfig->getValue(self::PRODUCT_DEFAULT_VISIBILITY);
}

public function getProductVisibilitySimple(): string
{
return $this->scopeConfig->getValue(self::PRODUCT_VISIBILITY_SIMPLE);
return (string)$this->scopeConfig->getValue(self::PRODUCT_VISIBILITY_SIMPLE);
}

public function getProductVisibilityConfigurable(): string
{
return $this->scopeConfig->getValue(self::PRODUCT_VISIBILITY_CONFIGURABLE);
return (string)$this->scopeConfig->getValue(self::PRODUCT_VISIBILITY_CONFIGURABLE);
}

/**
Expand Down
72 changes: 56 additions & 16 deletions Helper/ProductFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public function getFilters($jobExecutor, $productFamily = null, $isProductModel
$advancedFilters['search']['family'][] = $familyFilter;
}

$updatedFilter = $this->getUpdatedFilter($jobExecutor);
if (!empty($updatedFilter)) {
$advancedFilters['search']['updated'][0] = $updatedFilter;
}

return [$advancedFilters];
}

Expand Down Expand Up @@ -329,13 +334,13 @@ protected function addStatusFilter()
}

/**
* Add updated filter for Akeneo API
* Get updated filter Data for Akeneo API
*
* @param JobExecutor $jobExecutor
*
* @return void
* @return mixed[]
*/
protected function addUpdatedFilter($jobExecutor)
protected function getUpdatedFilter($jobExecutor)
{
/** @var string $mode */
$mode = $this->configHelper->getUpdatedMode();
Expand All @@ -344,41 +349,52 @@ protected function addUpdatedFilter($jobExecutor)
$dateAfter = $this->configHelper->getUpdatedBetweenAfterFilter() . ' 00:00:00';
$dateBefore = $this->configHelper->getUpdatedBetweenBeforeFilter() . ' 23:59:59';
if (empty($dateAfter) || empty($dateBefore)) {
return;
return [];
}
$dates = [$dateAfter, $dateBefore];
$this->searchBuilder->addFilter('updated', $mode, $dates);

return [
'operator' => $mode,
'value' => $dates,
];
}
if ($mode == Update::SINCE_LAST_N_DAYS) {
/** @var string $filter */
$filter = $this->configHelper->getUpdatedSinceFilter();
if (!is_numeric($filter)) {
return;
return [];
}
$this->searchBuilder->addFilter('updated', $mode, (int)$filter);

return [
'operator' => $mode,
'value' => (int)$filter,
];
}
if ($mode == Update::SINCE_LAST_IMPORT) {
// Get the last import date as filter
/** @var string $filter */
$filter = $this->getLastImportDateFilter($jobExecutor);
if (!$filter) {
return;
return [];
}

$this->searchBuilder->addFilter('updated', Update::GREATER_THAN, $filter);
return [
'operator' => Update::GREATER_THAN,
'value' => $filter,
];
}
if ($mode == Update::SINCE_LAST_N_HOURS) {
/** @var int $currentDateTime */
$currentDateTime = $this->timezone->date()->getTimestamp();
/** @var string $valueConfig */
$valueConfig = $this->configHelper->getUpdatedSinceLastHoursFilter();
if (!$valueConfig) {
return;
return [];
}
/** @var int $filter */
$filter = ((int)$valueConfig) * 3600;
if (!is_numeric($filter)) {
return;
return [];
}

/** @var int $timestamp */
Expand All @@ -387,29 +403,53 @@ protected function addUpdatedFilter($jobExecutor)
$date = (new \DateTime())->setTimestamp($timestamp)->format('Y-m-d H:i:s');

if (!empty($date)) {
$this->searchBuilder->addFilter('updated', Update::GREATER_THAN, $date);
return [
'operator' => Update::GREATER_THAN,
'value' => $date,
];
}

return;
return [];
}
if ($mode == Update::LOWER_THAN) {
/** @var string $date */
$date = $this->configHelper->getUpdatedLowerFilter();
if (empty($date)) {
return;
return [];
}
$date = $date . ' 23:59:59';
}
if ($mode == Update::GREATER_THAN) {
$date = $this->configHelper->getUpdatedGreaterFilter();
if (empty($date)) {
return;
return [];
}
$date = $date . ' 00:00:00';
}
if (!empty($date)) {
$this->searchBuilder->addFilter('updated', $mode, $date);
return [
'operator' => $mode,
'value' => $date,
];
}
}

/**
* Add updated filter for Akeneo API
*
* @param JobExecutor $jobExecutor
*
* @return void
*/
protected function addUpdatedFilter($jobExecutor)
{
$updatedFilter = $this->getUpdatedFilter($jobExecutor);

if (empty($updatedFilter)) {
return;
}

$this->searchBuilder->addFilter('updated', $updatedFilter['operator'], $updatedFilter['value']);
}

/**
Expand Down
Loading

0 comments on commit e68eeda

Please sign in to comment.