|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Integration\Aws\Translate; |
| 6 | + |
| 7 | +use Alchemy\Workflow\Executor\RunContext; |
| 8 | +use App\Api\Model\Input\Attribute\AssetAttributeBatchUpdateInput; |
| 9 | +use App\Api\Model\Input\Attribute\AttributeActionInput; |
| 10 | +use App\Asset\Attribute\AttributesResolver; |
| 11 | +use App\Attribute\AttributeInterface; |
| 12 | +use App\Attribute\AttributeManager; |
| 13 | +use App\Attribute\BatchAttributeManager; |
| 14 | +use App\Entity\Core\Attribute; |
| 15 | +use App\Integration\AbstractIntegrationAction; |
| 16 | +use App\Integration\ApiBudgetLimiter; |
| 17 | +use App\Integration\IfActionInterface; |
| 18 | +use App\Integration\IntegrationConfig; |
| 19 | +use Aws\Translate\TranslateClient; |
| 20 | + |
| 21 | +class TranslateAction extends AbstractIntegrationAction implements IfActionInterface |
| 22 | +{ |
| 23 | + public function __construct( |
| 24 | + private readonly ApiBudgetLimiter $apiBudgetLimiter, |
| 25 | + private readonly AttributesResolver $attributesResolver, |
| 26 | + private readonly AttributeManager $attributeManager, |
| 27 | + private BatchAttributeManager $batchAttributeManager, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function doHandle(RunContext $context): void |
| 32 | + { |
| 33 | + $config = $this->getIntegrationConfig($context); |
| 34 | + $asset = $this->getAsset($context); |
| 35 | + |
| 36 | + $this->apiBudgetLimiter->acceptIntegrationApiCall($config); |
| 37 | + |
| 38 | + $sourceLangage = $config['source_lng']; |
| 39 | + $destinationLanguage = $config['destination_lng']; |
| 40 | + |
| 41 | + $attributeIndex = $this->attributesResolver->resolveAssetAttributes($asset, false); |
| 42 | + |
| 43 | + $attrDefs = $this->attributeManager->getAttributeDefinitions($asset->getWorkspaceId()); |
| 44 | + |
| 45 | + foreach ($attrDefs as $attrDef) { |
| 46 | + if (!$attrDef->isTranslatable()) { |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + $text = $attributeIndex->getAttribute($attrDef->getId(), AttributeInterface::NO_LOCALE)?->getValue(); |
| 51 | + if (empty($text)) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + $client = $this->createClient($config); |
| 55 | + |
| 56 | + $result = $client->translateText([ |
| 57 | + 'Settings' => [ |
| 58 | + 'Brevity' => 'ON', |
| 59 | + 'Formality' => 'FORMAL', |
| 60 | + 'Profanity' => 'MASK', |
| 61 | + ], |
| 62 | + 'SourceLanguageCode' => $sourceLangage, |
| 63 | + 'TargetLanguageCode' => $destinationLanguage, |
| 64 | + |
| 65 | + 'Text' => $text, |
| 66 | + ]); |
| 67 | + |
| 68 | + $translatedText = $result->get('TranslatedText'); |
| 69 | + |
| 70 | + $input = new AssetAttributeBatchUpdateInput(); |
| 71 | + $i = new AttributeActionInput(); |
| 72 | + $i->definitionId = $attrDef->getId(); |
| 73 | + $i->action = BatchAttributeManager::ACTION_SET; |
| 74 | + $i->origin = Attribute::ORIGIN_MACHINE; |
| 75 | + $i->originVendor = AwsTranslateIntegration::getName(); |
| 76 | + $i->value = $translatedText; |
| 77 | + $i->locale = $destinationLanguage; |
| 78 | + $input->actions[] = $i; |
| 79 | + |
| 80 | + $this->batchAttributeManager->handleBatch( |
| 81 | + $asset->getWorkspaceId(), |
| 82 | + [$asset->getId()], |
| 83 | + $input, |
| 84 | + null |
| 85 | + ); |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private function createClient(IntegrationConfig $options): TranslateClient |
| 91 | + { |
| 92 | + return new TranslateClient([ |
| 93 | + 'region' => $options['region'], |
| 94 | + 'credentials' => [ |
| 95 | + 'key' => $options['accessKeyId'], |
| 96 | + 'secret' => $options['accessKeySecret'], |
| 97 | + ], |
| 98 | + 'version' => 'latest', |
| 99 | + ]); |
| 100 | + } |
| 101 | +} |
0 commit comments