Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions phpstan-6.7.0.0.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,28 @@ parameters:

- message: '#(Call to an undefined method.*)?Shopware\\Core\\Framework\\Struct\\Struct::(all|getExtensionOfType)\(\).*(, string given)?#'
path: src/Checkout/Payment/Service/VaultTokenService.php

- message: '#Shopware\\Core\\Content\\Cookie\\Event\\CookieGroupCollectEvent#'
paths:
- src/Storefront/Framework/Cookie/CookieProviderSubscriber.php
- tests/Storefront/Framework/Cookie/CookieProviderSubscriberTest.php

- message: '#Shopware\\Core\\Content\\Cookie\\Struct\\CookieEntry#'
paths:
- src/Storefront/Framework/Cookie/CookieProviderSubscriber.php
- tests/Storefront/Framework/Cookie/CookieProviderSubscriberTest.php

- message: '#Shopware\\Core\\Content\\Cookie\\Struct\\CookieGroup#'
path: tests/Storefront/Framework/Cookie/CookieProviderSubscriberTest.php

- message: '#^Mixed variable in a `\$(required|entries)\-\>\.\.\.\(\)` can skip important errors\. Make sure the type is known$#'
identifier: typePerfect.noMixedMethodCaller
paths:
- src/Storefront/Framework/Cookie/CookieProviderSubscriber.php
- tests/Storefront/Framework/Cookie/CookieProviderSubscriberTest.php

- message: '#^Call to internal static method Shopware\\Core\\Framework\\Context\:\:createDefaultContext\(\) from outside its root namespace Shopware\.$#'
identifier: staticMethod.internal
paths:
- src/Storefront/Framework/Cookie/GooglePayCookieProvider.php
- src/Storefront/Framework/Cookie/PayPalCookieProvider.php
5 changes: 3 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ parameters:
-
message: '#^Call to internal static method Shopware\\Core\\Framework\\Context\:\:createDefaultContext\(\) from outside its root namespace Shopware\.$#'
identifier: staticMethod.internal
count: 1
path: src/Storefront/Framework/Cookie/GooglePayCookieProvider.php
paths:
- src/Storefront/Framework/Cookie/GooglePayCookieProvider.php
- src/Storefront/Framework/Cookie/PayPalCookieProvider.php

-
message: '#^Mixed variable in a `\$criteria\-\>\.\.\.\(\)` can skip important errors\. Make sure the type is known$#'
Expand Down
9 changes: 9 additions & 0 deletions src/Resources/config/services/storefront.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@
</call>
</service>

<service id="Swag\PayPal\Storefront\Framework\Cookie\CookieProviderSubscriber">
<argument type="service" id="payment_method.repository"/>

<tag name="kernel.event_subscriber"/>
</service>

<service id="Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider"
decorates="Shopware\Storefront\Framework\Cookie\CookieProviderInterface">
<argument type="service" id="Swag\PayPal\Storefront\Framework\Cookie\PayPalCookieProvider.inner"/>
<argument type="service" id="payment_method.repository"/>
<argument type="service" id="request_stack"/>
</service>

<service id="Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider"
decorates="Shopware\Storefront\Framework\Cookie\CookieProviderInterface">
<argument type="service" id="Swag\PayPal\Storefront\Framework\Cookie\GooglePayCookieProvider.inner"/>
<argument type="service" id="payment_method.repository"/>
<argument type="service" id="request_stack"/>
</service>

<service id="Swag\PayPal\Storefront\RequestSubscriber">
Expand Down
85 changes: 85 additions & 0 deletions src/Storefront/Framework/Cookie/CookieProviderSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php declare(strict_types=1);
/*
* (c) shopware AG <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Swag\PayPal\Storefront\Framework\Cookie;

use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
use Shopware\Core\Content\Cookie\Event\CookieGroupCollectEvent;
use Shopware\Core\Content\Cookie\Struct\CookieEntry;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\PrefixFilter;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Swag\PayPal\Checkout\Payment\Method\GooglePayHandler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @internal
*/
#[Package('checkout')]
class CookieProviderSubscriber implements EventSubscriberInterface
{
/**
* @param EntityRepository<PaymentMethodCollection> $paymentMethodRepository
*/
public function __construct(
private readonly EntityRepository $paymentMethodRepository,
) {
}

public static function getSubscribedEvents(): array
{
return [
CookieGroupCollectEvent::class => 'onCookieGroupCollect',
];
}

public function onCookieGroupCollect(CookieGroupCollectEvent $event): void
{
$required = $event->cookieGroupCollection->get('cookie.groupRequired');
if (!($entries = $required?->getEntries())) {
return;
}

$payPalCookie = new CookieEntry('paypal-cookie-key');
$payPalCookie->name = 'paypal.cookie.name';

if ($this->isGooglePayActive($event->getSalesChannelContext())) {
$googleCookie = new CookieEntry('paypal-google-pay-cookie-key');
$googleCookie->name = 'paypal.cookie.googlePay';

$entries->add($payPalCookie);
$entries->add($googleCookie);
} elseif ($this->isPayPalPaymentActive($event->getSalesChannelContext())) {
$entries->add($payPalCookie);
}
}

private function isPayPalPaymentActive(SalesChannelContext $salesChannelContext): bool
{
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new PrefixFilter('technicalName', 'swag_paypal_'));
$criteria->addFilter(new EqualsFilter('salesChannels.id', $salesChannelContext->getSalesChannelId()));

return $this->paymentMethodRepository->searchIds($criteria, $salesChannelContext->getContext())->firstId() !== null;
}

