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 08c4282 commit 01a9241
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 58 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
2 changes: 1 addition & 1 deletion spec/Generator/ChannelHostnameUrlGeneratorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,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->generateForRoute($channel, 'route', [])->shouldReturn('/route');
}
}
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
8 changes: 4 additions & 4 deletions spec/Mapper/EcommerceOrderProductMapperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ 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');

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

Expand Down Expand Up @@ -203,7 +203,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 +217,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 +232,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']);
}
}
2 changes: 1 addition & 1 deletion src/Enqueuer/WebhookEnqueuer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function enqueue($channel): void
$activeCampaignListId = $channel->getActiveCampaignListId();
Assert::notNull($activeCampaignListId, 'The channel ActiveCampaign list id should not be null.');
$searchWebhooks = $this->activeCampaignWebhookClient->list([
'filters[url]' => $this->channelHostnameUrlGenerator->generate($channel, 'webgriffe_sylius_active_campaign_list_status_webhook'),
'filters[url]' => $this->channelHostnameUrlGenerator->generateForRoute($channel, 'webgriffe_sylius_active_campaign_list_status_webhook'),
'filters[listid]' => (string) $activeCampaignListId,
])->getResourceResponseLists();
if (count($searchWebhooks) > 0) {
Expand Down
37 changes: 36 additions & 1 deletion src/Generator/ChannelHostnameUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@

namespace Webgriffe\SyliusActiveCampaignPlugin\Generator;

use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Sylius\Component\Core\Model\ChannelInterface;
use Symfony\Component\Asset\UrlPackage;
use Symfony\Component\Asset\VersionStrategy\EmptyVersionStrategy;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Webmozart\Assert\Assert;

final class ChannelHostnameUrlGenerator implements ChannelHostnameUrlGeneratorInterface

Check failure on line 14 in src/Generator/ChannelHostnameUrlGenerator.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

Method Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGenerator#generate() was removed

Check failure on line 14 in src/Generator/ChannelHostnameUrlGenerator.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

Method Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGenerator#generate() was removed
{
public function __construct(

Check failure on line 16 in src/Generator/ChannelHostnameUrlGenerator.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

The number of required arguments for Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGenerator#__construct() increased from 1 to 2

Check failure on line 16 in src/Generator/ChannelHostnameUrlGenerator.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

The number of required arguments for Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGenerator#__construct() increased from 1 to 2
private UrlGeneratorInterface $urlGenerator,
private CacheManager $cacheManager,
) {
}

public function generate(ChannelInterface $channel, string $routeName, array $parameters = []): string
public function generateForRoute(ChannelInterface $channel, string $routeName, array $parameters = []): string
{
$channelRequestContext = $this->urlGenerator->getContext();
$previousHost = $channelRequestContext->getHost();
Expand All @@ -29,4 +33,35 @@ public function generate(ChannelInterface $channel, string $routeName, array $pa

return $url;
}

public function generateForImage(ChannelInterface $channel, string $imagePath, ?string $imageFilter = null): string
{
if ($imageFilter !== null) {
$channelRequestContext = $this->urlGenerator->getContext();
$previousHost = $channelRequestContext->getHost();
$channelHost = $channel->getHostname();
Assert::string($channelHost);
$channelRequestContext->setHost($channelHost);

$imageUrl = $this->cacheManager->getBrowserPath(
$imagePath,
$imageFilter,
);

$channelRequestContext->setHost($previousHost);

return $imageUrl;
}
// Fall back to the image path if no liip filter is provided, not recommended!

$hostname = $channel->getHostname();
Assert::notNull($hostname, 'The channel\'s hostname should not be null.');
// TODO: is there any better way to handle this? Especially the media/image directory
$urlPackage = new UrlPackage(
$this->urlGenerator->getContext()->getScheme() . '://' . $hostname . (str_ends_with($hostname, '/') ? '' : '/') . 'media/image',
new EmptyVersionStrategy(),
);

return $urlPackage->getUrl($imagePath);
}
}
4 changes: 3 additions & 1 deletion src/Generator/ChannelHostnameUrlGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

interface ChannelHostnameUrlGeneratorInterface

Check failure on line 9 in src/Generator/ChannelHostnameUrlGeneratorInterface.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

Method generateForRoute() was added to interface Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGeneratorInterface

Check failure on line 9 in src/Generator/ChannelHostnameUrlGeneratorInterface.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

Method generateForImage() was added to interface Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGeneratorInterface

Check failure on line 9 in src/Generator/ChannelHostnameUrlGeneratorInterface.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

Method Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGeneratorInterface#generate() was removed

Check failure on line 9 in src/Generator/ChannelHostnameUrlGeneratorInterface.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

Method generateForRoute() was added to interface Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGeneratorInterface

Check failure on line 9 in src/Generator/ChannelHostnameUrlGeneratorInterface.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

Method generateForImage() was added to interface Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGeneratorInterface

Check failure on line 9 in src/Generator/ChannelHostnameUrlGeneratorInterface.php

View workflow job for this annotation

GitHub Actions / Roave BC Check

Method Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGeneratorInterface#generate() was removed
{
public function generate(ChannelInterface $channel, string $routeName, array $parameters = []): string;
public function generateForRoute(ChannelInterface $channel, string $routeName, array $parameters = []): string;

public function generateForImage(ChannelInterface $channel, string $imagePath, ?string $imageFilter = null): string;
}
4 changes: 2 additions & 2 deletions src/Mapper/EcommerceOrderMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private function getLocaleCodeFromOrder(OrderInterface $order): string
private function getOrderUrl(BaseOrderInterface $order, ChannelInterface $channel, string $localeCode, bool $isCart): string
{
if ($isCart) {
return $this->channelHostnameUrlGenerator->generate(
return $this->channelHostnameUrlGenerator->generateForRoute(
$channel,
'sylius_shop_cart_summary',
[
Expand All @@ -133,7 +133,7 @@ private function getOrderUrl(BaseOrderInterface $order, ChannelInterface $channe
);
}

return $this->channelHostnameUrlGenerator->generate(
return $this->channelHostnameUrlGenerator->generateForRoute(
$channel,
'sylius_shop_order_show',
[
Expand Down
Loading

0 comments on commit 01a9241

Please sign in to comment.