Skip to content

Commit c8e0470

Browse files
committed
Introduce Liip imagine filter to apply to product images (#83)
1 parent 4f6f6ba commit c8e0470

18 files changed

+151
-73
lines changed

composer.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@
8888
"cache:clear": "symfony-cmd",
8989
"assets:install %PUBLIC_DIR%": "symfony-cmd",
9090
"security-checker security:check": "script"
91-
}
91+
},
92+
"ecs": "vendor/bin/ecs check",
93+
"phpstan": "vendor/bin/phpstan analyse",
94+
"psalm": "vendor/bin/psalm",
95+
"phpspec": "vendor/bin/phpspec run --ansi -f progress --no-interaction",
96+
"unit": "vendor/bin/phpunit --colors=always",
97+
"behat": "vendor/bin/behat --colors --strict -vvv --no-interaction || vendor/bin/behat --colors --strict -vvv --no-interaction --rerun",
98+
"tests": [
99+
"composer validate --ansi --strict",
100+
"@ecs",
101+
"@phpstan",
102+
"@psalm",
103+
"@phpspec",
104+
"@unit",
105+
"@behat"
106+
]
92107
}
93108
}

config/services/generator.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4-
<parameters>
5-
<parameter key="webgriffe_sylius_active_campaign.scheme">https</parameter>
6-
</parameters>
74
<services>
85
<service id="webgriffe.sylius_active_campaign_plugin.generator.channel_hostname_url"
96
class="Webgriffe\SyliusActiveCampaignPlugin\Generator\ChannelHostnameUrlGenerator">
107
<argument type="service" id="router"/>
8+
<argument type="service" id="liip_imagine.cache.manager"/>
119
</service>
1210
</services>
1311
</container>

config/services/mapper.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
<argument type="service" id="webgriffe.sylius_active_campaign_plugin.factory.active_campaign.ecommerce_order_product"/>
3333
<argument type="service" id="webgriffe.sylius_active_campaign_plugin.generator.channel_hostname_url"/>
3434
<argument type="string">%kernel.default_locale%</argument>
35-
<argument type="string">%webgriffe_sylius_active_campaign.scheme%</argument>
36-
<argument type="string">%webgriffe_sylius_active_campaign.mapper.ecommerce_order_product.image_type%</argument>
3735
</service>
3836

3937
<service id="webgriffe.sylius_active_campaign_plugin.mapper.ecommerce_order_discount"

docs/usage/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,16 @@ But what if you need to export to ActiveCampaign only some Sylius Orders? Simply
191191
The EcommerceOrderProductMapper service set the product image url needed to show it in the ActiveCampaign admin
192192
dashboard but also for the email template. By default, the service will take the first image for the product, but you can
193193
specify a Sylius image type to use for this purpose (for example you could have a `main` type used to specify the first
194-
image of the product). Set this parameter in the `webgriffe_sylius_active_campaign_plugin.yaml` file:
194+
image of the product). You could also set the Liip imagine filter to apply to the image to be loaded more easily. Set
195+
these parameters in the `webgriffe_sylius_active_campaign_plugin.yaml` file:
195196

196197
```yaml
197198
webgriffe_sylius_active_campaign:
198199
...
199200
mapper:
200201
ecommerce_order_product:
201202
image_type: 'main'
203+
image_filter: 'sylius_medium'
202204
```
203205
204206
Before creating the resource on ActiveCampaign, the EcommerceOrderEnqueuer queries for a corresponding ecommerce order

spec/Enqueuer/WebhookEnqueuerSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function let(
2929
$channel->getId()->willReturn(3);
3030
$channel->getActiveCampaignListId()->willReturn(4);
3131

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

3434
$this->beConstructedWith($messageBus, $activeCampaignWebhookClient, $channelHostnameUrlGenerator);
3535
}

spec/Generator/ChannelHostnameUrlGeneratorSpec.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace spec\Webgriffe\SyliusActiveCampaignPlugin\Generator;
66

