Skip to content

Commit

Permalink
feat: add cross-engage requirements to glue (#4)
Browse files Browse the repository at this point in the history
* feat: add cross-engage requirements to glue

* improve code
  • Loading branch information
sourcecube authored Aug 7, 2023
1 parent d052267 commit e947509
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace FondOfSpryker\Zed\SalesNewsletterSignup\Communication\Plugin\CheckoutRestApi;

use Generated\Shared\Transfer\QuoteTransfer;
use Generated\Shared\Transfer\RestCheckoutRequestAttributesTransfer;
use Spryker\Zed\CheckoutRestApiExtension\Dependency\Plugin\QuoteMapperPluginInterface;
use Spryker\Zed\Kernel\Communication\AbstractPlugin;

/**
* @method \FondOfSpryker\Zed\SalesNewsletterSignup\Communication\SalesNewsletterSignupCommunicationFactory getFactory()
*/
class CustomerHashCheckoutQuoteMapPlugin extends AbstractPlugin implements QuoteMapperPluginInterface
{
/**
* @param \Generated\Shared\Transfer\RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
*
* @return \Generated\Shared\Transfer\QuoteTransfer
*/
public function map(RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer, QuoteTransfer $quoteTransfer): QuoteTransfer
{
$customer = $restCheckoutRequestAttributesTransfer->getCustomer();
if ($customer && $customer->getEmail()) {
$quoteTransfer->setUserHash($this->getFactory()->getNewsletterService()->getHash($customer->getEmail()));
}

return $quoteTransfer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace FondOfSpryker\Zed\SalesNewsletterSignup\Communication\Plugin\CheckoutRestApi;

use Generated\Shared\Transfer\QuoteTransfer;
use Generated\Shared\Transfer\RestCheckoutRequestAttributesTransfer;
use Spryker\Zed\CheckoutRestApiExtension\Dependency\Plugin\QuoteMapperPluginInterface;
use Spryker\Zed\Kernel\Communication\AbstractPlugin;

/**
* @method \FondOfSpryker\Zed\SalesNewsletterSignup\Communication\SalesNewsletterSignupCommunicationFactory getFactory()
*/
class CustomerOptInOptOutUrlQuoteMapPlugin extends AbstractPlugin implements QuoteMapperPluginInterface
{
/**
* @param \Generated\Shared\Transfer\RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
*
* @return \Generated\Shared\Transfer\QuoteTransfer
*/
public function map(RestCheckoutRequestAttributesTransfer $restCheckoutRequestAttributesTransfer, QuoteTransfer $quoteTransfer): QuoteTransfer
{
$customer = $restCheckoutRequestAttributesTransfer->getCustomer();
if ($restCheckoutRequestAttributesTransfer->getSignupNewsletter() === true && $customer && $customer->getEmail()) {
$newsletterService = $this->getFactory()->getNewsletterService();
$params = [
'language' => $newsletterService->getLanguagePrefix(),
$newsletterService->getNewsletterParamName() => $newsletterService->getNewsletterParamName(),
$newsletterService->getNewsletterTokenParamName() => $newsletterService->getHash($customer->getEmail()),
];

$quoteTransfer->setOptInUrl($newsletterService->getOptInUrl($params));
$quoteTransfer->setOptOutUrl($newsletterService->getOptOutUrl($params));
}

return $quoteTransfer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace FondOfSpryker\Zed\SalesNewsletterSignup\Communication;

use FondOfSpryker\Service\Newsletter\NewsletterServiceInterface;
use FondOfSpryker\Zed\SalesNewsletterSignup\SalesNewsletterSignupDependencyProvider;
use Spryker\Zed\Kernel\Communication\AbstractCommunicationFactory;

class SalesNewsletterSignupCommunicationFactory extends AbstractCommunicationFactory
{
/**
* @return \FondOfSpryker\Service\Newsletter\NewsletterServiceInterface
*/
public function getNewsletterService(): NewsletterServiceInterface
{
return $this->getProvidedDependency(SalesNewsletterSignupDependencyProvider::SERVICE_NEWSLETTER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace FondOfSpryker\Zed\SalesNewsletterSignup;

use Spryker\Zed\Kernel\AbstractBundleDependencyProvider;
use Spryker\Zed\Kernel\Container;

class SalesNewsletterSignupDependencyProvider extends AbstractBundleDependencyProvider
{
public const SERVICE_NEWSLETTER = 'SERVICE_NEWSLETTER';
public const FACADE_STORE = 'FACADE_STORE';

/**
* @param \Spryker\Zed\Kernel\Container $container
*
* @return \Spryker\Zed\Kernel\Container
*/
public function provideCommunicationLayerDependencies(Container $container)
{
$container = parent::provideCommunicationLayerDependencies($container);
$container = $this->addNewsletterService($container);
$container = $this->addStoreFacade($container);

return $container;
}

/**
* @param \Spryker\Zed\Kernel\Container $container
*
* @return \Spryker\Zed\Kernel\Container
*/
protected function addNewsletterService(Container $container)
{
$container[static::SERVICE_NEWSLETTER] = function (Container $container) {
return $container->getLocator()->newsletter()->service();
};

return $container;
}

/**
* @param \Spryker\Zed\Kernel\Container $container
*
* @return \Spryker\Zed\Kernel\Container
*/
protected function addStoreFacade(Container $container)
{
$container[static::FACADE_STORE] = function (Container $container) {
return $container->getLocator()->store()->facade();
};

return $container;
}
}

0 comments on commit e947509

Please sign in to comment.