-
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
085a68b
commit 1993328
Showing
11 changed files
with
229 additions
and
23 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...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,52 @@ | ||
<?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') | ||
] | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(JobInterface $job): bool | ||
{ | ||
return in_array($job->getName(), $this->supportedJobNames); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...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,41 @@ | ||
<?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 | ||
) { | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultValues(): array | ||
{ | ||
return [ | ||
'filters' => [], | ||
'actions' => [], | ||
'realTimeVersioning' => true, | ||
'users_to_notify' => [], | ||
'is_user_authenticated' => false | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(JobInterface $job): bool | ||
{ | ||
return in_array($job->getName(), $this->supportedJobNames); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
<?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; | ||
use Exception; | ||
|
||
class ValidateOpenAiKeyTasklet implements TaskletInterface | ||
{ | ||
private StepExecution $stepExecution; | ||
|
||
public function __construct( | ||
private ?string $openAiKey | ||
) { | ||
} | ||
|
||
public function execute(): void | ||
{ | ||
if (empty($this->openAiKey)) { | ||
$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
20 changes: 20 additions & 0 deletions
20
src/Macopedia/OpenAiTranslator/Repository/AttributeRepository.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,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Macopedia\OpenAiTranslator\Repository; | ||
|
||
use Akeneo\Pim\Structure\Bundle\Doctrine\ORM\Repository\AttributeRepository as BaseAttributeRepository; | ||
|
||
class AttributeRepository extends BaseAttributeRepository | ||
{ | ||
public function getAttributesByCodes(array $codes): array | ||
{ | ||
return $this->_em->createQueryBuilder() | ||
->select('att') | ||
->from($this->_entityName, 'att', 'att.code') | ||
->where('att.code IN (:codes)')->setParameter('codes', $codes) | ||
->getQuery() | ||
->getResult(); | ||
} | ||
} |
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
6 changes: 6 additions & 0 deletions
6
src/Macopedia/OpenAiTranslator/Resources/config/repositories.yml
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,6 @@ | ||
services: | ||
Macopedia\OpenAiTranslator\Repository\AttributeRepository: | ||
factory: [ '@doctrine.orm.entity_manager', 'getRepository' ] | ||
arguments: [ '%pim_catalog.entity.attribute.class%' ] | ||
tags: | ||
- { name: 'pim_repository' } |
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
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
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