-
Notifications
You must be signed in to change notification settings - Fork 1
TE-4189: Fixed EventTransfer with additional params #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f1a81cf
5d8b3db
78d8c8f
ad4a851
d2627a3
04d7b74
bdfd743
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,8 +9,11 @@ | |
|
|
||
| use Generated\Shared\Transfer\EventEntityTransfer; | ||
| use Iterator; | ||
| use Spryker\Shared\Kernel\Transfer\AbstractTransfer; | ||
| use Spryker\Zed\EventBehavior\Dependency\Facade\EventBehaviorToEventInterface; | ||
| use Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceAdditionalValuesRepositoryExtensionPluginInterface; | ||
| use Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceBulkRepositoryPluginInterface; | ||
| use Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceForeignKeysRepositoryExtensionPluginInterface; | ||
| use Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourcePluginInterface; | ||
| use Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceRepositoryPluginInterface; | ||
|
|
||
|
|
@@ -79,13 +82,13 @@ public function processResourceEvents(array $plugins, array $ids = []): void | |
| */ | ||
| protected function processEventsForRepositoryPlugins(EventResourceRepositoryPluginInterface $plugin, array $ids = []): void | ||
| { | ||
| if ($ids !== []) { | ||
| $this->triggerBulk($plugin, $ids); | ||
| if (!$ids && !$this->hasAdditionalValues($plugin)) { | ||
| $this->triggerBulkIds($plugin, $ids); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $this->processEventsForRepositoryPlugin($plugin); | ||
| $this->processEventsForRepositoryPlugin($plugin, $ids); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -96,64 +99,78 @@ protected function processEventsForRepositoryPlugins(EventResourceRepositoryPlug | |
| */ | ||
| protected function processEventsForBulkRepositoryPlugins(EventResourceBulkRepositoryPluginInterface $plugin, array $ids = []): void | ||
| { | ||
| if ($ids !== []) { | ||
| $this->triggerBulk($plugin, $ids); | ||
| if ($ids !== [] && !$this->hasAdditionalValues($plugin)) { | ||
| $this->triggerBulkIds($plugin, $ids); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $this->processEventsForRepositoryBulkPlugins($plugin); | ||
| $this->processEventsForRepositoryBulkPlugins($plugin, $ids); | ||
| } | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceRepositoryPluginInterface $plugin | ||
| * @param array<int> $ids | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function processEventsForRepositoryPlugin(EventResourceRepositoryPluginInterface $plugin): void | ||
| protected function processEventsForRepositoryPlugin(EventResourceRepositoryPluginInterface $plugin, array $ids): void | ||
| { | ||
| foreach ($this->createEventResourceRepositoryPluginIterator($plugin) as $eventEntities) { | ||
| $eventEntitiesIds = $this->getEventEntitiesIds($plugin, $eventEntities); | ||
| $this->triggerBulk($plugin, $eventEntitiesIds); | ||
| foreach ($this->createEventResourceRepositoryPluginIterator($plugin, $ids) as $eventEntities) { | ||
| if (!$this->hasAdditionalValues($plugin)) { | ||
| $eventEntitiesIds = $this->getEventEntitiesIds($plugin, $eventEntities); | ||
| $this->triggerBulkIds($plugin, $eventEntitiesIds); | ||
|
|
||
| continue; | ||
| } | ||
| $this->triggerBulk($plugin, $eventEntities); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceRepositoryPluginInterface $plugin | ||
| * @param array<int> $ids | ||
| * | ||
| * @return \Iterator<array<\Generated\Shared\Transfer\EventEntityTransfer>> | ||
| * @return \Iterator<array<\Spryker\Shared\Kernel\Transfer\AbstractEntityTransfer>> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this true? isnt the actual object still the concrete one? |
||
| */ | ||
| protected function createEventResourceRepositoryPluginIterator(EventResourceRepositoryPluginInterface $plugin): Iterator | ||
| protected function createEventResourceRepositoryPluginIterator(EventResourceRepositoryPluginInterface $plugin, array $ids): Iterator | ||
| { | ||
| return new EventResourceRepositoryPluginIterator($plugin, $this->chunkSize); | ||
| return new EventResourceRepositoryPluginIterator($plugin, $this->chunkSize, $ids); | ||
| } | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceBulkRepositoryPluginInterface $plugin | ||
| * @param array<int> $ids | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function processEventsForRepositoryBulkPlugins(EventResourceBulkRepositoryPluginInterface $plugin): void | ||
| protected function processEventsForRepositoryBulkPlugins(EventResourceBulkRepositoryPluginInterface $plugin, array $ids): void | ||
| { | ||
| foreach ($this->createEventResourceRepositoryBulkPluginIterator($plugin) as $eventEntities) { | ||
| $eventEntitiesIds = $this->getEventEntitiesIds($plugin, $eventEntities); | ||
| $this->triggerBulk($plugin, $eventEntitiesIds); | ||
| foreach ($this->createEventResourceRepositoryBulkPluginIterator($plugin, $ids) as $eventEntities) { | ||
| if (!$this->hasAdditionalValues($plugin)) { | ||
| $eventEntitiesIds = $this->getEventEntitiesIds($plugin, $eventEntities); | ||
| $this->triggerBulkIds($plugin, $eventEntitiesIds); | ||
|
|
||
| continue; | ||
| } | ||
| $this->triggerBulk($plugin, $eventEntities); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceBulkRepositoryPluginInterface $plugin | ||
| * @param array<int> $ids | ||
| * | ||
| * @return \Iterator<array<\Generated\Shared\Transfer\EventEntityTransfer>> | ||
| * @return \Iterator<array<\Spryker\Shared\Kernel\Transfer\AbstractTransfer>> | ||
| */ | ||
| protected function createEventResourceRepositoryBulkPluginIterator(EventResourceBulkRepositoryPluginInterface $plugin): Iterator | ||
| protected function createEventResourceRepositoryBulkPluginIterator(EventResourceBulkRepositoryPluginInterface $plugin, $ids): Iterator | ||
| { | ||
| return new EventResourceRepositoryBulkPluginIterator($plugin, $this->chunkSize); | ||
| return new EventResourceRepositoryBulkPluginIterator($plugin, $this->chunkSize, $ids); | ||
| } | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourcePluginInterface $plugin | ||
| * @param array<\Generated\Shared\Transfer\EventEntityTransfer> $chunkOfEventEntitiesTransfers | ||
| * @param array<\Spryker\Shared\Kernel\Transfer\AbstractTransfer> $chunkOfEventEntitiesTransfers | ||
| * | ||
| * @return array<int> | ||
| */ | ||
|
|
@@ -183,18 +200,89 @@ protected function getIdColumnName($plugin): ?string | |
| return $idColumnName[1] ?? null; | ||
| } | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourcePluginInterface $plugin | ||
| * @param array<\Spryker\Shared\Kernel\Transfer\AbstractTransfer> $transfers | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those are never "Just" the abstract one - at least those are usually invalid to be used for methods that do not exist |
||
| * | ||
| * @return void | ||
| */ | ||
| protected function triggerBulk(EventResourcePluginInterface $plugin, array $transfers): void | ||
| { | ||
| $idColumnName = $this->getIdColumnName($plugin); | ||
| $additionalValues = []; | ||
| $foreignKeys = []; | ||
|
|
||
| if ($plugin instanceof EventResourceAdditionalValuesRepositoryExtensionPluginInterface) { | ||
| $additionalValues = $plugin->getAdditionalValuesMapping(); | ||
| } | ||
| if ($plugin instanceof EventResourceForeignKeysRepositoryExtensionPluginInterface) { | ||
| $foreignKeys = $plugin->getForeignKeysMapping(); | ||
| } | ||
|
|
||
| $eventEntityTransfers = array_map(function (AbstractTransfer $transfer) use ($idColumnName, $foreignKeys, $additionalValues) { | ||
| $transferArray = $transfer->modifiedToArray(); | ||
| $transferInCamelCaseArray = $transfer->modifiedToArray(true, true); | ||
| $eventEntityTransfer = (new EventEntityTransfer()) | ||
| ->setId($transferArray[$idColumnName]); | ||
|
|
||
| $eventEntityTransfer->setForeignKeys( | ||
| $this->mapAdditionalFiles($foreignKeys, $transferInCamelCaseArray), | ||
| ); | ||
| $eventEntityTransfer->setAdditionalValues( | ||
| $this->mapAdditionalFiles($additionalValues, $transferInCamelCaseArray), | ||
| ); | ||
|
|
||
| return $eventEntityTransfer; | ||
| }, $transfers); | ||
|
|
||
| $this->eventFacade->triggerBulk($plugin->getEventName(), $eventEntityTransfers); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, string> $additionalValuesMapping | ||
| * @param array<string, mixed> $transferArray | ||
| * | ||
| * @return array | ||
| */ | ||
| protected function mapAdditionalFiles(array $additionalValuesMapping, array $transferArray): array | ||
| { | ||
| $additionalValues = []; | ||
| foreach ($additionalValuesMapping as $tableFieldName => $transferPropertyName) { | ||
| $additionalValues[$tableFieldName] = $transferArray[$transferPropertyName]; | ||
| } | ||
|
|
||
| return $additionalValues; | ||
| } | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourcePluginInterface $plugin | ||
| * @param array<int> $ids | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function triggerBulk(EventResourcePluginInterface $plugin, array $ids): void | ||
| protected function triggerBulkIds(EventResourcePluginInterface $plugin, array $ids): void | ||
| { | ||
| $eventEntityTransfers = array_map(function ($id) { | ||
| return (new EventEntityTransfer())->setId($id); | ||
| }, $ids); | ||
|
|
||
| $this->eventFacade->triggerBulk($plugin->getEventName(), $eventEntityTransfers); | ||
| } | ||
|
|
||
| /** | ||
| * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourcePluginInterface $plugin | ||
| * | ||
| * @return bool | ||
| */ | ||
| protected function hasAdditionalValues(EventResourcePluginInterface $plugin) | ||
| { | ||
| if ( | ||
| $plugin instanceof EventResourceAdditionalValuesRepositoryExtensionPluginInterface || | ||
| $plugin instanceof EventResourceForeignKeysRepositoryExtensionPluginInterface | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.