Skip to content

Commit

Permalink
Merge pull request #1 from diglin/feature/compatibility-akeneo-7
Browse files Browse the repository at this point in the history
Feature/compatibility akeneo 7
  • Loading branch information
GautierDig authored Sep 12, 2024
2 parents 0ec48f9 + 916063c commit ef8d71e
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 201 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.zsh_config
/.idea/
55 changes: 19 additions & 36 deletions Component/Catalog/Updater/Setter/UnrestrictedCreateOptionValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,38 @@

namespace Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter;

use Akeneo\Bundle\StorageUtilsBundle\Doctrine\Common\Saver\BaseSaver;
use Akeneo\Component\StorageUtils\Factory\SimpleFactoryInterface;
use Pim\Component\Catalog\Updater\AttributeOptionUpdater;
use Akeneo\Bundle\StorageUtilsBundle\Doctrine\Common\Detacher\ObjectDetacher;
use Akeneo\Pim\Structure\Component\Updater\AttributeOptionUpdater;
use Akeneo\Tool\Bundle\StorageUtilsBundle\Doctrine\Common\Detacher\ObjectDetacher;
use Akeneo\Tool\Bundle\StorageUtilsBundle\Doctrine\Common\Saver\BaseSaver;
use Akeneo\Tool\Component\StorageUtils\Factory\SimpleFactory;

