<?php
declare(strict_types=1);
/*
* (c) shopware AG <info@shopware.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\AmazonPay\Storefront\EventListeners;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Swag\AmazonPay\Components\Button\ButtonProviderInterface;
use Swag\AmazonPay\Storefront\Page\Extension\AmazonPayButtonExtension;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AmazonPayButtonEventListener implements EventSubscriberInterface
{
/**
* @var ButtonProviderInterface
*/
private $buttonProvider;
public function __construct(
ButtonProviderInterface $buttonProvider
) {
$this->buttonProvider = $buttonProvider;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
CheckoutCartPageLoadedEvent::class => 'addAmazonPayButtonExtension',
OffcanvasCartPageLoadedEvent::class => 'addAmazonPayButtonExtension',
ProductPageLoadedEvent::class => 'addAmazonPayButtonExtension',
CheckoutRegisterPageLoadedEvent::class => 'addAmazonPayButtonExtension',
];
}
public function addAmazonPayButtonExtension(PageLoadedEvent $event): void
{
$buttonExtension = $this->buttonProvider->getAmazonPayButton($event);
if ($buttonExtension === null) {
return;
}
$event->getPage()->addExtension(AmazonPayButtonExtension::EXTENSION_NAME, $buttonExtension);
}
}