-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure product option values are synchronized from the variant to its…
… sample
- Loading branch information
Showing
11 changed files
with
190 additions
and
15 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
37 changes: 37 additions & 0 deletions
37
spec/Form/EventSubscriber/SynchronizeSampleProductVariantOptionValuesFormSubscriberSpec.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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\BabDev\SyliusProductSamplesPlugin\Form\EventSubscriber; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Model\ProductVariantInterface; | ||
use BabDev\SyliusProductSamplesPlugin\Synchronizer\ProductVariantOptionValuesSynchronizerInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Symfony\Component\Form\FormEvent; | ||
|
||
final class SynchronizeSampleProductVariantOptionValuesFormSubscriberSpec extends ObjectBehavior | ||
{ | ||
public function let(ProductVariantOptionValuesSynchronizerInterface $optionValuesSynchronizer): void | ||
{ | ||
$this->beConstructedWith($optionValuesSynchronizer); | ||
} | ||
|
||
public function it_does_nothing_when_there_is_no_data(FormEvent $event): void | ||
{ | ||
$event->getData()->willReturn(null); | ||
|
||
$this->onSubmit($event); | ||
} | ||
|
||
public function it_synchronizes_option_values_from_the_variant_to_its_sample( | ||
ProductVariantOptionValuesSynchronizerInterface $optionValuesSynchronizer, | ||
FormEvent $event, | ||
ProductVariantInterface $sampleVariant, | ||
): void { | ||
$event->getData()->willReturn($sampleVariant); | ||
|
||
$optionValuesSynchronizer->synchronize($sampleVariant)->shouldBeCalled(); | ||
|
||
$this->onSubmit($event); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
spec/Synchronizer/ProductVariantOptionValuesSynchronizerSpec.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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\BabDev\SyliusProductSamplesPlugin\Synchronizer; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Generator\SampleVariantNameGeneratorInterface; | ||
use BabDev\SyliusProductSamplesPlugin\Model\ProductVariantInterface; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Product\Model\ProductOptionValueInterface; | ||
|
||
final class ProductVariantOptionValuesSynchronizerSpec extends ObjectBehavior | ||
{ | ||
public function it_synchronizes_option_values_from_the_variant_to_its_sample( | ||
SampleVariantNameGeneratorInterface $nameGenerator, | ||
ProductVariantInterface $sampleVariant, | ||
ProductVariantInterface $actualVariant, | ||
ProductOptionValueInterface $smallOptionValue, | ||
ProductOptionValueInterface $mediumOptionValue, | ||
ProductOptionValueInterface $largeOptionValue, | ||
): void { | ||
$sampleVariant->getSampleOf()->willReturn($actualVariant); | ||
$sampleVariant->getOptionValues()->willReturn(new ArrayCollection([$smallOptionValue->getWrappedObject(), $mediumOptionValue->getWrappedObject()])); | ||
|
||
$actualVariant->hasOptionValue($smallOptionValue)->willReturn(true); | ||
$actualVariant->hasOptionValue($mediumOptionValue)->willReturn(false); | ||
$sampleVariant->removeOptionValue($mediumOptionValue)->shouldBeCalled(); | ||
|
||
$actualVariant->getOptionValues()->willReturn(new ArrayCollection([$smallOptionValue->getWrappedObject(), $largeOptionValue->getWrappedObject()])); | ||
$sampleVariant->hasOptionValue($smallOptionValue)->willReturn(true); | ||
$sampleVariant->hasOptionValue($largeOptionValue)->willReturn(false); | ||
$sampleVariant->addOptionValue($largeOptionValue)->shouldBeCalled(); | ||
|
||
$this->synchronize($sampleVariant); | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
src/Form/EventSubscriber/SynchronizeSampleProductVariantOptionValuesFormSubscriber.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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BabDev\SyliusProductSamplesPlugin\Form\EventSubscriber; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Model\ProductVariantInterface; | ||
use BabDev\SyliusProductSamplesPlugin\Synchronizer\ProductVariantOptionValuesSynchronizerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class SynchronizeSampleProductVariantOptionValuesFormSubscriber implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private ProductVariantOptionValuesSynchronizerInterface $optionValuesSynchronizer, | ||
) { | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
FormEvents::SUBMIT => ['onSubmit', -10], | ||
]; | ||
} | ||
|
||
/** | ||
* @note This listener *MUST* run after {@see ManageSampleProductVariantAssignmentsFormSubscriber::onSubmit()} to ensure | ||
* the relationships between the variant and its sample have been set | ||
*/ | ||
public function onSubmit(FormEvent $event): void | ||
{ | ||
/** @var ProductVariantInterface|null $sampleVariant */ | ||
$sampleVariant = $event->getData(); | ||
|
||
if (null === $sampleVariant) { | ||
return; | ||
} | ||
|
||
Assert::isInstanceOf($sampleVariant, ProductVariantInterface::class); | ||
|
||
$this->optionValuesSynchronizer->synchronize($sampleVariant); | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/Synchronizer/ProductVariantOptionValuesSynchronizer.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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BabDev\SyliusProductSamplesPlugin\Synchronizer; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Model\ProductVariantInterface; | ||
use Sylius\Component\Product\Model\ProductOptionValueInterface; | ||
|
||
final class ProductVariantOptionValuesSynchronizer implements ProductVariantOptionValuesSynchronizerInterface | ||
{ | ||
public function synchronize(ProductVariantInterface $sampleVariant): void | ||
{ | ||
$actualVariant = $sampleVariant->getSampleOf(); | ||
|
||
if (null === $actualVariant) { | ||
return; | ||
} | ||
|
||
// First, remove outdated option values from the sample | ||
/** @var ProductOptionValueInterface $optionValue */ | ||
foreach ($sampleVariant->getOptionValues() as $optionValue) { | ||
if (!$actualVariant->hasOptionValue($optionValue)) { | ||
$sampleVariant->removeOptionValue($optionValue); | ||
} | ||
} | ||
|
||
// Next, add missing option values to the sample | ||
/** @var ProductOptionValueInterface $optionValue */ | ||
foreach ($actualVariant->getOptionValues() as $optionValue) { | ||
if (!$sampleVariant->hasOptionValue($optionValue)) { | ||
$sampleVariant->addOptionValue($optionValue); | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Synchronizer/ProductVariantOptionValuesSynchronizerInterface.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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BabDev\SyliusProductSamplesPlugin\Synchronizer; | ||
|
||
use BabDev\SyliusProductSamplesPlugin\Model\ProductVariantInterface; | ||
|
||
interface ProductVariantOptionValuesSynchronizerInterface | ||
{ | ||
public function synchronize(ProductVariantInterface $sampleVariant): void; | ||
} |