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

Configure number of synchronised products in Channel configuration #32

Merged
merged 7 commits into from
Jul 29, 2024
Merged
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
8 changes: 0 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ jobs:
name: Run PHPStan
run: vendor/bin/phpstan analyse -c phpstan.neon -l max src/

-
name: Run Psalm
run: vendor/bin/psalm

-
name: Run PHPSpec
run: vendor/bin/phpspec run --ansi -f progress --no-interaction
Expand Down Expand Up @@ -151,10 +147,6 @@ jobs:
name: Run PHPStan
run: vendor/bin/phpstan analyse -c phpstan.neon -l max src/

-
name: Run Psalm
run: vendor/bin/psalm

-
name: Run PHPSpec
run: vendor/bin/phpspec run --ansi -f progress --no-interaction
Expand Down
14 changes: 11 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
"symfony/dotenv": "^5.4 || ^6.0",
"symfony/flex": "^2.2.2",
"symfony/intl": "^5.4 || ^6.0",
"symfony/web-profiler-bundle": "^5.4 || ^6.0",
"vimeo/psalm": "5.25.0"
"symfony/web-profiler-bundle": "^5.4 || ^6.0"
},
"conflict": {
"behat/mink-selenium2-driver": ">=1.7.0"
Expand Down Expand Up @@ -94,6 +93,15 @@
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd",
"security-checker security:check": "script"
}
},
"analyse": [
"composer validate --ansi --strict",
"vendor/bin/phpstan analyse -c phpstan.neon -l max src/"
],
"test": [
"vendor/bin/phpspec run --ansi -f progress --no-interaction",
"vendor/bin/phpunit --colors=always",
"vendor/bin/behat --colors --strict -vvv --no-interaction || vendor/bin/behat --colors --strict -vvv --no-interaction --rerun"
]
}
}
5 changes: 5 additions & 0 deletions config/app/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ sylius_ui:
commerce_weavers_sylius_also_bought_missing_configuration:
template: '@CommerceWeaversSyliusAlsoBoughtPlugin/_missing_configuration.html.twig'
priority: 40
sylius.admin.channel.form.second_column_content:
blocks:
commerce_weavers_sylius_also_bought_number_of_synchronised_products:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
commerce_weavers_sylius_also_bought_number_of_synchronised_products:
commerce_weavers_sylius_also_bought_number_of_synchronized_products:

just because in different places, we already use the US version with z

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for twig tempalte file name etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me change that in a separate PR, as it would require also changing the property name and a bunch of other stuff :D

template: '@CommerceWeaversSyliusAlsoBoughtPlugin/Admin/Channel/Form/numberOfSynchronisedProducts.html.twig'
priority: 0
13 changes: 13 additions & 0 deletions config/services/form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container
xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<services>
<service id="CommerceWeavers\SyliusAlsoBoughtPlugin\Form\Extension\ChannelTypeExtension" public="true">
<tag name="form.type_extension" />
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion config/services/processor.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="sylius.repository.product" />
<argument type="service" id="CommerceWeavers\SyliusAlsoBoughtPlugin\Provider\BoughtTogetherProductsAssociationProviderInterface" />
<argument>%commerce_weavers_sylius_also_bought.number_of_products_to_associate%</argument>
<argument type="service" id="CommerceWeavers\SyliusAlsoBoughtPlugin\Provider\SynchronizableProductsNumberProviderInterface" />
<tag name="messenger.message_handler" bus="sylius.event_bus" priority="15" />
</service>
</services>
Expand Down
7 changes: 7 additions & 0 deletions config/services/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@
>
<argument type="service" id="sylius.repository.order" />
</service>

<service
id="CommerceWeavers\SyliusAlsoBoughtPlugin\Provider\SynchronizableProductsNumberProviderInterface"
class="CommerceWeavers\SyliusAlsoBoughtPlugin\Provider\SynchronizableProductsNumberProvider"
>
<argument>%commerce_weavers_sylius_also_bought.number_of_products_to_associate%</argument>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@managing_channels
Feature: Managing number of items to synchronize on channel
In order to change the number of items that are synchronised as bought together products
As an Administrator
I want to be able to edit their number on channel configuration

Background:
Given the store operates on a channel named "Web Channel"
And I am logged in as an administrator

@ui
Scenario: Seeing the default number of synchronised products
When I want to modify a channel "Web Channel"
Then the number of synchronised products should be 10