class UnrestrictedCreateOptionValue
{
/** @var SimpleFactoryInterface */
protected $optionValueFactory;

/**
* @var ObjectDetacher
*/
protected $objectDetacher;

/**
* UnrestrictedCreateOptionValue constructor.
* @param SimpleFactoryInterface $optionValueFactory
* @param AttributeOptionUpdater $attributeOptionUpdater
* @param BaseSaver $baseSaver
*/
public function __construct(
SimpleFactoryInterface $optionValueFactory,
AttributeOptionUpdater $attributeOptionUpdater,
BaseSaver $baseSaver,
ObjectDetacher $objectDetacher
)
{
$this->optionValueFactory = $optionValueFactory;
$this->attributeOptionUpdater = $attributeOptionUpdater;
$this->baseSaver = $baseSaver;
$this->objectDetacher = $objectDetacher;
private SimpleFactory $optionValueFactory,
private AttributeOptionUpdater $attributeOptionUpdater,
private BaseSaver $baseSaver,
private ObjectDetacher $objectDetacher
) {

}

/**
* @param $codeAttribute
* @param $code
*/
public function createOptionValue($codeAttribute, $code) {
public function createOptionValue($codeAttribute, $code): void
{
$attributeOptionValue = $this->optionValueFactory->create();

//todo: make the label locales dynamic by detecting the enabled locales
//todo: make the sort_order dynamic by getting the highest sort_order available for this attribute
$tab = [
'attribute' => $codeAttribute,
'code' => $code,
'sort_order' => 2,
'labels' => [
'de_DE' => $code,
'de_CH' => $code,
'fr_CH' => $code,
'it_CH' => $code,
'en_GB' => $code,
'en_US' => $code,
'fr_FR' => $code,
]
];

Expand All @@ -59,4 +42,4 @@ public function createOptionValue($codeAttribute, $code) {

$this->objectDetacher->detach($attributeOptionValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,39 @@

namespace Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter;

use Akeneo\Component\StorageUtils\Repository\IdentifiableObjectRepositoryInterface;
use Pim\Component\Catalog\Builder\EntityWithValuesBuilderInterface;
use Pim\Component\Catalog\Model\AttributeInterface;
use Pim\Component\Catalog\Model\EntityWithValuesInterface;
use Pim\Component\Catalog\Repository\AttributeOptionRepositoryInterface;
use Pim\Component\Catalog\Updater\Setter\AttributeSetter as BaseMultiSelectAttributeSetter;
use Akeneo\Pim\Enrichment\Component\Product\Model\EntityWithValuesInterface;
use Akeneo\Pim\Structure\Bundle\Doctrine\ORM\Repository\AttributeOptionRepository;
use Akeneo\Pim\Enrichment\Component\Product\Updater\Setter\AttributeSetter as BaseMultiSelectAttributeSetter;
use Akeneo\Pim\Structure\Component\Model\AttributeInterface;

class UnrestrictedMultiSelectAttributeSetter extends BaseMultiSelectAttributeSetter
{
/** @var AttributeOptionRepositoryInterface */
protected $attrOptionRepository;

/** @var UnrestrictedCreateOptionValue */
protected $unrestrictedCreateOptionValue;

/**
* UnrestrictedMultiSelectAttributeSetter constructor.
* @param EntityWithValuesBuilderInterface $entityWithValuesBuilder
* @param $supportedTypes
* @param AttributeOptionRepositoryInterface $identifiableObjectRepositoryInterface
* @param \Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter\UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue
*/
public function __construct(
EntityWithValuesBuilderInterface $entityWithValuesBuilder,
$supportedTypes,
AttributeOptionRepositoryInterface $identifiableObjectRepositoryInterface,
UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue
)
{
$this->attrOptionRepository = $identifiableObjectRepositoryInterface;
$this->unrestrictedCreateOptionValue = $unrestrictedCreateOptionValue;
protected $entityWithValuesBuilder,
protected $supportedTypes,
private AttributeOptionRepository $attrOptionRepository,
private UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue
) {
parent::__construct($entityWithValuesBuilder, $supportedTypes);
}


/**
* @param EntityWithValuesInterface $entityWithValues
* @param AttributeInterface $attribute
* @param mixed $data
* @param array $options
*/
public function setAttributeData(
EntityWithValuesInterface $entityWithValues,
AttributeInterface $attribute,
$data,
array $options = []
) {
if (null === $data) {
$option = null;
} else {
AttributeInterface $attribute,
$data,
array $options = []
): void {
if ($data !== null) {
$data = preg_replace('/[^a-zA-Z0-9\']/', '_', $data);
$this->checkOption($attribute, $data);
}

parent::setAttributeData($entityWithValues, $attribute, $data, $options);
}

/**
* @param AttributeInterface $attribute
* @param $datas
*/
protected function checkOption(AttributeInterface $attribute, $datas)
protected function checkOption(AttributeInterface $attribute, $datas): void
{
if (null === $datas) {
if ($datas === null) {
return;
}

Expand All @@ -78,4 +47,4 @@ protected function checkOption(AttributeInterface $attribute, $datas)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,29 @@

namespace Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter;

use Akeneo\Component\StorageUtils\Detacher\ObjectDetacherInterface;
use Pim\Component\Catalog\Builder\EntityWithValuesBuilderInterface;
use Pim\Component\Catalog\Model\AttributeInterface;
use Pim\Component\Catalog\Model\EntityWithValuesInterface;
use Pim\Component\Catalog\Repository\AttributeOptionRepositoryInterface;
use Pim\Component\Catalog\Updater\Setter\AttributeSetter as BaseSimpleSelectAttributeSetter;
use Akeneo\Pim\Enrichment\Component\Product\Model\EntityWithValuesInterface;
use Akeneo\Pim\Structure\Bundle\Doctrine\ORM\Repository\AttributeOptionRepository;
use Akeneo\Pim\Enrichment\Component\Product\Updater\Setter\AttributeSetter as BaseSimpleSelectAttributeSetter;
use Akeneo\Pim\Structure\Component\Model\AttributeInterface;

class UnrestrictedSimpleSelectAttributeSetter extends BaseSimpleSelectAttributeSetter
{
/** @var \Doctrine\ORM\EntityRepository */
protected $attrOptionRepository;

/** @var UnrestrictedCreateOptionValue */
protected $unrestrictedCreateOptionValue;

/** @var ObjectDetacherInterface */
protected $objectDetacher;

/**
* UnrestrictedSimpleSelectAttributeSetter constructor.
* @param EntityWithValuesBuilderInterface $entityWithValuesBuilder
* @param $supportedTypes
* @param AttributeOptionRepositoryInterface $identifiableObjectRepositoryInterface
* @param \Niji\AutoAttributeOptionsSetterBundle\Component\Catalog\Updater\Setter\UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue
*/
public function __construct(
EntityWithValuesBuilderInterface $entityWithValuesBuilder,
$supportedTypes,
AttributeOptionRepositoryInterface $identifiableObjectRepositoryInterface,
UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue
)
{
$this->attrOptionRepository = $identifiableObjectRepositoryInterface;
$this->unrestrictedCreateOptionValue = $unrestrictedCreateOptionValue;
protected $entityWithValuesBuilder,
protected $supportedTypes,
private AttributeOptionRepository $attrOptionRepository,
private UnrestrictedCreateOptionValue $unrestrictedCreateOptionValue
) {
parent::__construct($entityWithValuesBuilder, $supportedTypes);
}


/**
* @param EntityWithValuesInterface $entityWithValues
* @param AttributeInterface $attribute
* @param mixed $data
* @param array $options
*/
public function setAttributeData(
EntityWithValuesInterface $entityWithValues,
AttributeInterface $attribute,
$data,
array $options = []
) {
if (null === $data) {
$option = null;
} else {
AttributeInterface $attribute,
$data,
array $options = []
): void {
if ($data !== null) {
$data = preg_replace('/[^a-zA-Z0-9\']/', '_', $data);

$identifier = $attribute->getCode() . '.' . $data;
Expand All @@ -67,4 +37,4 @@ public function setAttributeData(

parent::setAttributeData($entityWithValues, $attribute, $data, $options);
}
}
}
5 changes: 0 additions & 5 deletions DependencyInjection/AutoAttributeOptionsSetterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class AutoAttributeOptionsSetterExtension extends Extension
{
/**
Expand Down
17 changes: 10 additions & 7 deletions Doctrine/ORM/Repository/AttributeOptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Niji\AutoAttributeOptionsSetterBundle\Doctrine\ORM\Repository;

use Pim\Bundle\CatalogBundle\Doctrine\ORM\Repository\AttributeOptionRepository as BaseAttributeOptionRepository;
use Akeneo\Pim\Structure\Bundle\Doctrine\ORM\Repository\AttributeOptionRepository as BaseAttributeOptionRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;

class AttributeOptionRepository extends BaseAttributeOptionRepository
{
Expand All @@ -15,14 +17,15 @@ class AttributeOptionRepository extends BaseAttributeOptionRepository
* @return bool
* TRUE if the option exists, FALSE otherwise.
*
* @throws \Doctrine\ORM\NoResultException
* @throws NoResultException
* In case of no results found.
* @throws \Doctrine\ORM\NonUniqueResultException
* In case of non unique result found.
* @throws NonUniqueResultException
* In case of non-unique result found.
*/
public function optionExists($code) {
public function optionExists($code): bool
{
if (null === $code) {
return FALSE;
return false;
}

list($attributeCode, $optionCode) = explode('.', $code);
Expand All @@ -39,4 +42,4 @@ public function optionExists($code) {

return (isset($count) && intval($count) > 0);
}
}
}
5 changes: 3 additions & 2 deletions EventListener/ClassMetadataListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
$classMetadata = $eventArgs->getClassMetadata();

$entityName = $classMetadata->getName();
if ('Pim\Bundle\CatalogBundle\Entity\AttributeOption' === $entityName) {
//if ($entityName === 'Pim\Bundle\CatalogBundle\Entity\AttributeOption') {
if ($entityName === 'Akeneo\Pim\Structure\Component\Model\AttributeOption') {
$classMetadata->customRepositoryClassName = 'Niji\AutoAttributeOptionsSetterBundle\Doctrine\ORM\Repository\AttributeOptionRepository';
}
}
}
}
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,12 @@ This bundle ensure that attribute options are created automatically when importi

`composer require niji/auto-attribute-options-setter-bundle`

- In your app/AppKernel.php add a line to enable the bundle:
- In your bundles.php add a line to enable the bundle:

```php
public function registerProjectBundles() {
return [
// your app bundles should be registered here,
.../...
new Niji\AutoAttributeOptionsSetterBundle\AutoAttributeOptionsSetterBundle(),
.../...
];
}
Niji\AutoAttributeOptionsSetterBundle\AutoAttributeOptionsSetterBundle::class => ['all' => true]
```

## Credits

This bundle is brought to you by Niji - http://www.niji.fr.
This bundle is brought to you by Niji - http://www.niji.fr And Diglin GmbH - https://www.diglin.com
4 changes: 2 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ parameters:
services:
pim_catalog.repository.attribute_option:
class: '%pim_catalog.repository.attribute_option.class%'
factory: 'doctrine.orm.entity_manager:getRepository'
factory: ['@doctrine.orm.entity_manager', 'getRepository']
arguments: ['%pim_catalog.entity.attribute_option.class%']
tags:
- { name: 'pim_repository' }

unrestricted_select_attribute_catalog.listener.class_metadata:
class: Niji\AutoAttributeOptionsSetterBundle\EventListener\ClassMetadataListener
tags:
- { name: doctrine.event_listener, event: loadClassMetadata }
- { name: doctrine.event_listener, event: loadClassMetadata }
Loading

0 comments on commit ef8d71e

Please sign in to comment.