|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Swh\SmartRelationSync\ApiDefinition; |
| 6 | + |
| 7 | +use OpenApi\Annotations\Property; |
| 8 | +use OpenApi\Annotations\Schema; |
| 9 | +use Shopware\Core\Framework\Api\ApiDefinition\DefinitionService; |
| 10 | +use Shopware\Core\Framework\Api\ApiDefinition\Generator\OpenApi\OpenApiDefinitionSchemaBuilder; |
| 11 | +use Shopware\Core\Framework\Api\Context\AdminApiSource; |
| 12 | +use Shopware\Core\Framework\Api\Context\SalesChannelApiSource; |
| 13 | +use Shopware\Core\Framework\DataAbstractionLayer\EntityDefinition; |
| 14 | +use Shopware\Core\Framework\DataAbstractionLayer\Field\Field; |
| 15 | +use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\ApiAware; |
| 16 | +use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\IgnoreInOpenapiSchema; |
| 17 | +use Shopware\Core\Framework\DataAbstractionLayer\Field\ManyToManyAssociationField; |
| 18 | +use Shopware\Core\Framework\DataAbstractionLayer\Field\OneToManyAssociationField; |
| 19 | +use Swh\SmartRelationSync\DataAbstractionLayer\WriteCommandExtractorDecorator; |
| 20 | + |
| 21 | +class OpenApiDefinitionSchemaBuilderDecorator extends OpenApiDefinitionSchemaBuilder |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @return Schema[] |
| 25 | + */ |
| 26 | + public function getSchemaByDefinition( |
| 27 | + EntityDefinition $definition, |
| 28 | + string $path, |
| 29 | + bool $forSalesChannel, |
| 30 | + bool $onlyFlat = false, |
| 31 | + string $apiType = DefinitionService::TYPE_JSON_API, |
| 32 | + ): array { |
| 33 | + $schemas = parent::getSchemaByDefinition($definition, $path, $forSalesChannel, $onlyFlat, $apiType); |
| 34 | + |
| 35 | + if (count($schemas) !== 1) { |
| 36 | + return $schemas; |
| 37 | + } |
| 38 | + |
| 39 | + $relevantFields = $definition->getFields() |
| 40 | + ->filter(fn(Field $field) => $this->isRelevantField($field)); |
| 41 | + |
| 42 | + $schema = current($schemas); |
| 43 | + |
| 44 | + foreach ($relevantFields as $field) { |
| 45 | + if (!$this->shouldFieldBeIncluded($field, $forSalesChannel)) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + $enableFieldName = WriteCommandExtractorDecorator::getCleanupEnableFieldName($field); |
| 50 | + |
| 51 | + $property = new Property([ |
| 52 | + 'property' => $enableFieldName, |
| 53 | + 'type' => 'boolean', |
| 54 | + ]); |
| 55 | + |
| 56 | + $schema->properties[] = $property; |
| 57 | + } |
| 58 | + |
| 59 | + return $schemas; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @phpstan-assert-if-true ManyToManyAssociationField|OneToManyAssociationField $field |
| 64 | + */ |
| 65 | + private function isRelevantField(Field $field): bool |
| 66 | + { |
| 67 | + return WriteCommandExtractorDecorator::isRelevantField($field); |
| 68 | + } |
| 69 | + |
| 70 | + private function shouldFieldBeIncluded(Field $field, bool $forSalesChannel): bool |
| 71 | + { |
| 72 | + if ($field->getPropertyName() === 'translations' |
| 73 | + || preg_match('#translations$#i', $field->getPropertyName()) |
| 74 | + ) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + $ignoreOpenApiSchemaFlag = $field->getFlag(IgnoreInOpenapiSchema::class); |
| 79 | + if ($ignoreOpenApiSchemaFlag !== null) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + $flag = $field->getFlag(ApiAware::class); |
| 84 | + if ($flag === null) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + if (!$flag->isSourceAllowed($forSalesChannel ? SalesChannelApiSource::class : AdminApiSource::class)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + return true; |
| 93 | + } |
| 94 | +} |
0 commit comments