Skip to content

Commit

Permalink
Introduce Liip imagine filter to apply to product images (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
lruozzi9 committed Aug 26, 2024
1 parent 4f6f6ba commit c8e0470
Show file tree
Hide file tree
Showing 18 changed files with 151 additions and 73 deletions.
17 changes: 16 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd",
"security-checker security:check": "script"
}
},
"ecs": "vendor/bin/ecs check",
"phpstan": "vendor/bin/phpstan analyse",
"psalm": "vendor/bin/psalm",
"phpspec": "vendor/bin/phpspec run --ansi -f progress --no-interaction",
"unit": "vendor/bin/phpunit --colors=always",
"behat": "vendor/bin/behat --colors --strict -vvv --no-interaction || vendor/bin/behat --colors --strict -vvv --no-interaction --rerun",
"tests": [
"composer validate --ansi --strict",
"@ecs",
"@phpstan",
"@psalm",
"@phpspec",
"@unit",
"@behat"
]
}
}
4 changes: 1 addition & 3 deletions config/services/generator.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?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">
<parameters>
<parameter key="webgriffe_sylius_active_campaign.scheme">https</parameter>
</parameters>
<services>
<service id="webgriffe.sylius_active_campaign_plugin.generator.channel_hostname_url"
class="Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGenerator">
<argument type="service" id="router"/>
<argument type="service" id="liip_imagine.cache.manager"/>
</service>
</services>
</container>
2 changes: 0 additions & 2 deletions config/services/mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
<argument type="service" id="webgriffe.sylius_active_campaign_plugin.factory.active_campaign.ecommerce_order_product"/>
<argument type="service" id="webgriffe.sylius_active_campaign_plugin.generator.channel_hostname_url"/>
<argument type="string">%kernel.default_locale%</argument>
<argument type="string">%webgriffe_sylius_active_campaign.scheme%</argument>
<argument type="string">%webgriffe_sylius_active_campaign.mapper.ecommerce_order_product.image_type%</argument>
</service>

<service id="webgriffe.sylius_active_campaign_plugin.mapper.ecommerce_order_discount"
Expand Down
4 changes: 3 additions & 1 deletion docs/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,16 @@ But what if you need to export to ActiveCampaign only some Sylius Orders? Simply
The EcommerceOrderProductMapper service set the product image url needed to show it in the ActiveCampaign admin
dashboard but also for the email template. By default, the service will take the first image for the product, but you can
specify a Sylius image type to use for this purpose (for example you could have a `main` type used to specify the first
image of the product). Set this parameter in the `webgriffe_sylius_active_campaign_plugin.yaml` file:
image of the product). You could also set the Liip imagine filter to apply to the image to be loaded more easily. Set
these parameters in the `webgriffe_sylius_active_campaign_plugin.yaml` file:

```yaml
webgriffe_sylius_active_campaign:
...
mapper:
ecommerce_order_product:
image_type: 'main'
image_filter: 'sylius_medium'
```
Before creating the resource on ActiveCampaign, the EcommerceOrderEnqueuer queries for a corresponding ecommerce order
Expand Down
2 changes: 1 addition & 1 deletion spec/Enqueuer/WebhookEnqueuerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function let(
$channel->getId()->willReturn(3);
$channel->getActiveCampaignListId()->willReturn(4);

$channelHostnameUrlGenerator->generate($channel, 'webgriffe_sylius_active_campaign_list_status_webhook')->willReturn('https://localhost/webhook');
$channelHostnameUrlGenerator->generateForRoute($channel, 'webgriffe_sylius_active_campaign_list_status_webhook')->willReturn('https://localhost/webhook');

$this->beConstructedWith($messageBus, $activeCampaignWebhookClient, $channelHostnameUrlGenerator);
}
Expand Down
25 changes: 21 additions & 4 deletions spec/Generator/ChannelHostnameUrlGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace spec\Webgriffe\SyliusActiveCampaignPlugin\Generator;

