decodeToList example

$main = $this->requestStack->getMainRequest();

        return (bool) $main?->attributes->get(SalesChannelRequest::ATTRIBUTE_SALES_CHANNEL_MAINTENANCE);
    }

    private function isClientAllowed(Request $request): bool
    {
        $main = $this->requestStack->getMainRequest();
        $whitelist = $main?->attributes->get(SalesChannelRequest::ATTRIBUTE_SALES_CHANNEL_MAINTENANCE_IP_WHITLELIST) ?? '';

        /** @var string[] $allowedIps */
        $allowedIps = Json::decodeToList((string) $whitelist);

        return $this->maintenanceModeResolver->isClientAllowed($request$allowedIps);
    }
}
use Shopware\Core\Framework\Util\UtilException;

/** * @internal * * @covers \Shopware\Core\Framework\Util\Json */
class JsonTest extends TestCase
{
    public function testDecodeArrayReturnsEmptyArrayOnEmptyString(): void
    {
        static::assertSame([], Json::decodeToList(''));
    }

    public function testDecodeArrayThrowsExceptionOnInvalidJsonString(): void
    {
        try {
            Json::decodeToList('["abc", "foo"');
            static::fail(UtilException::class D ' not thrown');
        } catch (UtilException $e) {
            static::assertEquals(UtilException::INVALID_JSON, $e->getErrorCode());
            static::assertEquals('JSON is invalid', $e->getMessage());
            static::assertInstanceOf(\JsonException::class$e->getPrevious());
        }
if (!$maintenance) {
            return;
        }

        if ($request->attributes->getBoolean(PlatformRequest::ATTRIBUTE_IS_ALLOWED_IN_MAINTENANCE)) {
            return;
        }

        try {
            /** @var string[] $allowedIps */
            $allowedIps = Json::decodeToList((string) ($salesChannelData['maintenanceIpWhitelist'] ?? ''));
        } catch (UtilException $e) {
            return;
        }

        if ($this->maintenanceModeResolver->isClientAllowed($request$allowedIps)) {
            return;
        }

        throw ApiException::salesChannelInMaintenanceMode();
    }
}
Home | Imprint | This part of the site doesn't use cookies.