Skip to content

Commit

Permalink
OpenApi PayDocumentationModifier for Sylius >=1.13 and Swagger PayDoc…
Browse files Browse the repository at this point in the history
…umentationNormalizer for Sylius <1.12
  • Loading branch information
coldic3 committed Nov 1, 2024
1 parent ba35700 commit d9c6a21
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 66 deletions.
30 changes: 30 additions & 0 deletions config/services/api/documentation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use CommerceWeavers\SyliusTpayPlugin\Api\Documentation\OpenApi\PayDocumentationModifier;
use CommerceWeavers\SyliusTpayPlugin\Api\Documentation\Swagger\PayDocumentationNormalizer;
use Sylius\Bundle\CoreBundle\SyliusCoreBundle;

return static function(ContainerConfigurator $container): void {
$services = $container->services();

if (SyliusCoreBundle::VERSION_ID >= 11300) {
$services->set('commerce_weavers_sylius_tpay.api.documentation.open_api.pay_documentation_modifier', PayDocumentationModifier::class)
->args([
param('sylius.security.new_api_shop_route')
])
->tag('sylius.open_api.modifier')
;
} else {
$services->set('commerce_weavers_sylius_tpay.api.documentation.swagger.pay_documentation_normalizer', PayDocumentationNormalizer::class)
->decorate('api_platform.swagger.normalizer.documentation')
->args([
service('.inner'),
param('sylius.security.new_api_shop_route'),
])
;
}
};
18 changes: 0 additions & 18 deletions config/services/api/open_api.php

This file was deleted.

61 changes: 61 additions & 0 deletions src/Api/Documentation/OpenApi/PayDocumentationModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Api\Documentation\OpenApi;

use ApiPlatform\Core\OpenApi\Model\MediaType;
use ApiPlatform\OpenApi\OpenApi;
use CommerceWeavers\SyliusTpayPlugin\Api\Documentation\PayRequestBodyExampleFactory;
use Sylius\Bundle\ApiBundle\OpenApi\Documentation\DocumentationModifierInterface;

if (!interface_exists(DocumentationModifierInterface::class)) {
return;
}

final class PayDocumentationModifier implements DocumentationModifierInterface
{
public function __construct(private readonly string $apiShopRoutePrefix)
{
}

public function modify(OpenApi $docs): OpenApi
{
$paths = $docs->getPaths();
$path = sprintf('%s/orders/{tokenValue}/pay', $this->apiShopRoutePrefix);
$pathItem = $paths->getPath($path);

if (null === $pathItem) {
return $docs;
}

$post = $pathItem->getPost();

if (null === $post) {
return $docs;
}

$requestBody = $post->getRequestBody();

if (null === $requestBody) {
return $docs;
}

$content = $requestBody->getContent();

/** @var MediaType $mediaType */
$mediaType = $content['application/ld+json'];

$content['application/ld+json'] = $mediaType->withExamples(new \ArrayObject(PayRequestBodyExampleFactory::create()));

$pathItem = $pathItem->withPost(
$post->withRequestBody(
$requestBody->withContent($content),
),
);

$paths->addPath($path, $pathItem);

return $docs->withPaths($paths);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,20 @@

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Api\OpenApi;
namespace CommerceWeavers\SyliusTpayPlugin\Api\Documentation;

use ApiPlatform\Core\OpenApi\Model\MediaType;
use ApiPlatform\OpenApi\OpenApi;
use Sylius\Bundle\ApiBundle\OpenApi\Documentation\DocumentationModifierInterface;

final class PayDocumentationModifier implements DocumentationModifierInterface
final class PayRequestBodyExampleFactory
{
private const EXAMPLE_VALUE = 'string';

public function __construct(private readonly string $apiShopRoutePrefix)
{
}

public function modify(OpenApi $docs): OpenApi
public static function create(): array
{
$paths = $docs->getPaths();
$path = sprintf('%s/orders/{tokenValue}/pay', $this->apiShopRoutePrefix);
$pathItem = $paths->getPath($path);

if (null === $pathItem) {
return $docs;
}

$post = $pathItem->getPost();

if (null === $post) {
return $docs;
}

$requestBody = $post->getRequestBody();

if (null === $requestBody) {
return $docs;
}

$content = $requestBody->getContent();

/** @var MediaType $mediaType */
$mediaType = $content['application/ld+json'];

$commonExampleData = [
'successUrl' => self::EXAMPLE_VALUE,
'failureUrl' => self::EXAMPLE_VALUE,
];

$content['application/ld+json'] = $mediaType->withExamples(new \ArrayObject([
return [
'Pay by link' => [
'value' => $commonExampleData,
],
Expand Down Expand Up @@ -104,16 +71,6 @@ public function modify(OpenApi $docs): OpenApi
'visaMobilePhoneNumber' => self::EXAMPLE_VALUE,
],
],
]));

$pathItem = $pathItem->withPost(
$post->withRequestBody(
$requestBody->withContent($content),
),
);

$paths->addPath($path, $pathItem);

return $docs->withPaths($paths);
];
}
}
36 changes: 36 additions & 0 deletions src/Api/Documentation/Swagger/PayDocumentationNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace CommerceWeavers\SyliusTpayPlugin\Api\Documentation\Swagger;

use CommerceWeavers\SyliusTpayPlugin\Api\Documentation\PayRequestBodyExampleFactory;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class PayDocumentationNormalizer implements NormalizerInterface
{
public function __construct(
private readonly NormalizerInterface $decoratedNormalizer,
private readonly string $apiShopRoutePrefix,
) {
}

public function supportsNormalization($data, $format = null): bool
{
return $this->decoratedNormalizer->supportsNormalization($data, $format);
}

public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decoratedNormalizer->normalize($object, $format, $context);

$payPath = sprintf('%s/orders/{tokenValue}/pay', $this->apiShopRoutePrefix);
if (!isset($docs['paths'][$payPath]['post']['requestBody']['content']['application/ld+json'])) {
return $docs;
}

$docs['paths'][$payPath]['post']['requestBody']['content']['application/ld+json']['examples'] = PayRequestBodyExampleFactory::create();

return $docs;
}
}

0 comments on commit d9c6a21

Please sign in to comment.