@ui
Scenario: Modifying the number of synchronised products
When I want to modify a channel "Web Channel"
And I change the number of synchronised products to 5
And I save my changes
Then I should be notified that it has been successfully edited
And the number of synchronised products should be 5
33 changes: 33 additions & 0 deletions features/synchronizing_bought_together_products.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,39 @@ Feature: Synchronizing bought together products
And the "Grains" product should have usually been bought with "Twigs", "Bird bath", "Bird netting", "Weaver" and "Swan"
And the "Bird feeder" product should have usually been bought with "Grains", "Twigs", "Bird bath", "Bird netting" and "Swift"

@cli
Scenario: Synchronizing maximum number of bought together products
Given the channel "United States" has 2 configured as number of synchronizable bought together products
And the store has a product association type "Bought together" with a code "bought_together"
And the store has products "Weaver", "Swan", "Swift", "Grains", "Twigs", "Bird feeder", "Bird bath" and "Bird netting"
And there is a customer "john.doe@example.com" that placed an order
And the customer bought a single "Weaver", "Bird bath" and "Bird netting"
And the customer bought 100 "Grains" products
And the customer bought 500 "Twigs" products
And the customer "John Doe" addressed it to "Elm Street", "90802" "Anytown" in the "United States" with identical billing address
And the customer chose "Free" shipping method with "Cash on delivery" payment
And this order is already paid
And there is another customer "john.galt@example.com" that placed an order
And the customer bought a single "Swan"
And the customer bought 200 "Grains" products
And the customer bought 20 "Twigs" products
And the customer "John Galt" addressed it to "Atlas Way", "385" "Libertyville" in the "United States" with identical billing address
And the customer chose "Free" shipping method with "Cash on delivery" payment
And this order is already paid
And there is another customer "jane.doe@example.com" that placed an order
And the customer bought a single "Swift", "Bird feeder", "Bird bath" and "Bird netting"
And the customer bought 100 "Grains" products
And the customer bought 100 "Twigs" products
And the customer "Jane Doe" addressed it to "Elm Street", "90802" "Anytown" in the "United States" with identical billing address
And the customer chose "Free" shipping method with "Cash on delivery" payment
And this order is already paid
When I synchronize bought together products by running command
Then I should be informed that the bought together products are synchronized
And the "Weaver" product should have usually been bought with "Grains" and "Twigs"
And the "Swan" product should have usually been bought with "Grains" and "Twigs"
And the "Grains" product should have usually been bought with "Twigs" and "Bird bath"
And the "Bird feeder" product should have usually been bought with "Grains" and "Twigs"

@cli
Scenario: Being notified about specific association type requirement
Given the store has a product "Weaver"
Expand Down
26 changes: 26 additions & 0 deletions migrations/Version20240408131819.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace CommerceWeaversSyliusAlsoBoughtMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240408131819 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add synchronised products number for channel';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_channel ADD number_of_synchronised_products INT DEFAULT 10 NOT NULL');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE sylius_channel DROP number_of_synchronised_products');
}
}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
- src
- tests/Behat

excludes_analyse:
excludePaths:
# Makes PHPStan crash
- 'src/DependencyInjection/Configuration.php'

Expand Down
27 changes: 0 additions & 27 deletions psalm.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ final class CommerceWeaversSyliusAlsoBoughtExtension extends AbstractResourceExt

private const ALIAS = 'commerce_weavers_sylius_also_bought';

/** @psalm-suppress UnusedVariable */
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
Expand Down
1 change: 0 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

final class Configuration implements ConfigurationInterface
{
/** @psalm-suppress UnusedVariable */
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('commerce_weavers_sylius_also_bought');
Expand Down
12 changes: 12 additions & 0 deletions src/Entity/BoughtTogetherConfigurableChannelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Entity;

interface BoughtTogetherConfigurableChannelInterface
{
public function setNumberOfSynchronisedProducts(int $number): void;

public function getNumberOfSynchronisedProducts(): int;
}
24 changes: 24 additions & 0 deletions src/Entity/BoughtTogetherConfigurableChannelTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Entity;

use Doctrine\ORM\Mapping as ORM;

trait BoughtTogetherConfigurableChannelTrait
{
/** @ORM\Column(name="number_of_synchronised_products", type="integer", nullable=false, options={"default": 10}) */
#[ORM\Column(name: 'number_of_synchronised_products', type: 'integer', nullable: false, options: ['default' => 10])]
private int $numberOfSynchronisedProducts = 10;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private int $numberOfSynchronisedProducts = 10;
private int $numberOfSynchronizedProducts = 10;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #32 (comment) 😅


public function setNumberOfSynchronisedProducts(int $number): void
{
$this->numberOfSynchronisedProducts = $number;
}

public function getNumberOfSynchronisedProducts(): int
{
return $this->numberOfSynchronisedProducts;
}
}
28 changes: 28 additions & 0 deletions src/Form/Extension/ChannelTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Form\Extension;

use Sylius\Bundle\ChannelBundle\Form\Type\ChannelType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;

final class ChannelTypeExtension extends AbstractTypeExtension
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('numberOfSynchronisedProducts', NumberType::class, [
'label' => 'commerce_weavers_sylius_also_bought.form.channel.number_of_synchronised_products',
'required' => true,
])
;
}