use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use PhpSpec\ObjectBehavior;
use Sylius\Component\Core\Model\ChannelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -15,13 +16,15 @@ class ChannelHostnameUrlGeneratorSpec extends ObjectBehavior
{
public function let(
UrlGeneratorInterface $router,
ChannelInterface $channel
ChannelInterface $channel,
CacheManager $cacheManager,
): void {
$channel->getHostname()->willReturn('domain.com');

$router->generate('route', [], UrlGeneratorInterface::ABSOLUTE_URL)->willReturn('/route');
$cacheManager->getBrowserPath('image.jpg', 'filter')->willReturn('/image.jpg?filter=filter');

$this->beConstructedWith($router);
$this->beConstructedWith($router, $cacheManager);
}

public function it_is_initializable(): void
Expand All @@ -34,7 +37,21 @@ public function it_implements_channel_hostname_url_generator_interface(): void
$this->shouldImplement(ChannelHostnameUrlGeneratorInterface::class);
}

public function it_should_returns_a_url(
public function it_should_returns_a_url_for_route(
ChannelInterface $channel,
UrlGeneratorInterface $router,
RequestContext $context
): void {
$router->getContext()->willReturn($context);
$context->getHost()->willReturn('otherdomain.com');

$context->setHost('domain.com')->shouldBeCalledOnce()->willReturn($context);
$context->setHost('otherdomain.com')->shouldBeCalledOnce()->willReturn($context);

$this->generateForRoute($channel, 'route', [])->shouldReturn('/route');
}

public function it_should_returns_a_url_for_image(
ChannelInterface $channel,
UrlGeneratorInterface $router,
RequestContext $context
Expand All @@ -45,6 +62,6 @@ public function it_should_returns_a_url(
$context->setHost('domain.com')->shouldBeCalledOnce()->willReturn($context);
$context->setHost('otherdomain.com')->shouldBeCalledOnce()->willReturn($context);

$this->generate($channel, 'route', [])->shouldReturn('/route');
$this->generateForImage($channel, 'image.jpg', 'filter')->shouldReturn('/image.jpg?filter=filter');
}
}
10 changes: 5 additions & 5 deletions spec/Mapper/EcommerceOrderMapperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function let(
ChannelHostnameUrlGeneratorInterface $channelHostnameUrlGenerator,
LocaleInterface $frenchLocale
): void {
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'it_IT'])->willReturn('https://localhost/order/sD4ew_w4s5T');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'it_IT'])->willReturn('https://localhost/order/sD4ew_w4s5T');

$ecommerceOrderProductMapper->mapFromOrderItem($firstOrderItem)->willReturn($firstOrderProduct);

Expand Down Expand Up @@ -250,7 +250,7 @@ public function it_maps_ecommerce_order_product_with_default_channel_locale_if_n
EcommerceOrderInterface $ecommerceOrder
): void {
$order->getLocaleCode()->willReturn(null);
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'fr_FR'])->shouldBeCalledOnce()->willReturn('https://localhost/order/sD4ew_w4s5T');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'fr_FR'])->shouldBeCalledOnce()->willReturn('https://localhost/order/sD4ew_w4s5T');

$this->mapFromOrder($order, true)->shouldReturn($ecommerceOrder);
}
Expand All @@ -263,7 +263,7 @@ public function it_maps_ecommerce_order_with_default_app_locale_if_not_existing_
): void {
$order->getLocaleCode()->willReturn(null);
$channel->getDefaultLocale()->willReturn(null);
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'en_US'])->shouldBeCalledOnce()->willReturn('https://localhost/order/sD4ew_w4s5T');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'en_US'])->shouldBeCalledOnce()->willReturn('https://localhost/order/sD4ew_w4s5T');

$this->mapFromOrder($order, true)->shouldReturn($ecommerceOrder);
}
Expand All @@ -277,7 +277,7 @@ public function it_maps_ecommerce_order_product_with_default_app_locale_if_not_e
): void {
$order->getLocaleCode()->willReturn(null);
$frenchLocale->getCode()->willReturn(null);
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'en_US'])->shouldBeCalledOnce()->willReturn('https://localhost/order/sD4ew_w4s5T');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'en_US'])->shouldBeCalledOnce()->willReturn('https://localhost/order/sD4ew_w4s5T');

