|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright since 2007 PrestaShop SA and Contributors |
| 5 | + * PrestaShop is an International Registered Trademark & Property of PrestaShop SA |
| 6 | + * |
| 7 | + * NOTICE OF LICENSE |
| 8 | + * |
| 9 | + * This source file is subject to the Academic Free License version 3.0 |
| 10 | + * that is bundled with this package in the file LICENSE.md. |
| 11 | + * It is also available through the world-wide-web at this URL: |
| 12 | + * https://opensource.org/licenses/AFL-3.0 |
| 13 | + * If you did not receive a copy of the license and are unable to |
| 14 | + * obtain it through the world-wide-web, please send an email |
| 15 | + * to [email protected] so we can send you a copy immediately. |
| 16 | + * |
| 17 | + * @author PrestaShop SA and Contributors <[email protected]> |
| 18 | + * @copyright Since 2007 PrestaShop SA and Contributors |
| 19 | + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 |
| 20 | + */ |
| 21 | + |
| 22 | +declare(strict_types=1); |
| 23 | + |
| 24 | +namespace PrestaShop\Module\APIResources\ApiPlatform\Resources\SearchAlias; |
| 25 | + |
| 26 | +use ApiPlatform\Metadata\ApiProperty; |
| 27 | +use ApiPlatform\Metadata\ApiResource; |
| 28 | +use PrestaShop\PrestaShop\Core\Domain\Alias\Command\AddSearchTermAliasesCommand; |
| 29 | +use PrestaShop\PrestaShop\Core\Domain\Alias\Command\DeleteSearchTermAliasesCommand; |
| 30 | +use PrestaShop\PrestaShop\Core\Domain\Alias\Command\UpdateSearchTermAliasesCommand; |
| 31 | +use PrestaShop\PrestaShop\Core\Domain\Alias\Exception\AliasConstraintException; |
| 32 | +use PrestaShop\PrestaShop\Core\Domain\Alias\Exception\AliasNotFoundException; |
| 33 | +use PrestaShop\PrestaShop\Core\Domain\Alias\Query\GetAliasesBySearchTermForEditing; |
| 34 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSCreate; |
| 35 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSDelete; |
| 36 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSGet; |
| 37 | +use PrestaShopBundle\ApiPlatform\Metadata\CQRSUpdate; |
| 38 | +use Symfony\Component\HttpFoundation\Response; |
| 39 | +use Symfony\Component\Validator\Constraints as Assert; |
| 40 | + |
| 41 | +#[ApiResource( |
| 42 | + operations: [ |
| 43 | + new CQRSGet( |
| 44 | + uriTemplate: '/search-alias/{search}', |
| 45 | + CQRSQuery: GetAliasesBySearchTermForEditing::class, |
| 46 | + scopes: ['search_alias_read'], |
| 47 | + exceptionToStatus: [AliasNotFoundException::class => 404], |
| 48 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 49 | + ), |
| 50 | + new CQRSCreate( |
| 51 | + uriTemplate: '/search-alias', |
| 52 | + validationContext: ['groups' => ['Default', 'Create']], |
| 53 | + CQRSCommand: AddSearchTermAliasesCommand::class, |
| 54 | + scopes: ['search_alias_write'], |
| 55 | + CQRSCommandMapping: self::COMMAND_MAPPING, |
| 56 | + ), |
| 57 | + new CQRSUpdate( |
| 58 | + uriTemplate: '/search-alias/{search}', |
| 59 | + CQRSCommand: UpdateSearchTermAliasesCommand::class, |
| 60 | + CQRSQuery: GetAliasesBySearchTermForEditing::class, |
| 61 | + scopes: ['search_alias_write'], |
| 62 | + CQRSCommandMapping: self::UPDATE_COMMAND_MAPPING, |
| 63 | + CQRSQueryMapping: self::QUERY_MAPPING, |
| 64 | + ), |
| 65 | + new CQRSDelete( |
| 66 | + uriTemplate: '/search-alias/{search}', |
| 67 | + CQRSCommand: DeleteSearchTermAliasesCommand::class, |
| 68 | + scopes: ['search_alias_write'], |
| 69 | + CQRSCommandMapping: self::DELETE_COMMAND_MAPPING, |
| 70 | + output: false, |
| 71 | + ), |
| 72 | + ], |
| 73 | + exceptionToStatus: [ |
| 74 | + AliasNotFoundException::class => Response::HTTP_NOT_FOUND, |
| 75 | + AliasConstraintException::class => Response::HTTP_UNPROCESSABLE_ENTITY, |
| 76 | + ], |
| 77 | +)] |
| 78 | +class SearchAlias |
| 79 | +{ |
| 80 | + #[ApiProperty(identifier: true)] |
| 81 | + #[Assert\NotBlank(groups: ['Create'])] |
| 82 | + #[Assert\Length(min: 1, max: 255)] |
| 83 | + public string $search = ''; |
| 84 | + |
| 85 | + #[Assert\NotBlank(groups: ['Create'])] |
| 86 | + #[Assert\Count(min: 1, groups: ['Create'])] |
| 87 | + #[Assert\All( |
| 88 | + constraints: [ |
| 89 | + new Assert\Collection( |
| 90 | + fields: [ |
| 91 | + 'alias' => new Assert\NotBlank(groups: ['Default', 'Create']), |
| 92 | + 'active' => new Assert\Type(type: 'bool', groups: ['Default', 'Create']), |
| 93 | + ], |
| 94 | + allowMissingFields: false, |
| 95 | + groups: ['Default', 'Create'] |
| 96 | + ), |
| 97 | + ], |
| 98 | + groups: ['Default', 'Create'] |
| 99 | + )] |
| 100 | + #[ApiProperty( |
| 101 | + openapiContext: [ |
| 102 | + 'type' => 'array', |
| 103 | + 'items' => [ |
| 104 | + 'type' => 'object', |
| 105 | + 'properties' => [ |
| 106 | + 'id_alias' => ['type' => 'integer'], |
| 107 | + 'alias' => ['type' => 'string'], |
| 108 | + 'active' => ['type' => 'boolean'], |
| 109 | + ], |
| 110 | + ], |
| 111 | + ] |
| 112 | + )] |
| 113 | + public array $aliases = []; |
| 114 | + |
| 115 | + protected const QUERY_MAPPING = [ |
| 116 | + '[search]' => '[searchTerm]', |
| 117 | + '[searchTerm]' => '[search]', |
| 118 | + ]; |
| 119 | + |
| 120 | + protected const COMMAND_MAPPING = [ |
| 121 | + '[search]' => '[searchTerm]', |
| 122 | + '[aliases]' => '[aliases]', |
| 123 | + ]; |
| 124 | + |
| 125 | + protected const UPDATE_COMMAND_MAPPING = [ |
| 126 | + '[search]' => '[oldSearchTerm]', |
| 127 | + '[aliases]' => '[aliases]', |
| 128 | + ]; |
| 129 | + |
| 130 | + protected const DELETE_COMMAND_MAPPING = [ |
| 131 | + '[search]' => '[searchTerm]', |
| 132 | + ]; |
| 133 | +} |
0 commit comments