diff --git a/phpstan.neon b/phpstan.neon index 48a64dab..06877abb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -15,5 +15,6 @@ parameters: - '#Cannot call method offset\(\) on .+ModelCriteria\|null#' - '#.+ has invalid return type Orm\\.+#' - '#.+ has invalid type Orm\\.+#' + - '#Call to an undefined method .+ActiveRecordInterface::.+#' bootstrapFiles: - tests/bootstrap.php diff --git a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceQueryContainerManager.php b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceQueryContainerManager.php index dc4302ab..4b03dce1 100644 --- a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceQueryContainerManager.php +++ b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceQueryContainerManager.php @@ -9,6 +9,8 @@ use Generated\Shared\Transfer\EventEntityTransfer; use Iterator; +use Propel\Runtime\ActiveRecord\ActiveRecordInterface; +use ReflectionClass; use Spryker\Zed\EventBehavior\Dependency\Facade\EventBehaviorToEventInterface; use Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceQueryContainerPluginInterface; @@ -62,64 +64,48 @@ public function processResourceEvents(array $plugins, array $ids = []): void */ protected function triggerEvents(EventResourceQueryContainerPluginInterface $plugin, array $ids = []): void { - if ($ids) { - $this->triggerBulk($plugin, $ids); - - return; + foreach ($this->createEventResourceQueryContainerPluginIterator($plugin, $ids) as $entities) { + $this->triggerBulk($plugin, $entities); } - - if ($plugin->queryData($ids) === null) { - $this->triggerEventWithEmptyId($plugin); - - return; - } - - $this->processEventsByPluginItreator($plugin); } /** * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceQueryContainerPluginInterface $plugin + * @param array $ids * - * @return void + * @return \Iterator> */ - protected function triggerEventWithEmptyId(EventResourceQueryContainerPluginInterface $plugin): void + protected function createEventResourceQueryContainerPluginIterator(EventResourceQueryContainerPluginInterface $plugin, $ids = []): Iterator { - $this->eventFacade->trigger($plugin->getEventName(), new EventEntityTransfer()); + return new EventResourceQueryContainerPluginIterator($plugin, $this->chunkSize, $ids); } /** * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceQueryContainerPluginInterface $plugin + * @param array<\Propel\Runtime\ActiveRecord\ActiveRecordInterface> $entities * * @return void */ - protected function processEventsByPluginItreator(EventResourceQueryContainerPluginInterface $plugin): void + protected function triggerBulk(EventResourceQueryContainerPluginInterface $plugin, array $entities): void { - foreach ($this->createEventResourceQueryContainerPluginIterator($plugin) as $ids) { - $this->triggerBulk($plugin, $ids); + if (!$entities) { + return; } - } - /** - * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceQueryContainerPluginInterface $plugin - * - * @return \Iterator> - */ - protected function createEventResourceQueryContainerPluginIterator(EventResourceQueryContainerPluginInterface $plugin): Iterator - { - return new EventResourceQueryContainerPluginIterator($plugin, $this->chunkSize); - } + $reflactionEntity = new ReflectionClass(current($entities)); - /** - * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceQueryContainerPluginInterface $plugin - * @param array $ids - * - * @return void - */ - protected function triggerBulk(EventResourceQueryContainerPluginInterface $plugin, array $ids): void - { - $eventEntityTransfers = array_map(function ($id) { - return (new EventEntityTransfer())->setId($id); - }, $ids); + $protectForeignKeysMethod = $reflactionEntity->getMethod('getForeignKeys'); + $protectForeignKeysMethod->setAccessible(true); + $protectAdditionalValuesMethod = $reflactionEntity->getMethod('getAdditionalValues'); + $protectAdditionalValuesMethod->setAccessible(true); + + $eventEntityTransfers = array_map(function (ActiveRecordInterface $entity) use ($plugin, $protectForeignKeysMethod, $protectAdditionalValuesMethod) { + return (new EventEntityTransfer()) + ->setId($entity->getPrimaryKey()) + ->setEvent($plugin->getEventName()) + ->setForeignKeys($protectForeignKeysMethod->invokeArgs($entity, [])) + ->setAdditionalValues($protectAdditionalValuesMethod->invokeArgs($entity, [])); + }, $entities); $this->eventFacade->triggerBulk($plugin->getEventName(), $eventEntityTransfers); } diff --git a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceQueryContainerPluginIterator.php b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceQueryContainerPluginIterator.php index 5e7e77a5..5639b8cb 100644 --- a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceQueryContainerPluginIterator.php +++ b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceQueryContainerPluginIterator.php @@ -7,7 +7,6 @@ namespace Spryker\Zed\EventBehavior\Business\Model; -use Propel\Runtime\ActiveQuery\ModelCriteria; use Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceQueryContainerPluginInterface; class EventResourceQueryContainerPluginIterator extends AbstractEventResourcePluginIterator @@ -17,13 +16,21 @@ class EventResourceQueryContainerPluginIterator extends AbstractEventResourcePlu */ protected $plugin; + /** + * @var array + */ + protected $ids; + /** * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceQueryContainerPluginInterface $plugin * @param int $chunkSize + * @param array $ids */ - public function __construct(EventResourceQueryContainerPluginInterface $plugin, int $chunkSize) + public function __construct(EventResourceQueryContainerPluginInterface $plugin, int $chunkSize, $ids = []) { parent::__construct($plugin, $chunkSize); + + $this->ids = $ids; } /** @@ -31,11 +38,9 @@ public function __construct(EventResourceQueryContainerPluginInterface $plugin, */ protected function updateCurrent(): void { - $this->current = $this->plugin->queryData() + $this->current = $this->plugin->queryData($this->ids) ->offset($this->offset) ->limit($this->chunkSize) - ->where($this->plugin->getIdColumnName() . ModelCriteria::ISNOTNULL) - ->select([$this->plugin->getIdColumnName()]) ->orderBy((string)$this->plugin->getIdColumnName()) ->find() ->getData(); diff --git a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryBulkPluginIterator.php b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryBulkPluginIterator.php index a575e0ba..d59e097c 100644 --- a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryBulkPluginIterator.php +++ b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryBulkPluginIterator.php @@ -16,13 +16,20 @@ class EventResourceRepositoryBulkPluginIterator extends AbstractEventResourcePlu */ protected $plugin; + /** + * @var array + */ + protected $ids; + /** * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceBulkRepositoryPluginInterface $plugin * @param int $chunkSize + * @param array $ids */ - public function __construct(EventResourceBulkRepositoryPluginInterface $plugin, int $chunkSize) + public function __construct(EventResourceBulkRepositoryPluginInterface $plugin, int $chunkSize, array $ids) { parent::__construct($plugin, $chunkSize); + $this->ids = $ids; } /** diff --git a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryManager.php b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryManager.php index c8bf42ca..0eedd8b4 100644 --- a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryManager.php +++ b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryManager.php @@ -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 $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 $ids * - * @return \Iterator> + * @return \Iterator> */ - 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 $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 $ids * - * @return \Iterator> + * @return \Iterator> */ - 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 */ @@ -183,13 +200,67 @@ 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 + * + * @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 $additionalValuesMapping + * @param array $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 $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); @@ -197,4 +268,21 @@ protected function triggerBulk(EventResourcePluginInterface $plugin, array $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; + } } diff --git a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryPluginIterator.php b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryPluginIterator.php index 183c49b6..f5ab98ce 100644 --- a/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryPluginIterator.php +++ b/src/Spryker/Zed/EventBehavior/Business/Model/EventResourceRepositoryPluginIterator.php @@ -24,12 +24,13 @@ class EventResourceRepositoryPluginIterator extends AbstractEventResourcePluginI /** * @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceRepositoryPluginInterface $plugin * @param int $chunkSize + * @param array $ids */ - public function __construct(EventResourceRepositoryPluginInterface $plugin, int $chunkSize) + public function __construct(EventResourceRepositoryPluginInterface $plugin, int $chunkSize, array $ids = []) { parent::__construct($plugin, $chunkSize); - $this->data = $plugin->getData(); + $this->data = $plugin->getData($ids); } /** diff --git a/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceAdditionalValuesRepositoryExtensionPluginInterface.php b/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceAdditionalValuesRepositoryExtensionPluginInterface.php new file mode 100644 index 00000000..68f67e8a --- /dev/null +++ b/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceAdditionalValuesRepositoryExtensionPluginInterface.php @@ -0,0 +1,22 @@ + 'transfer_property_name'] + * + * @api + * + * @return array + */ + public function getAdditionalValuesMapping(): array; +} diff --git a/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceBulkRepositoryPluginInterface.php b/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceBulkRepositoryPluginInterface.php index a65a6944..1b4a2659 100644 --- a/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceBulkRepositoryPluginInterface.php +++ b/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceBulkRepositoryPluginInterface.php @@ -17,8 +17,9 @@ interface EventResourceBulkRepositoryPluginInterface extends EventResourcePlugin * * @param int $offset * @param int $limit + * @param array $ids * * @return array<\Spryker\Shared\Kernel\Transfer\AbstractTransfer> */ - public function getData(int $offset, int $limit): array; + public function getData(int $offset, int $limit, array $ids = []): array; } diff --git a/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceForeignKeysRepositoryExtensionPluginInterface.php b/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceForeignKeysRepositoryExtensionPluginInterface.php new file mode 100644 index 00000000..d54d1c6e --- /dev/null +++ b/src/Spryker/Zed/EventBehavior/Dependency/Plugin/EventResourceForeignKeysRepositoryExtensionPluginInterface.php @@ -0,0 +1,22 @@ + 'transfer_property_name'] + * + * @api + * + * @return array + */ + public function getForeignKeysMapping(): array; +}