|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright since 2007 PrestaShop SA and Contributors |
| 4 | + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA |
| 5 | + * |
| 6 | + * NOTICE OF LICENSE |
| 7 | + * |
| 8 | + * This source file is subject to the Academic Free License version 3.0 |
| 9 | + * that is bundled with this package in the file LICENSE.md. |
| 10 | + * It is also available through the world-wide-web at this URL: |
| 11 | + * https://opensource.org/licenses/AFL-3.0 |
| 12 | + * If you did not receive a copy of the license and are unable to |
| 13 | + * obtain it through the world-wide-web, please send an email |
| 14 | + * to license@prestashop.com so we can send you a copy immediately. |
| 15 | + * |
| 16 | + * @author PrestaShop SA and Contributors <contact@prestashop.com> |
| 17 | + * @copyright Since 2007 PrestaShop SA and Contributors |
| 18 | + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 |
| 19 | + */ |
| 20 | + |
| 21 | +namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\Attachment; |
| 22 | + |
| 23 | +use ApiPlatform\Metadata\ApiProperty; |
| 24 | +use ApiPlatform\Metadata\ApiResource; |
| 25 | +use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\DefaultLanguage; |
| 26 | +use PrestaShop\PrestaShop\Core\ConstraintValidator\Constraints\TypedRegex; |
| 27 | +use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\AddAttachmentCommand; |
| 28 | +use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\DeleteAttachmentCommand; |
| 29 | +use PrestaShop\PrestaShop\Core\Domain\Attachment\Command\EditAttachmentCommand; |
| 30 | +use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentConstraintException; |
| 31 | +use PrestaShop\PrestaShop\Core\Domain\Attachment\Exception\AttachmentNotFoundException; |
| 32 | +use PrestaShop\PrestaShop\Core\Domain\Attachment\Query\GetAttachmentForEditing; |
| 33 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate; |
| 34 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete; |
| 35 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; |
| 36 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate; |
| 37 | +use PrestaShopBundle\ApiPlatform\Metadata\LocalizedValue; |
| 38 | +use Symfony\Component\HttpFoundation\File\File; |
| 39 | +use Symfony\Component\HttpFoundation\Response; |
| 40 | +use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
| 41 | +use Symfony\Component\Validator\Constraints as Assert; |
| 42 | + |
| 43 | +#[ApiResource( |
| 44 | + operations: [ |
| 45 | + new CQRSGet( |
| 46 | + uriTemplate: '/attachments/{attachmentId}', |
| 47 | + CQRSQuery: GetAttachmentForEditing::class, |
| 48 | + scopes: [ |
| 49 | + 'attachment_read', |
| 50 | + ], |
| 51 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 52 | + ), |
| 53 | + new CQRSCreate( |
| 54 | + uriTemplate: '/attachments', |
| 55 | + inputFormats: ['multipart' => ['multipart/form-data']], |
| 56 | + // Form data value are all string so we disable type enforcement |
| 57 | + denormalizationContext: [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true], |
| 58 | + validationContext: ['groups' => ['Default', 'Create']], |
| 59 | + CQRSCommand: AddAttachmentCommand::class, |
| 60 | + CQRSQuery: GetAttachmentForEditing::class, |
| 61 | + scopes: [ |
| 62 | + 'attachment_write', |
| 63 | + ], |
| 64 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 65 | + CQRSCommandMapping: self::COMMAND_MAPPING, |
| 66 | + ), |
| 67 | + new CQRSUpdate( |
| 68 | + // We have to force POST request, because we cannot use PUT with files AND data |
| 69 | + method: CQRSUpdate::METHOD_POST, |
| 70 | + uriTemplate: '/attachments/{attachmentId}', |
| 71 | + inputFormats: ['multipart' => ['multipart/form-data']], |
| 72 | + validationContext: ['groups' => ['Default', 'Update']], |
| 73 | + // Form data value are all string so we disable type enforcement |
| 74 | + denormalizationContext: [ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT => true], |
| 75 | + CQRSCommand: EditAttachmentCommand::class, |
| 76 | + CQRSQuery: GetAttachmentForEditing::class, |
| 77 | + scopes: [ |
| 78 | + 'attachment_write', |
| 79 | + ], |
| 80 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 81 | + CQRSCommandMapping: self::COMMAND_MAPPING, |
| 82 | + ), |
| 83 | + new CQRSDelete( |
| 84 | + uriTemplate: '/attachments/{attachmentId}', |
| 85 | + CQRSCommand: DeleteAttachmentCommand::class, |
| 86 | + scopes: [ |
| 87 | + 'attachment_write', |
| 88 | + ], |
| 89 | + ), |
| 90 | + ], |
| 91 | + exceptionToStatus: [ |
| 92 | + AttachmentConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 93 | + AttachmentNotFoundException::class => Response::HTTP_NOT_FOUND, |
| 94 | + ], |
| 95 | +)] |
| 96 | +class Attachment |
| 97 | +{ |
| 98 | + #[ApiProperty(identifier: true)] |
| 99 | + public int $attachmentId; |
| 100 | + |
| 101 | + #[LocalizedValue] |
| 102 | + #[DefaultLanguage(groups: ['Create'], fieldName: 'names')] |
| 103 | + #[DefaultLanguage(groups: ['Update'], fieldName: 'names', allowNull: true)] |
| 104 | + #[Assert\All(constraints: [ |
| 105 | + new TypedRegex([ |
| 106 | + 'type' => TypedRegex::TYPE_CATALOG_NAME, |
| 107 | + ]), |
| 108 | + ])] |
| 109 | + public array $names; |
| 110 | + |
| 111 | + #[LocalizedValue] |
| 112 | + #[DefaultLanguage(groups: ['Create'], fieldName: 'descriptions')] |
| 113 | + #[DefaultLanguage(groups: ['Update'], fieldName: 'descriptions', allowNull: true)] |
| 114 | + public array $descriptions; |
| 115 | + |
| 116 | + public ?File $attachment = null; |
| 117 | + |
| 118 | + public const QUERY_MAPPING = [ |
| 119 | + '[name]' => '[names]', |
| 120 | + '[description]' => '[descriptions]', |
| 121 | + ]; |
| 122 | + |
| 123 | + public const COMMAND_MAPPING = [ |
| 124 | + '[names]' => '[localizedNames]', |
| 125 | + '[descriptions]' => '[localizedDescriptions]', |
| 126 | + '[attachment].pathName' => '[pathName]', |
| 127 | + '[attachment].size' => '[fileSize]', |
| 128 | + '[attachment].mimeType' => '[mimeType]', |
| 129 | + '[attachment].originalName' => '[originalName]', |
| 130 | + '[attachment].clientOriginalName' => '[originalName]', |
| 131 | + ]; |
| 132 | +} |
0 commit comments