$this->mapFromOrder($order, true)->shouldReturn($ecommerceOrder);
}
Expand All @@ -289,7 +289,7 @@ public function it_maps_ecommerce_abandoned_cart_from_order(
EcommerceOrderFactoryInterface $ecommerceOrderFactory,
ChannelHostnameUrlGeneratorInterface $channelHostnameUrlGenerator
): void {
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_cart_summary', ['_locale' => 'it_IT'])->shouldBeCalledOnce()->willReturn('https://localhost/cart');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_cart_summary', ['_locale' => 'it_IT'])->shouldBeCalledOnce()->willReturn('https://localhost/cart');
$ecommerceOrder->setOrderUrl('https://localhost/cart')->shouldBeCalledOnce();
$order->getState()->willReturn(OrderInterface::STATE_CART);
$ecommerceOrderFactory->createNew(
Expand Down
29 changes: 13 additions & 16 deletions spec/Mapper/EcommerceOrderProductMapperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function let(
): void {
$ecommerceOrderProductFactory->createNew('Wine bottle', 1200, 2, '432')->willReturn($ecommerceOrderProduct);

$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_product_show', ['_locale' => 'it_IT', 'slug' => 'wine-bottle'])->willReturn('https://localhost/products/wine-bottle');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_product_show', ['_locale' => 'it_IT', 'slug' => 'wine-bottle'])->willReturn('https://localhost/products/wine-bottle');
$channelHostnameUrlGenerator->generateForImage($channel, 'path/wine.png', null)->willReturn('https://domain.org/media/image/path/wine.png');

$frenchLocale->getCode()->willReturn('fr_FR');

Expand Down Expand Up @@ -69,7 +70,7 @@ public function let(
$ecommerceOrderProduct->setImageUrl('https://domain.org/media/image/path/wine.png');
$ecommerceOrderProduct->setProductUrl('https://localhost/products/wine-bottle');

$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'https', null);
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', null, null);
}

public function it_is_initializable(): void
Expand Down Expand Up @@ -117,13 +118,6 @@ public function it_throws_if_order_item_order_channel_is_null(OrderItemInterface
->during('mapFromOrderItem', [$orderItem]);
}

public function it_throws_if_order_item_order_channel_hostname_is_null(OrderItemInterface $orderItem, ChannelInterface $channel): void
{
$channel->getHostname()->willReturn(null);
$this->shouldThrow(new InvalidArgumentException('The channel\'s hostname should not be null.'))
->during('mapFromOrderItem', [$orderItem]);
}

public function it_maps_ecommerce_order_product_without_category_if_main_taxon_does_not_exist(
OrderItemInterface $orderItem,
ProductInterface $product,
Expand Down Expand Up @@ -154,8 +148,9 @@ public function it_maps_ecommerce_order_product_without_image_url_if_products_do
ProductInterface $product,
EcommerceOrderProductInterface $ecommerceOrderProduct
): void {
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'https', 'main');
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'main');
$product->getImagesByType('main')->willReturn(new ArrayCollection());
$product->getImages()->willReturn(new ArrayCollection());
$ecommerceOrderProduct->setImageUrl('media/image/path/wine.png')->shouldNotBeCalled();
$ecommerceOrderProduct->setImageUrl(null)->shouldBeCalledOnce();

Expand All @@ -168,11 +163,13 @@ public function it_maps_ecommerce_order_product_with_image_url_from_specified_ty
OrderItemInterface $orderItem,
ProductInterface $product,
EcommerceOrderProductInterface $ecommerceOrderProduct,
ImageInterface $typedImage
ImageInterface $typedImage,
ChannelInterface $channel,
): void {
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'https', 'main');
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'main');
$product->getImagesByType('main')->willReturn(new ArrayCollection([$typedImage->getWrappedObject()]));
$typedImage->getPath()->willReturn('path/main.jpg');
$channelHostnameUrlGenerator->generateForImage($channel, 'path/main.jpg', null)->willReturn('https://domain.org/media/image/path/main.jpg');
$ecommerceOrderProduct->setImageUrl('https://domain.org/media/image/path/wine.png')->shouldNotBeCalled();
$ecommerceOrderProduct->setImageUrl(null)->shouldNotBeCalled();
$ecommerceOrderProduct->setImageUrl('https://domain.org/media/image/path/main.jpg')->shouldBeCalledOnce();
Expand All @@ -187,7 +184,7 @@ public function it_maps_ecommerce_order_product_without_image_url_from_specified
ProductInterface $product,
EcommerceOrderProductInterface $ecommerceOrderProduct
): void {
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'https', '');
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', '');
$product->getImagesByType('')->shouldNotBeCalled();
$ecommerceOrderProduct->setImageUrl('https://domain.org/media/image/path/wine.png')->shouldBeCalledOnce();
$ecommerceOrderProduct->setImageUrl(null)->shouldNotBeCalled();
Expand All @@ -203,7 +200,7 @@ public function it_maps_ecommerce_order_product_with_default_channel_locale_if_n
EcommerceOrderProductInterface $ecommerceOrderProduct
): void {
$order->getLocaleCode()->willReturn(null);
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_product_show', ['_locale' => 'fr_FR', 'slug' => 'wine-bottle'])->shouldBeCalledOnce()->willReturn('https://localhost/products/wine-bottle');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_product_show', ['_locale' => 'fr_FR', 'slug' => 'wine-bottle'])->shouldBeCalledOnce()->willReturn('https://localhost/products/wine-bottle');

$this->mapFromOrderItem($orderItem)->shouldReturn($ecommerceOrderProduct);
}
Expand All @@ -217,7 +214,7 @@ public function it_maps_ecommerce_order_product_with_default_app_locale_if_not_e
): void {
$order->getLocaleCode()->willReturn(null);
$channel->getDefaultLocale()->willReturn(null);
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_product_show', ['_locale' => 'en_US', 'slug' => 'wine-bottle'])->shouldBeCalledOnce()->willReturn('https://localhost/products/wine-bottle');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_product_show', ['_locale' => 'en_US', 'slug' => 'wine-bottle'])->shouldBeCalledOnce()->willReturn('https://localhost/products/wine-bottle');

$this->mapFromOrderItem($orderItem)->shouldReturn($ecommerceOrderProduct);
}
Expand All @@ -232,7 +229,7 @@ public function it_maps_ecommerce_order_product_with_default_app_locale_if_not_e
): void {
$order->getLocaleCode()->willReturn(null);
$frenchLocale->getCode()->willReturn(null);
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_product_show', ['_locale' => 'en_US', 'slug' => 'wine-bottle'])->shouldBeCalledOnce()->willReturn('https://localhost/products/wine-bottle');
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_product_show', ['_locale' => 'en_US', 'slug' => 'wine-bottle'])->shouldBeCalledOnce()->willReturn('https://localhost/products/wine-bottle');

$this->mapFromOrderItem($orderItem)->shouldReturn($ecommerceOrderProduct);
}
Expand Down
2 changes: 1 addition & 1 deletion spec/MessageHandler/Webhook/WebhookCreateHandlerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function let(
$channel->getId()->willReturn(1);
$channel->getActiveCampaignListId()->willReturn(4);

$channelHostnameUrlGenerator->generate($channel, 'webgriffe_sylius_active_campaign_list_status_webhook')->willReturn('https://localhost/webhook');
$channelHostnameUrlGenerator->generateForRoute($channel, 'webgriffe_sylius_active_campaign_list_status_webhook')->willReturn('https://localhost/webhook');

$channelRepository->find(1)->willReturn($channel);

Expand Down
9 changes: 8 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ private function buildMapperNode(ArrayNodeDefinition $rootNode): void
->children()
->arrayNode('ecommerce_order_product')
->children()
->scalarNode('image_type')->defaultNull()->end()
->scalarNode('image_type')
->defaultValue('main')
->info('Type of the product image to send to ActiveCampaign. If none is specified or the type does not exists on current product then the first image will be used.')
->end()
->scalarNode('image_filter')
->defaultValue('sylius_medium')
->info('Liip filter to apply to the image. If none is specified then the original image will be used.')
->end()
->end()
->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('webgriffe_sylius_active_campaign.api_client.base_url', (string) $config['api_client']['base_url']);
$container->setParameter('webgriffe_sylius_active_campaign.api_client.key', (string) $config['api_client']['key']);

$container->setParameter('webgriffe_sylius_active_campaign.mapper.ecommerce_order_product.image_type', $config['mapper']['ecommerce_order_product']['image_type']);

$loader->load('services.xml');

$this->addMapperOptionsOnMappers($container, $config);
}

public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface
{
return new Configuration();
}

private function addMapperOptionsOnMappers(ContainerBuilder $container, array $config): void
{
$definition = $container->getDefinition('webgriffe.sylius_active_campaign_plugin.mapper.ecommerce_order_product');
$definition->setArgument('$imageType', $config['mapper']['ecommerce_order_product']['image_type']);
$definition->setArgument('$imageFilter', $config['mapper']['ecommerce_order_product']['image_filter']);
}
}
Loading

0 comments on commit c8e0470

Please sign in to comment.