public static function getExtendedTypes(): iterable
{
yield ChannelType::class;
}
}
21 changes: 16 additions & 5 deletions src/Processor/BoughtTogetherProductsAssociationProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use CommerceWeavers\SyliusAlsoBoughtPlugin\Entity\BoughtTogetherProductsAwareInterface;
use CommerceWeavers\SyliusAlsoBoughtPlugin\Event\SynchronizationEnded;
use CommerceWeavers\SyliusAlsoBoughtPlugin\Provider\BoughtTogetherProductsAssociationProviderInterface;
use CommerceWeavers\SyliusAlsoBoughtPlugin\Provider\SynchronizableProductsNumberProviderInterface;
use Doctrine\ORM\EntityManagerInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
Expand All @@ -20,7 +21,8 @@ public function __construct(
private EntityManagerInterface $entityManager,
private ProductRepositoryInterface $productRepository,
private BoughtTogetherProductsAssociationProviderInterface $boughtTogetherProductsAssociationProvider,
private int $numberOfProductsToAssociate = 10,
private SynchronizableProductsNumberProviderInterface $synchronizableProductsNumberProvider,
private int $batchSize = 100,
) {
}

Expand All @@ -31,8 +33,14 @@ public function __invoke(SynchronizationEnded $event): void

Assert::allIsInstanceOf($products, BoughtTogetherProductsAwareInterface::class);

$batch = 0;
foreach ($products as $product) {
Zales0123 marked this conversation as resolved.
Show resolved Hide resolved
$boughtTogetherProducts = array_slice($product->getBoughtTogetherProducts(), 0, $this->numberOfProductsToAssociate - 1, true);
$boughtTogetherProducts = array_slice(
$product->getBoughtTogetherProducts(),
0,
$this->synchronizableProductsNumberProvider->getNumberOfProductsToSynchronise($product) - 1,
true,
);
$boughtTogetherAssociation = $this->boughtTogetherProductsAssociationProvider->getForProduct($product);
$this->entityManager->persist($boughtTogetherAssociation);

Expand All @@ -42,14 +50,17 @@ public function __invoke(SynchronizationEnded $event): void
foreach ($frequentlyBoughtTogetherProducts as $frequentlyBoughtTogetherProduct) {
$boughtTogetherAssociation->addAssociatedProduct($frequentlyBoughtTogetherProduct);
}

if (++$batch >= $this->batchSize) {
$this->entityManager->flush();
$batch = 0;
}
}

$this->entityManager->flush();
}

/**
* @return ProductInterface[]
*/
/** @return ProductInterface[] */
private function findProductsByCodes(array $codes): array
{
/** @var ProductInterface[] $products */
Expand Down
32 changes: 32 additions & 0 deletions src/Provider/SynchronizableProductsNumberProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusAlsoBoughtPlugin\Provider;

use CommerceWeavers\SyliusAlsoBoughtPlugin\Entity\BoughtTogetherConfigurableChannelInterface;
use Sylius\Component\Core\Model\ProductInterface;

final class SynchronizableProductsNumberProvider implements SynchronizableProductsNumberProviderInterface
{
public function __construct(private int $defaultNumberOfProducts = 10)
{
}

public function getNumberOfProductsToSynchronise(ProductInterface $product): int
{
$channels = $product->getChannels();

$numberOfProducts = 0;
/** @var BoughtTogetherConfigurableChannelInterface $channel */
foreach ($channels as $channel) {
$numberOfProducts = max($numberOfProducts, $channel->getNumberOfSynchronisedProducts());
}

if ($numberOfProducts <= 0) {
return $this->defaultNumberOfProducts;
}

return $numberOfProducts;
}
}
Loading
Loading