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
56 changes: 32 additions & 24 deletions src/Context/ContextResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTimeImmutable;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use Shopware\App\SDK\Context\ActionButton\ActionButtonAction;
use Shopware\App\SDK\Context\Cart\Cart;
use Shopware\App\SDK\Context\Gateway\Checkout\CheckoutGatewayAction;
Expand Down Expand Up @@ -47,8 +48,7 @@
*/
public function assembleWebhook(RequestInterface $request, ShopInterface $shop): WebhookAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -65,8 +65,7 @@

public function assembleActionButton(RequestInterface $request, ShopInterface $shop): ActionButtonAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand Down Expand Up @@ -110,8 +109,7 @@

public function assembleTaxProvider(RequestInterface $request, ShopInterface $shop): TaxProviderAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -127,8 +125,7 @@

public function assemblePaymentPay(RequestInterface $request, ShopInterface $shop): PaymentPayAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -147,8 +144,7 @@

public function assemblePaymentFinalize(RequestInterface $request, ShopInterface $shop): PaymentFinalizeAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -166,8 +162,7 @@

public function assemblePaymentCapture(RequestInterface $request, ShopInterface $shop): PaymentCaptureAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -185,8 +180,7 @@

public function assemblePaymentRecurringCapture(RequestInterface $request, ShopInterface $shop): PaymentRecurringAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -203,8 +197,7 @@

public function assemblePaymentValidate(RequestInterface $request, ShopInterface $shop): PaymentValidateAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -221,8 +214,7 @@

public function assemblePaymentRefund(RequestInterface $request, ShopInterface $shop): RefundAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand Down Expand Up @@ -275,8 +267,7 @@

public function assembleCheckoutGatewayRequest(RequestInterface $request, ShopInterface $shop): CheckoutGatewayAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -294,8 +285,7 @@

public function assembleContextGatewayRequest(RequestInterface $request, ShopInterface $shop): ContextGatewayAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source']) || !isset($body['data']) || !\is_array($body['data'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -312,8 +302,7 @@

public function assembleInAppPurchasesFilterRequest(RequestInterface $request, ShopInterface $shop): FilterAction
{
$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();
$body = $this->getBody($request);

if (!\is_array($body) || !isset($body['source']) || !\is_array($body['source'])) {
throw new MalformedWebhookBodyException();
Expand All @@ -330,6 +319,25 @@
);
}

/**
* @throws \JsonException
*/
private function getBody(RequestInterface $request): mixed
{
if ($request instanceof ServerRequestInterface) {
$body = $request->getParsedBody();

if ($body !== null) {
return $body;
}
}

$body = \json_decode($request->getBody()->getContents(), true, flags: \JSON_THROW_ON_ERROR);
$request->getBody()->rewind();

return $body;
}

/**
* @param array<string, mixed> $source
*/
Expand Down Expand Up @@ -367,7 +375,7 @@
{
foreach ($source as $key => $value) {
if (!\is_scalar($value)) {
continue;

Check warning on line 378 in src/Context/ContextResolver.php

View workflow job for this annotation

GitHub Actions / unit

Escaped Mutant for Mutator "Continue_": @@ @@ { foreach ($source as $key => $value) { if (!\is_scalar($value)) { - continue; + break; } yield (string) $value => $key; } } }
}

yield ((string) $value) => $key;
Expand Down
12 changes: 12 additions & 0 deletions tests/Context/ContextResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Shopware\App\SDK\Context\ContextResolver;
use Shopware\App\SDK\Context\InAppPurchase\InAppPurchase;
Expand Down Expand Up @@ -36,6 +37,17 @@ public function testAssembleWebhookMalformed(): void
);
}

public function testAssembleWebhookUsesParsedBody(): void
{
$contextResolver = new ContextResolver($this->createMock(InAppPurchaseProvider::class));
$request = $this->createMock(ServerRequestInterface::class);
$request->expects(static::once())->method('getParsedBody')->willReturn([]);
$request->expects(static::never())->method('getBody');

static::expectException(MalformedWebhookBodyException::class);
$contextResolver->assembleWebhook($request, $this->getShop());
}

public function testAssembleWebhook(): void
{
$collection = new Collection([
Expand Down
Loading