Skip to content

Commit

Permalink
Merge pull request #16 from Setono/fix-urls
Browse files Browse the repository at this point in the history
Make URL generation independent of request context
  • Loading branch information
loevgaard authored Sep 21, 2022
2 parents d71a315 + 75ce8b5 commit 6ffd668
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 16 deletions.
3 changes: 2 additions & 1 deletion composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"Sylius\\Component\\Mailer\\Sender\\SenderInterface",
"Sylius\\Component\\Order\\Context\\CartContextInterface",
"Sylius\\Component\\Order\\Context\\CartNotFoundException",
"Sylius\\Component\\Order\\Model\\OrderInterface"
"Sylius\\Component\\Order\\Model\\OrderInterface",
"Sylius\\Component\\Channel\\Model\\ChannelInterface"
]
}
3 changes: 2 additions & 1 deletion src/Mailer/EmailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function sendNotification(NotificationInterface $notification): void
Assert::notNull($order);

$channel = $order->getChannel();
Assert::notNull($channel);

$email = $notification->getEmail();
Assert::notNull($email);
Expand All @@ -44,7 +45,7 @@ public function sendNotification(NotificationInterface $notification): void
'localeCode' => $order->getLocaleCode(),
'urls' => [
'cartRecovery' => $this->cartRecoveryUrlGenerator->generate($order),
'unsubscribe' => $this->unsubscribeUrlGenerator->generate($email, (string) $order->getLocaleCode()),
'unsubscribe' => $this->unsubscribeUrlGenerator->generate($channel, $email, (string) $order->getLocaleCode()),
],
]);
}
Expand Down
14 changes: 11 additions & 3 deletions src/UrlGenerator/CartRecoveryUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Webmozart\Assert\Assert;