7+
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
78
use PhpSpec\ObjectBehavior;
89
use Sylius\Component\Core\Model\ChannelInterface;
910
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -15,13 +16,15 @@ class ChannelHostnameUrlGeneratorSpec extends ObjectBehavior
1516
{
1617
public function let(
1718
UrlGeneratorInterface $router,
18-
ChannelInterface $channel
19+
ChannelInterface $channel,
20+
CacheManager $cacheManager,
1921
): void {
2022
$channel->getHostname()->willReturn('domain.com');
2123

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

24-
$this->beConstructedWith($router);
27+
$this->beConstructedWith($router, $cacheManager);
2528
}
2629

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

37-
public function it_should_returns_a_url(
40+
public function it_should_returns_a_url_for_route(
41+
ChannelInterface $channel,
42+
UrlGeneratorInterface $router,
43+
RequestContext $context
44+
): void {
45+
$router->getContext()->willReturn($context);
46+
$context->getHost()->willReturn('otherdomain.com');
47+
48+
$context->setHost('domain.com')->shouldBeCalledOnce()->willReturn($context);
49+
$context->setHost('otherdomain.com')->shouldBeCalledOnce()->willReturn($context);
50+
51+
$this->generateForRoute($channel, 'route', [])->shouldReturn('/route');
52+
}
53+
54+
public function it_should_returns_a_url_for_image(
3855
ChannelInterface $channel,
3956
UrlGeneratorInterface $router,
4057
RequestContext $context
@@ -45,6 +62,6 @@ public function it_should_returns_a_url(
4562
$context->setHost('domain.com')->shouldBeCalledOnce()->willReturn($context);
4663
$context->setHost('otherdomain.com')->shouldBeCalledOnce()->willReturn($context);
4764

48-
$this->generate($channel, 'route', [])->shouldReturn('/route');
65+
$this->generateForImage($channel, 'image.jpg', 'filter')->shouldReturn('/image.jpg?filter=filter');
4966
}
5067
}

spec/Mapper/EcommerceOrderMapperSpec.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function let(
5555
ChannelHostnameUrlGeneratorInterface $channelHostnameUrlGenerator,
5656
LocaleInterface $frenchLocale
5757
): void {
58-
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'it_IT'])->willReturn('https://localhost/order/sD4ew_w4s5T');
58+
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_order_show', ['tokenValue' => 'sD4ew_w4s5T', '_locale' => 'it_IT'])->willReturn('https://localhost/order/sD4ew_w4s5T');
5959

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

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

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

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

282282
$this->mapFromOrder($order, true)->shouldReturn($ecommerceOrder);
283283
}
@@ -289,7 +289,7 @@ public function it_maps_ecommerce_abandoned_cart_from_order(
289289
EcommerceOrderFactoryInterface $ecommerceOrderFactory,
290290
ChannelHostnameUrlGeneratorInterface $channelHostnameUrlGenerator
291291
): void {
292-
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_cart_summary', ['_locale' => 'it_IT'])->shouldBeCalledOnce()->willReturn('https://localhost/cart');
292+
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_cart_summary', ['_locale' => 'it_IT'])->shouldBeCalledOnce()->willReturn('https://localhost/cart');
293293
$ecommerceOrder->setOrderUrl('https://localhost/cart')->shouldBeCalledOnce();
294294
$order->getState()->willReturn(OrderInterface::STATE_CART);
295295
$ecommerceOrderFactory->createNew(

spec/Mapper/EcommerceOrderProductMapperSpec.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public function let(
3636
): void {
3737
$ecommerceOrderProductFactory->createNew('Wine bottle', 1200, 2, '432')->willReturn($ecommerceOrderProduct);
3838

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

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

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

72-
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'https', null);
73+
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', null, null);
7374
}
7475

7576
public function it_is_initializable(): void
@@ -117,13 +118,6 @@ public function it_throws_if_order_item_order_channel_is_null(OrderItemInterface
117118
->during('mapFromOrderItem', [$orderItem]);
118119
}
119120

120-
public function it_throws_if_order_item_order_channel_hostname_is_null(OrderItemInterface $orderItem, ChannelInterface $channel): void
121-
{
122-
$channel->getHostname()->willReturn(null);
123-
$this->shouldThrow(new InvalidArgumentException('The channel\'s hostname should not be null.'))
124-
->during('mapFromOrderItem', [$orderItem]);
125-
}
126-
127121
public function it_maps_ecommerce_order_product_without_category_if_main_taxon_does_not_exist(
128122
OrderItemInterface $orderItem,
129123
ProductInterface $product,
@@ -154,8 +148,9 @@ public function it_maps_ecommerce_order_product_without_image_url_if_products_do
154148
ProductInterface $product,
155149
EcommerceOrderProductInterface $ecommerceOrderProduct
156150
): void {
157-
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'https', 'main');
151+
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'main');
158152
$product->getImagesByType('main')->willReturn(new ArrayCollection());
153+
$product->getImages()->willReturn(new ArrayCollection());
159154
$ecommerceOrderProduct->setImageUrl('media/image/path/wine.png')->shouldNotBeCalled();
160155
$ecommerceOrderProduct->setImageUrl(null)->shouldBeCalledOnce();
161156

