Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<int> $ids
*
* @return void
* @return \Iterator<array<\Propel\Runtime\ActiveRecord\ActiveRecordInterface>>
*/
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<array<int>>
*/
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<int> $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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,25 +16,31 @@ class EventResourceQueryContainerPluginIterator extends AbstractEventResourcePlu
*/
protected $plugin;

/**
* @var array<int>
*/
protected $ids;

/**
* @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceQueryContainerPluginInterface $plugin
* @param int $chunkSize
* @param array<int> $ids
*/
public function __construct(EventResourceQueryContainerPluginInterface $plugin, int $chunkSize)
public function __construct(EventResourceQueryContainerPluginInterface $plugin, int $chunkSize, $ids = [])
{
parent::__construct($plugin, $chunkSize);

$this->ids = $ids;
}

/**
* @return void
*/
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ class EventResourceRepositoryBulkPluginIterator extends AbstractEventResourcePlu
*/
protected $plugin;

/**
* @var array<int>
*/
protected $ids;

/**
* @param \Spryker\Zed\EventBehavior\Dependency\Plugin\EventResourceBulkRepositoryPluginInterface $plugin
* @param int $chunkSize
* @param array<int> $ids
*/
public function __construct(EventResourceBulkRepositoryPluginInterface $plugin, int $chunkSize)
public function __construct(EventResourceBulkRepositoryPluginInterface $plugin, int $chunkSize, array $ids)
{
parent::__construct($plugin, $chunkSize);
$this->ids = $ids;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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>>
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
*/
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Are you only using methods from the abstract one here?

*
* @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;
}
}
Loading