final class CartRecoveryUrlGenerator implements CartRecoveryUrlGeneratorInterface
{
Expand All @@ -21,9 +22,11 @@ public function __construct(UrlGeneratorInterface $urlGenerator, string $route)

public function generate(
OrderInterface $order,
array $parameters = [],
int $referenceType = UrlGeneratorInterface::ABSOLUTE_URL
array $parameters = []
): string {
$channel = $order->getChannel();
Assert::notNull($channel);

$parameters = array_merge([
'tokenValue' => $order->getTokenValue(),
'utm_source' => 'sylius',
Expand All @@ -32,6 +35,11 @@ public function generate(
'_locale' => $order->getLocaleCode(),
], $parameters);

return $this->urlGenerator->generate($this->route, $parameters, $referenceType);
return sprintf(
'%s://%s%s',
$this->urlGenerator->getContext()->getScheme(),
(string) $channel->getHostname(),
$this->urlGenerator->generate($this->route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH)
);
}
}
4 changes: 1 addition & 3 deletions src/UrlGenerator/CartRecoveryUrlGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Setono\SyliusAbandonedCartPlugin\UrlGenerator;

use Sylius\Component\Core\Model\OrderInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

interface CartRecoveryUrlGeneratorInterface
{
Expand All @@ -14,7 +13,6 @@ interface CartRecoveryUrlGeneratorInterface
*/
public function generate(
OrderInterface $order,
array $parameters = [],
int $referenceType = UrlGeneratorInterface::ABSOLUTE_URL
array $parameters = []
): string;
}
12 changes: 9 additions & 3 deletions src/UrlGenerator/UnsubscribeUrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Setono\SyliusAbandonedCartPlugin\UrlGenerator;

use Setono\SyliusAbandonedCartPlugin\Hasher\EmailHasherInterface;
use Sylius\Component\Channel\Model\ChannelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class UnsubscribeUrlGenerator implements UnsubscribeUrlGeneratorInterface
Expand All @@ -23,10 +24,10 @@ public function __construct(UrlGeneratorInterface $urlGenerator, EmailHasherInte
}

public function generate(
ChannelInterface $channel,
string $email,
string $locale,
array $parameters = [],
int $referenceType = UrlGeneratorInterface::ABSOLUTE_URL
array $parameters = []
): string {
$parameters = array_merge([
'email' => $email,
Expand All @@ -37,6 +38,11 @@ public function generate(
'_locale' => $locale,
], $parameters);

return $this->urlGenerator->generate($this->route, $parameters, $referenceType);
return sprintf(
'%s://%s%s',
$this->urlGenerator->getContext()->getScheme(),
(string) $channel->getHostname(),
$this->urlGenerator->generate($this->route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH)
);
}
}
6 changes: 3 additions & 3 deletions src/UrlGenerator/UnsubscribeUrlGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace Setono\SyliusAbandonedCartPlugin\UrlGenerator;

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Sylius\Component\Channel\Model\ChannelInterface;

interface UnsubscribeUrlGeneratorInterface
{
/**
* Generates an unsubscribe URL for a given email
*/
public function generate(
ChannelInterface $channel,
string $email,
string $locale,
array $parameters = [],
int $referenceType = UrlGeneratorInterface::ABSOLUTE_URL
array $parameters = []
): string;
}
9 changes: 9 additions & 0 deletions tests/UrlGenerator/CartRecoveryUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusAbandonedCartPlugin\UrlGenerator\CartRecoveryUrlGenerator;
use Sylius\Component\Core\Model\Channel;
use Sylius\Component\Core\Model\Order;
use Symfony\Component\Routing\Route;

Expand All @@ -26,9 +27,13 @@ protected function getRoutes(): iterable
*/
public function it_generates_url(): void
{
$channel = new Channel();
$channel->setHostname('example.com');

$order = new Order();
$order->setLocaleCode('en_US');
$order->setTokenValue('token');
$order->setChannel($channel);

$cartRecoveryUrlGenerator = new CartRecoveryUrlGenerator($this->urlGenerator, 'sylius_shop_cart_summary');
self::assertSame('https://example.com/cart?tokenValue=token&utm_source=sylius&utm_medium=email&utm_campaign=Abandoned%20Cart&_locale=en_US', $cartRecoveryUrlGenerator->generate($order));
Expand All @@ -39,9 +44,13 @@ public function it_generates_url(): void
*/
public function it_allows_to_overwrite_parameters(): void
{
$channel = new Channel();
$channel->setHostname('example.com');

$order = new Order();
$order->setLocaleCode('en_US');
$order->setTokenValue('token');
$order->setChannel($channel);

$cartRecoveryUrlGenerator = new CartRecoveryUrlGenerator($this->urlGenerator, 'sylius_shop_cart_summary');
self::assertSame('https://example.com/cart?tokenValue=token&utm_source=sylius&utm_medium=email&utm_campaign=Abandoned%20Cart%20%232&_locale=en_US&utm_content=Number%20two', $cartRecoveryUrlGenerator->generate($order, [
Expand Down
11 changes: 9 additions & 2 deletions tests/UrlGenerator/UnsubscribeUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Setono\SyliusAbandonedCartPlugin\Hasher\EmailHasher;
use Setono\SyliusAbandonedCartPlugin\UrlGenerator\UnsubscribeUrlGenerator;
use Sylius\Component\Core\Model\Channel;
use Symfony\Component\Routing\Route;

/**
Expand All @@ -32,9 +33,12 @@ public function it_generates_url(): void
'setono_sylius_abandoned_cart_shop_unsubscribe_customer'
);

$channel = new Channel();
$channel->setHostname('example.com');

self::assertSame(
'https://example.com/abandoned-cart/[email protected]&hash=2ac21379842b5445001475a596caab5843ecbc6be46c27f882cb6c0bd75fb9f9&utm_source=sylius&utm_medium=email&utm_campaign=Abandoned%20Cart%20Unsubscribe&_locale=en_US',
$urlGenerator->generate('[email protected]', 'en_US')
$urlGenerator->generate($channel, '[email protected]', 'en_US')
);
}

Expand All @@ -49,9 +53,12 @@ public function it_allows_to_overwrite_parameters(): void
'setono_sylius_abandoned_cart_shop_unsubscribe_customer'
);

$channel = new Channel();
$channel->setHostname('example.com');

self::assertSame(
'https://example.com/abandoned-cart/[email protected]&hash=2ac21379842b5445001475a596caab5843ecbc6be46c27f882cb6c0bd75fb9f9&utm_source=sylius&utm_medium=email&utm_campaign=Abandoned%20Cart%20Unsubscribe%20%232&_locale=en_US&utm_content=Number%20two',
$urlGenerator->generate('[email protected]', 'en_US', [
$urlGenerator->generate($channel, '[email protected]', 'en_US', [
'utm_campaign' => 'Abandoned Cart Unsubscribe #2',
'utm_content' => 'Number two',
])
Expand Down

0 comments on commit 6ffd668

Please sign in to comment.