-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
184 additions
and
57 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
databox/api/src/Api/Model/Output/ESDocumentStateOutput.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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Api\Model\Output; | ||
|
||
use Symfony\Component\Serializer\Annotation\Groups; | ||
|
||
final readonly class ESDocumentStateOutput | ||
{ | ||
public function __construct( | ||
#[Groups(['_'])] | ||
private array $data, | ||
#[Groups(['_'])] | ||
private bool $synced, | ||
) | ||
{ | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
public function getSynced(): bool | ||
{ | ||
return $this->synced; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
databox/api/src/Api/Processor/AssetElasticsearchDocumentSyncProcessor.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,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Api\Processor; | ||
|
||
use Alchemy\AuthBundle\Security\JwtUser; | ||
use Alchemy\AuthBundle\Security\Traits\SecurityAwareTrait; | ||
use Alchemy\ESBundle\Listener\DeferredIndexListener; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProcessorInterface; | ||
use App\Entity\Core\Asset; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class AssetElasticsearchDocumentSyncProcessor implements ProcessorInterface | ||
{ | ||
use SecurityAwareTrait; | ||
|
||
public function __construct( | ||
private readonly DeferredIndexListener $deferredIndexListener, | ||
) { | ||
} | ||
|
||
/** | ||
* @param Asset $data | ||
*/ | ||
public function process($data, Operation $operation, array $uriVariables = [], array $context = []): Response | ||
{ | ||
$this->denyAccessUnlessGranted(JwtUser::ROLE_TECH); | ||
$this->deferredIndexListener->scheduleForUpdate($data); | ||
|
||
return new Response('', 201); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Elasticsearch; | ||
|
||
use Alchemy\CoreBundle\Entity\AbstractUuidEntity; | ||
use App\Api\Model\Output\ESDocumentStateOutput; | ||
use Elastica\Request; | ||
use FOS\ElasticaBundle\Persister\ObjectPersister; | ||
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface; | ||
use RuntimeException; | ||
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; | ||
|
||
final readonly class ESDocumentStateManager | ||
{ | ||
/** | ||
* @param ObjectPersisterInterface[] $objectPersisters | ||
*/ | ||
public function __construct( | ||
#[TaggedIterator(tag: 'fos_elastica.persister')] | ||
private iterable $objectPersisters, | ||
private ElasticSearchClient $elasticSearchClient, | ||
|
||
) { | ||
} | ||
|
||
public function getObjectState(AbstractUuidEntity $object): ESDocumentStateOutput | ||
{ | ||
$document = $this->getObjectPersister($object)->transformToElasticaDocument($object); | ||
$indexName = $this->elasticSearchClient->getIndexName($document->getIndex()); | ||
$response = $this->elasticSearchClient->request($indexName.'/_doc/'.$object->getId(), [], Request::GET); | ||
|
||
$data = $response->getData(); | ||
$synced = $document->getData() == $data['_source']; | ||
|
||
return new ESDocumentStateOutput($data, $synced); | ||
} | ||
|
||
private function getObjectPersister(object $object): ObjectPersister | ||
{ | ||
foreach ($this->objectPersisters as $objectPersister) { | ||
if ($objectPersister instanceof ObjectPersister && $objectPersister->handlesObject($object)) { | ||
return $objectPersister; | ||
} | ||
} | ||
|
||
throw new RuntimeException(sprintf('No object persister found for object of class %s', get_class($object))); | ||
} | ||
} |
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
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