|
| 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 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\WebserviceKey; |
| 24 | + |
| 25 | +use ApiPlatform\Metadata\ApiProperty; |
| 26 | +use ApiPlatform\Metadata\ApiResource; |
| 27 | +use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\AddWebserviceKeyCommand; |
| 28 | +use PrestaShop\PrestaShop\Core\Domain\Webservice\Command\EditWebserviceKeyCommand; |
| 29 | +use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceConstraintException; |
| 30 | +use PrestaShop\PrestaShop\Core\Domain\Webservice\Exception\WebserviceKeyNotFoundException; |
| 31 | +use PrestaShop\PrestaShop\Core\Domain\Webservice\Query\GetWebserviceKeyForEditing; |
| 32 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate; |
| 33 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; |
| 34 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSPartialUpdate; |
| 35 | +use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType; |
| 36 | +use Symfony\Component\HttpFoundation\Response; |
| 37 | +use Symfony\Component\Validator\Constraints as Assert; |
| 38 | + |
| 39 | +#[ApiResource( |
| 40 | + operations: [ |
| 41 | + new CQRSCreate( |
| 42 | + uriTemplate: '/webservice-key', |
| 43 | + validationContext: ['groups' => ['Default', 'Create']], |
| 44 | + CQRSCommand: AddWebserviceKeyCommand::class, |
| 45 | + scopes: ['webservice_key_write'], |
| 46 | + CQRSCommandMapping: [ |
| 47 | + '[shopIds]' => '[associatedShops]', |
| 48 | + '[enabled]' => '[status]', |
| 49 | + ], |
| 50 | + ), |
| 51 | + new CQRSGet( |
| 52 | + uriTemplate: '/webservice-key/{webserviceKeyId}', |
| 53 | + requirements: ['webserviceKeyId' => '\d+'], |
| 54 | + CQRSQuery: GetWebserviceKeyForEditing::class, |
| 55 | + scopes: ['webservice_key_read'], |
| 56 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 57 | + ), |
| 58 | + new CQRSPartialUpdate( |
| 59 | + uriTemplate: '/webservice-key/{webserviceKeyId}', |
| 60 | + read: false, |
| 61 | + CQRSCommand: EditWebserviceKeyCommand::class, |
| 62 | + CQRSQuery: GetWebserviceKeyForEditing::class, |
| 63 | + scopes: ['webservice_key_write'], |
| 64 | + CQRSCommandMapping: [ |
| 65 | + '[shopIds]' => '[shopAssociation]', |
| 66 | + '[enabled]' => '[status]', |
| 67 | + ], |
| 68 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 69 | + ), |
| 70 | + ], |
| 71 | + normalizationContext: ['skip_null_values' => false], |
| 72 | + exceptionToStatus: [ |
| 73 | + WebserviceKeyNotFoundException::class => Response::HTTP_NOT_FOUND, |
| 74 | + WebserviceConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 75 | + ], |
| 76 | +)] |
| 77 | +class WebserviceKey |
| 78 | +{ |
| 79 | + #[ApiProperty(identifier: true)] |
| 80 | + public int $webserviceKeyId; |
| 81 | + |
| 82 | + #[Assert\NotBlank(groups: ['Create'])] |
| 83 | + #[Assert\Length(min: 32, max: 32)] |
| 84 | + public string $key; |
| 85 | + |
| 86 | + #[Assert\NotBlank(groups: ['Create'])] |
| 87 | + #[Assert\Length(min: 1, max: FormattedTextareaType::LIMIT_MEDIUMTEXT_UTF8_MB4)] |
| 88 | + public string $description; |
| 89 | + |
| 90 | + #[Assert\NotNull(groups: ['Create'])] |
| 91 | + public bool $enabled; |
| 92 | + |
| 93 | + #[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'array']])] |
| 94 | + #[Assert\Collection( |
| 95 | + fields: [ |
| 96 | + 'DELETE' => new Assert\All( |
| 97 | + constraints: [ |
| 98 | + new Assert\Type(type: 'string'), |
| 99 | + ], |
| 100 | + groups: ['Create'] |
| 101 | + ), |
| 102 | + 'GET' => new Assert\All( |
| 103 | + constraints: [ |
| 104 | + new Assert\Type(type: 'string'), |
| 105 | + ], |
| 106 | + groups: ['Create'] |
| 107 | + ), |
| 108 | + 'HEAD' => new Assert\All( |
| 109 | + constraints: [ |
| 110 | + new Assert\Type(type: 'string'), |
| 111 | + ], |
| 112 | + groups: ['Create'] |
| 113 | + ), |
| 114 | + 'PATCH' => new Assert\All( |
| 115 | + constraints: [ |
| 116 | + new Assert\Type(type: 'string'), |
| 117 | + ], |
| 118 | + groups: ['Create'] |
| 119 | + ), |
| 120 | + 'PUT' => new Assert\All( |
| 121 | + constraints: [ |
| 122 | + new Assert\Type(type: 'string'), |
| 123 | + ], |
| 124 | + groups: ['Create'] |
| 125 | + ), |
| 126 | + 'POST' => new Assert\All( |
| 127 | + constraints: [ |
| 128 | + new Assert\Type(type: 'string'), |
| 129 | + ], |
| 130 | + groups: ['Create'] |
| 131 | + ), |
| 132 | + ], |
| 133 | + allowMissingFields: true, |
| 134 | + groups: ['Create'])] |
| 135 | + public array $permissions; |
| 136 | + |
| 137 | + #[ApiProperty(openapiContext: ['type' => 'array', 'items' => ['type' => 'integer'], 'example' => [1, 3]])] |
| 138 | + #[Assert\NotBlank(allowNull: true)] |
| 139 | + public array $shopIds; |
| 140 | + |
| 141 | + public const QUERY_MAPPING = [ |
| 142 | + '[status]' => '[enabled]', |
| 143 | + '[resourcePermissions]' => '[permissions]', |
| 144 | + '[associatedShops]' => '[shopIds]', |
| 145 | + ]; |
| 146 | + |
| 147 | + public function setEnabled(string|bool $enabled): self |
| 148 | + { |
| 149 | + $this->enabled = (bool) $enabled; |
| 150 | + |
| 151 | + return $this; |
| 152 | + } |
| 153 | +} |
0 commit comments