private function isGooglePayActive(SalesChannelContext $salesChannelContext): bool
{
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new EqualsFilter('handlerIdentifier', GooglePayHandler::class));
$criteria->addFilter(new EqualsFilter('salesChannels.id', $salesChannelContext->getSalesChannelId()));

return $this->paymentMethodRepository->searchIds($criteria, $salesChannelContext->getContext())->firstId() !== null;
}
}
18 changes: 17 additions & 1 deletion src/Storefront/Framework/Cookie/GooglePayCookieProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@

namespace Swag\PayPal\Storefront\Framework\Cookie;

use Shopware\Core\Content\Cookie\Event\CookieGroupCollectEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
use Swag\PayPal\Checkout\Payment\Method\GooglePayHandler;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @deprecated tag:v11.0.0 - Will be removed. Use {@see CookieGroupCollectEvent} instead to introduce cookies.
Expand All @@ -31,6 +34,7 @@ class GooglePayCookieProvider implements CookieProviderInterface
public function __construct(
CookieProviderInterface $cookieProvider,
private readonly EntityRepository $paymentMethodRepository,
private readonly RequestStack $requestStack,
) {
$this->original = $cookieProvider;
}
Expand All @@ -41,6 +45,9 @@ public function __construct(
public function getCookieGroups(): array
{
$cookies = $this->original->getCookieGroups();
if (\class_exists(CookieGroupCollectEvent::class)) {
return $cookies;
}

foreach ($cookies as &$cookie) {
if (!\is_array($cookie)) {
Expand Down Expand Up @@ -75,9 +82,18 @@ private function isRequiredCookieGroup(array $cookie): bool
private function isGooglePayActive(): bool
{
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new EqualsFilter('handlerIdentifier', GooglePayHandler::class));

return $this->paymentMethodRepository->searchIds($criteria, Context::createDefaultContext())->firstId() !== null;
$mainRequestAttributes = $this->requestStack->getMainRequest()?->attributes;
$context = $mainRequestAttributes?->get('sw-context') ?? Context::createDefaultContext();
$salesChannelId = $mainRequestAttributes?->getString('sw-sales-channel-id') ?? '';

if (Uuid::isValid($salesChannelId)) {
$criteria->addFilter(new EqualsFilter('salesChannels.id', $salesChannelId));
}

return $this->paymentMethodRepository->searchIds($criteria, $context)->firstId() !== null;
}
}
40 changes: 38 additions & 2 deletions src/Storefront/Framework/Cookie/PayPalCookieProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

namespace Swag\PayPal\Storefront\Framework\Cookie;

use Shopware\Core\Content\Cookie\Event\CookieGroupCollectEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\PrefixFilter;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* @deprecated tag:v11.0.0 - Will be removed. Use {@see CookieGroupCollectEvent} instead to introduce cookies.
Expand All @@ -23,8 +31,11 @@ class PayPalCookieProvider implements CookieProviderInterface
*
* @deprecated tag:v11.0.0 - Will be removed. Use {@see CookieGroupCollectEvent} instead to introduce cookies.
*/
public function __construct(CookieProviderInterface $cookieProvider)
{
public function __construct(
CookieProviderInterface $cookieProvider,
private readonly EntityRepository $paymentMethodRepository,
private readonly RequestStack $requestStack,
) {
$this->original = $cookieProvider;
}

Expand All @@ -34,6 +45,9 @@ public function __construct(CookieProviderInterface $cookieProvider)
public function getCookieGroups(): array
{
$cookies = $this->original->getCookieGroups();
if (\class_exists(CookieGroupCollectEvent::class)) {
return $cookies;
}

foreach ($cookies as &$cookie) {
if (!\is_array($cookie)) {
Expand All @@ -48,6 +62,10 @@ public function getCookieGroups(): array
continue;
}

if (!$this->isPayPalPaymentActive()) {
continue;
}

$cookie['entries'][] = [
'snippet_name' => 'paypal.cookie.name',
'cookie' => 'paypal-cookie-key',
Expand All @@ -62,4 +80,22 @@ private function isRequiredCookieGroup(array $cookie): bool
return (\array_key_exists('isRequired', $cookie) && $cookie['isRequired'] === true)
&& (\array_key_exists('snippet_name', $cookie) && $cookie['snippet_name'] === 'cookie.groupRequired');
}

private function isPayPalPaymentActive(): bool
{
$criteria = new Criteria();
$criteria->setLimit(1);
$criteria->addFilter(new EqualsFilter('active', true));
$criteria->addFilter(new PrefixFilter('technicalName', 'swag_paypal_'));

$mainRequestAttributes = $this->requestStack->getMainRequest()?->attributes;
$context = $mainRequestAttributes?->get('sw-context') ?? Context::createDefaultContext();
$salesChannelId = $mainRequestAttributes?->getString('sw-sales-channel-id') ?? '';

if (Uuid::isValid($salesChannelId)) {
$criteria->addFilter(new EqualsFilter('salesChannels.id', $salesChannelId));
}

return $this->paymentMethodRepository->searchIds($criteria, $context)->firstId() !== null;
}
}
Loading