CustomFieldsBulkActionEndpoint.php
2 months ago
CustomFieldsDuplicateEndpoint.php
2 months ago
CustomFieldsEndpoint.php
1 month ago
CustomFieldsGetEndpoint.php
1 month ago
CustomFieldsPostEndpoint.php
2 months ago
CustomFieldsPutEndpoint.php
2 months ago
index.php
2 months ago
CustomFieldsDuplicateEndpoint.php
94 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\CustomFields\RestApi\Endpoints; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\REST\Request; |
| 9 | use MailPoet\API\REST\Response; |
| 10 | use MailPoet\CustomFields\CustomFieldsRepository; |
| 11 | use MailPoet\CustomFields\RestApi\CustomFieldApiException; |
| 12 | use MailPoet\Entities\CustomFieldEntity; |
| 13 | use MailPoet\Validator\Builder; |
| 14 | use MailPoetVendor\Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
| 15 | |
| 16 | class CustomFieldsDuplicateEndpoint extends CustomFieldsEndpoint { |
| 17 | /** @var CustomFieldsRepository */ |
| 18 | private $customFieldsRepository; |
| 19 | |
| 20 | public function __construct( |
| 21 | CustomFieldsRepository $customFieldsRepository |
| 22 | ) { |
| 23 | $this->customFieldsRepository = $customFieldsRepository; |
| 24 | } |
| 25 | |
| 26 | public function handle(Request $request): Response { |
| 27 | $customField = $this->customFieldsRepository->findOneById($this->getId($request)); |
| 28 | if (!$customField instanceof CustomFieldEntity || $customField->getDeletedAt() !== null) { |
| 29 | throw new CustomFieldApiException( |
| 30 | __('The custom field does not exist.', 'mailpoet'), |
| 31 | 404, |
| 32 | 'mailpoet_custom_fields_not_found' |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | $params = $customField->getParams() ?: []; |
| 37 | $params['label'] = $this->getDuplicateLabel($params, $customField->getName()); |
| 38 | |
| 39 | $attempts = 0; |
| 40 | while (true) { |
| 41 | try { |
| 42 | $duplicate = $this->customFieldsRepository->createOrUpdate([ |
| 43 | 'name' => $this->getDuplicateName($customField->getName()), |
| 44 | 'type' => $customField->getType(), |
| 45 | 'params' => $params, |
| 46 | ]); |
| 47 | break; |
| 48 | } catch (UniqueConstraintViolationException $exception) { |
| 49 | // A concurrent duplicate request picked the same candidate name. Recompute and retry. |
| 50 | if (++$attempts >= 3) { |
| 51 | throw new CustomFieldApiException( |
| 52 | __('A custom field with this name already exists.', 'mailpoet'), |
| 53 | 409, |
| 54 | 'mailpoet_custom_fields_duplicate', |
| 55 | [], |
| 56 | $exception |
| 57 | ); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | return new Response($this->buildItem($duplicate), 201); |
| 63 | } |
| 64 | |
| 65 | private function getId(Request $request): int { |
| 66 | $rawId = $request->getParam('id'); |
| 67 | return (int)(is_scalar($rawId) ? $rawId : 0); |
| 68 | } |
| 69 | |
| 70 | private function getDuplicateName(string $name): string { |
| 71 | /* translators: %s is the original custom field name. */ |
| 72 | $candidate = sprintf(__('%s copy', 'mailpoet'), $name); |
| 73 | $suffix = 2; |
| 74 | while ($this->customFieldsRepository->findOneBy(['name' => $candidate]) instanceof CustomFieldEntity) { |
| 75 | /* translators: %1$s is the original custom field name, %2$d is the copy number. */ |
| 76 | $candidate = sprintf(__('%1$s copy %2$d', 'mailpoet'), $name, $suffix); |
| 77 | $suffix++; |
| 78 | } |
| 79 | return $candidate; |
| 80 | } |
| 81 | |
| 82 | private function getDuplicateLabel(array $params, string $fallback): string { |
| 83 | $label = isset($params['label']) && is_scalar($params['label']) ? (string)$params['label'] : $fallback; |
| 84 | /* translators: %s is the original custom field label. */ |
| 85 | return sprintf(__('%s copy', 'mailpoet'), $label); |
| 86 | } |
| 87 | |
| 88 | public static function getRequestSchema(): array { |
| 89 | return [ |
| 90 | 'id' => Builder::integer()->required(), |
| 91 | ]; |
| 92 | } |
| 93 | } |
| 94 |