@@ -168,11 +163,13 @@ public function it_maps_ecommerce_order_product_with_image_url_from_specified_ty
168163
OrderItemInterface $orderItem,
169164
ProductInterface $product,
170165
EcommerceOrderProductInterface $ecommerceOrderProduct,
171-
ImageInterface $typedImage
166+
ImageInterface $typedImage,
167+
ChannelInterface $channel,
172168
): void {
173-
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'https', 'main');
169+
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'main');
174170
$product->getImagesByType('main')->willReturn(new ArrayCollection([$typedImage->getWrappedObject()]));
175171
$typedImage->getPath()->willReturn('path/main.jpg');
172+
$channelHostnameUrlGenerator->generateForImage($channel, 'path/main.jpg', null)->willReturn('https://domain.org/media/image/path/main.jpg');
176173
$ecommerceOrderProduct->setImageUrl('https://domain.org/media/image/path/wine.png')->shouldNotBeCalled();
177174
$ecommerceOrderProduct->setImageUrl(null)->shouldNotBeCalled();
178175
$ecommerceOrderProduct->setImageUrl('https://domain.org/media/image/path/main.jpg')->shouldBeCalledOnce();
@@ -187,7 +184,7 @@ public function it_maps_ecommerce_order_product_without_image_url_from_specified
187184
ProductInterface $product,
188185
EcommerceOrderProductInterface $ecommerceOrderProduct
189186
): void {
190-
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', 'https', '');
187+
$this->beConstructedWith($ecommerceOrderProductFactory, $channelHostnameUrlGenerator, 'en_US', '');
191188
$product->getImagesByType('')->shouldNotBeCalled();
192189
$ecommerceOrderProduct->setImageUrl('https://domain.org/media/image/path/wine.png')->shouldBeCalledOnce();
193190
$ecommerceOrderProduct->setImageUrl(null)->shouldNotBeCalled();
@@ -203,7 +200,7 @@ public function it_maps_ecommerce_order_product_with_default_channel_locale_if_n
203200
EcommerceOrderProductInterface $ecommerceOrderProduct
204201
): void {
205202
$order->getLocaleCode()->willReturn(null);
206-
$channelHostnameUrlGenerator->generate($channel, 'sylius_shop_product_show', ['_locale' => 'fr_FR', 'slug' => 'wine-bottle'])->shouldBeCalledOnce()->willReturn('https://localhost/products/wine-bottle');
203+
$channelHostnameUrlGenerator->generateForRoute($channel, 'sylius_shop_product_show', ['_locale' => 'fr_FR', 'slug' => 'wine-bottle'])->shouldBeCalledOnce()->willReturn('https://localhost/products/wine-bottle');
207204

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

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

237234
$this->mapFromOrderItem($orderItem)->shouldReturn($ecommerceOrderProduct);
238235
}

spec/MessageHandler/Webhook/WebhookCreateHandlerSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function let(
2929
$channel->getId()->willReturn(1);
3030
$channel->getActiveCampaignListId()->willReturn(4);
3131

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

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

src/DependencyInjection/Configuration.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,14 @@ private function buildMapperNode(ArrayNodeDefinition $rootNode): void
5858
->children()
5959
->arrayNode('ecommerce_order_product')
6060
->children()
61-
->scalarNode('image_type')->defaultNull()->end()
61+
->scalarNode('image_type')
62+
->defaultValue('main')
63+
->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.')
64+
->end()
65+
->scalarNode('image_filter')
66+
->defaultValue('sylius_medium')
67+
->info('Liip filter to apply to the image. If none is specified then the original image will be used.')
68+
->end()
6269
->end()
6370
->end()
6471
->end()

0 commit comments

Comments
 (0)