-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Translate attributes of one produt at once. Refactor translation process
- Loading branch information
1 parent
ddb011e
commit e247a54
Showing
4 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...anslator/Connector/Job/JobParameters/ConstraintCollectionProvider/TranslateAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macopedia\OpenAiTranslator\Connector\Job\JobParameters\ConstraintCollectionProvider; | ||
|
||
use Akeneo\Tool\Component\Batch\Job\JobInterface; | ||
use Akeneo\Tool\Component\Batch\Job\JobParameters\ConstraintCollectionProviderInterface; | ||
use Symfony\Component\Validator\Constraints\All; | ||
use Symfony\Component\Validator\Constraints\Collection; | ||
use Symfony\Component\Validator\Constraints\NotNull; | ||
use Symfony\Component\Validator\Constraints\Type; | ||
|
||
class TranslateAttributes implements ConstraintCollectionProviderInterface | ||
{ | ||
/** | ||
* @param array<string> $supportedJobNames | ||
*/ | ||
public function __construct( | ||
private array $supportedJobNames, | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getConstraintCollection(): Collection | ||
{ | ||
return new Collection( | ||
[ | ||
'fields' => [ | ||
'filters' => new NotNull(), | ||
'actions' => new NotNull(), | ||
'realTimeVersioning' => new Type('bool'), | ||
'users_to_notify' => [ | ||
new Type('array'), | ||
new All(new Type('string')), | ||
], | ||
'is_user_authenticated' => new Type('bool'), | ||
'open_ai_key' => new NotNull(), | ||
] | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(JobInterface $job): bool | ||
{ | ||
return in_array($job->getName(), $this->supportedJobNames); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...OpenAiTranslator/Connector/Job/JobParameters/DefaultValueProvider/TranslateAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macopedia\OpenAiTranslator\Connector\Job\JobParameters\DefaultValueProvider; | ||
|
||
use Akeneo\Tool\Component\Batch\Job\JobInterface; | ||
use Akeneo\Tool\Component\Batch\Job\JobParameters\DefaultValuesProviderInterface; | ||
|
||
class TranslateAttributes implements DefaultValuesProviderInterface | ||
{ | ||
/** | ||
* @param array<string> $supportedJobNames | ||
*/ | ||
public function __construct( | ||
private array $supportedJobNames, | ||
private ?string $openAiKey | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultValues(): array | ||
{ | ||
return [ | ||
'filters' => [], | ||
'actions' => [], | ||
'realTimeVersioning' => true, | ||
'users_to_notify' => [], | ||
'is_user_authenticated' => false, | ||
'open_ai_key' => $this->openAiKey ?? '' | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(JobInterface $job): bool | ||
{ | ||
return in_array($job->getName(), $this->supportedJobNames); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/Macopedia/OpenAiTranslator/Connector/Tasklet/ValidateOpenAiKeyTasklet.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macopedia\OpenAiTranslator\Connector\Tasklet; | ||
|
||
use Akeneo\Tool\Component\Batch\Job\BatchStatus; | ||
use Akeneo\Tool\Component\Batch\Model\StepExecution; | ||
use Akeneo\Tool\Component\Connector\Step\TaskletInterface; | ||
|
||
class ValidateOpenAiKeyTasklet implements TaskletInterface | ||
{ | ||
private StepExecution $stepExecution; | ||
|
||
public function execute(): void | ||
{ | ||
if (empty($this->stepExecution->getJobParameters()->get('open_ai_key'))) { | ||
$this->stepExecution->addFailureException(new \Exception('OpenAI key is not set')); | ||
$this->stepExecution->setStatus(new BatchStatus(BatchStatus::FAILED)); | ||
} | ||
} | ||
|
||
public function setStepExecution(StepExecution $stepExecution): void | ||
{ | ||
$this->stepExecution = $stepExecution; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters