-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenApi PayDocumentationModifier for Sylius >=1.13 and Swagger PayDoc…
…umentationNormalizer for Sylius <1.12
- Loading branch information
Showing
5 changed files
with
132 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
]) | ||
; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
src/Api/Documentation/OpenApi/PayDocumentationModifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/Api/Documentation/Swagger/PayDocumentationNormalizer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |