-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2679 from breakone/variant-creator
Variant Creator
- Loading branch information
Showing
20 changed files
with
688 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/CoreShop/Bundle/VariantBundle/Controller/VariantController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\VariantBundle\Controller; | ||
|
||
use CoreShop\Bundle\ResourceBundle\Controller\AdminController; | ||
use CoreShop\Bundle\VariantBundle\Messenger\CreateVariantMessage; | ||
use CoreShop\Bundle\VariantBundle\Service\VariantGeneratorService; | ||
use CoreShop\Component\Variant\Model\AttributeGroupInterface; | ||
use CoreShop\Component\Variant\Model\AttributeInterface; | ||
use CoreShop\Component\Variant\Model\ProductVariantAwareInterface; | ||
use Pimcore\Model\DataObject; | ||
use Pimcore\Model\DataObject\AbstractObject; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
/** | ||
* @psalm-suppress InternalClass | ||
*/ | ||
class VariantController extends AdminController | ||
{ | ||
public function getAttributesAction(Request $request) | ||
{ | ||
$id = $this->getParameterFromRequest($request, 'id'); | ||
|
||
if (!$id) { | ||
throw new \InvalidArgumentException('no product id given'); | ||
} | ||
|
||
$product = DataObject::getById($id); | ||
|
||
if (!$product instanceof ProductVariantAwareInterface) { | ||
throw new NotFoundHttpException('no product found'); | ||
} | ||
|
||
if (AbstractObject::OBJECT_TYPE_VARIANT === $product->getType()) { | ||
$product = $product->getVariantParent(); | ||
} | ||
|
||
$attributeGroups = $product->getAllowedAttributeGroups(); | ||
|
||
$data = array_map(static function (AttributeGroupInterface $group) { | ||
return [ | ||
'text' => sprintf('%s (ID: %s)', $group->getKey(), $group->getId()), | ||
'sorting' => $group->getSorting(), | ||
'leaf' => false, | ||
'iconCls' => 'pimcore_icon_object', | ||
'data' => array_map(static function (AttributeInterface $attribute) use ($group) { | ||
return [ | ||
'text' => sprintf('%s (ID: %s)', $attribute->getKey(), $attribute->getId()), | ||
'id' => $attribute->getId(), | ||
'group_id' => $group->getId(), | ||
'sorting' => $attribute->getSorting(), | ||
'leaf' => true, | ||
'checked' => false, | ||
'iconCls' => 'pimcore_icon_object', | ||
]; | ||
}, $group->getAttributes()), | ||
]; | ||
}, $attributeGroups); | ||
|
||
return $this->json( | ||
[ | ||
'success' => true, | ||
'data' => $data, | ||
] | ||
); | ||
} | ||
|
||
public function generateVariantsAction( | ||
Request $request, | ||
VariantGeneratorService $variantGeneratorService, | ||
MessageBusInterface $messageBus, | ||
TranslatorInterface $translator, | ||
) { | ||
$id = $this->getParameterFromRequest($request, 'id'); | ||
$attributes = $this->getParameterFromRequest($request, 'attributes'); | ||
|
||
if (!$id) { | ||
throw new \InvalidArgumentException('no product id given'); | ||
} | ||
|
||
if (!$attributes) { | ||
throw new \InvalidArgumentException('no attributes given'); | ||
} | ||
|
||
$product = DataObject::getById($id); | ||
|
||
if (!$product instanceof ProductVariantAwareInterface) { | ||
throw new NotFoundHttpException('no product found'); | ||
} | ||
|
||
if (AbstractObject::OBJECT_TYPE_VARIANT === $product->getType()) { | ||
$product = $product->getVariantParent(); | ||
} | ||
|
||
$combinations = []; | ||
$variantGeneratorService->generateCombinations($attributes, [], 0, $combinations); | ||
|
||
foreach ($combinations as $attributeIds) { | ||
/** | ||
* @psalm-suppress InternalMethod | ||
*/ | ||
$messageBus->dispatch( | ||
new CreateVariantMessage($product->getId(), $attributeIds, $this->getAdminUser()?->getId()) | ||
); | ||
} | ||
|
||
return $this->json( | ||
[ | ||
'success' => true, | ||
'message' => $translator->trans('coreshop.variant_generator.generate_in_background', [], 'admin'), | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/CoreShop/Bundle/VariantBundle/Messenger/CreateVariantMessage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\VariantBundle\Messenger; | ||
|
||
|
||
class CreateVariantMessage | ||
{ | ||
public function __construct( | ||
private int $objectId, | ||
private array $attributeIds, | ||
private ?int $userId = null | ||
) {} | ||
|
||
public function getObjectId(): int | ||
{ | ||
return $this->objectId; | ||
} | ||
|
||
public function setObjectId(int $objectId): void | ||
{ | ||
$this->objectId = $objectId; | ||
} | ||
|
||
public function getAttributeIds(): array | ||
{ | ||
return $this->attributeIds; | ||
} | ||
|
||
public function setAttributeIds(array $attributeIds): void | ||
{ | ||
$this->attributeIds = $attributeIds; | ||
} | ||
|
||
public function getUserId(): ?int | ||
{ | ||
return $this->userId; | ||
} | ||
|
||
public function setUserId(?int $userId): void | ||
{ | ||
$this->userId = $userId; | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/CoreShop/Bundle/VariantBundle/Messenger/Handler/CreateVariantMessageHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* CoreShop | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - CoreShop Commercial License (CCL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) CoreShop GmbH (https://www.coreshop.org) | ||
* @license https://www.coreshop.org/license GPLv3 and CCL | ||
* | ||
*/ | ||
|
||
namespace CoreShop\Bundle\VariantBundle\Messenger\Handler; | ||
|
||
use CoreShop\Bundle\VariantBundle\Messenger\CreateVariantMessage; | ||
use CoreShop\Bundle\VariantBundle\Service\VariantGeneratorServiceInterface; | ||
use CoreShop\Component\Variant\Model\ProductVariantAwareInterface; | ||
use Pimcore\Model\DataObject; | ||
use Pimcore\Model\Notification\Service\NotificationService; | ||
|
||
class CreateVariantMessageHandler | ||
{ | ||
public function __construct( | ||
protected VariantGeneratorServiceInterface $variantGeneratorService, | ||
protected NotificationService $notificationService | ||
) { | ||
} | ||
|
||
public function __invoke(CreateVariantMessage $message) | ||
{ | ||
$object = DataObject::getById($message->getObjectId()); | ||
|
||
if (!$object instanceof ProductVariantAwareInterface) { | ||
return; | ||
} | ||
|
||
$attributeIds = $message->getAttributeIds(); | ||
if (!$attributeIds) { | ||
return; | ||
} | ||
|
||
$variant = $this->variantGeneratorService->generateVariant($attributeIds, $object); | ||
|
||
if (null !== $variant && null !== $message->getUserId()) { | ||
/** | ||
* @psalm-suppress InternalMethod | ||
*/ | ||
$this->notificationService->sendToUser( | ||
$message->getUserId(), | ||
0, | ||
sprintf('Variant %s generated', $variant->getName()), | ||
sprintf('Variant %s with ID %s for Product %s with ID %s has been generated', $variant->getName(), | ||
$variant->getId(), $object->getKey(), $object->getId()) | ||
); | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
src/CoreShop/Bundle/VariantBundle/Resources/config/pimcore/admin.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
core_shop_variant: | ||
pimcore_admin: | ||
js: | ||
resource: '/bundles/coreshopvariant/pimcore/js/resource.js' |
3 changes: 3 additions & 0 deletions
3
src/CoreShop/Bundle/VariantBundle/Resources/config/pimcore/config.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
imports: | ||
- { resource: admin.yml } | ||
- { resource: messenger.yml } |
18 changes: 18 additions & 0 deletions
18
src/CoreShop/Bundle/VariantBundle/Resources/config/pimcore/messenger.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
framework: | ||
messenger: | ||
transports: | ||
coreshop_variant: | ||
dsn: "doctrine://default?queue_name=coreshop_variant" | ||
failure_transport: coreshop_variant_failed | ||
retry_strategy: | ||
max_retries: 3 | ||
delay: 300000 | ||
multiplier: 2 | ||
# we store failed messages here for admins to manually review them later | ||
coreshop_variant_failed: | ||
dsn: "doctrine://default?queue_name=coreshop_variant_failed" | ||
retry_strategy: | ||
max_retries: 0 | ||
|
||
routing: | ||
'CoreShop\Bundle\VariantBundle\Messenger\CreateVariantMessage': coreshop_variant |
13 changes: 10 additions & 3 deletions
13
src/CoreShop/Bundle/VariantBundle/Resources/config/pimcore/routing.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
coreshop_variant_attribute: | ||
path: /product/attribute/{product} | ||
defaults: { _controller: CoreShop\Bundle\VariantBundle\Controller\ProductController::attributeAction } | ||
coreshop_admin_variant_attributes: | ||
path: /admin/variant/attributes | ||
defaults: { _controller: CoreShop\Bundle\VariantBundle\Controller\VariantController::getAttributesAction } | ||
options: | ||
expose: true | ||
|
||
coreshop_admin_variant_generator: | ||
path: /admin/variant/generate | ||
defaults: { _controller: CoreShop\Bundle\VariantBundle\Controller\VariantController::generateVariantsAction } | ||
methods: [POST] | ||
options: | ||
expose: true |
Oops, something went wrong.