Skip to content

Commit a35fb81

Browse files
add ChunkGeneratorTrait
1 parent c814aa2 commit a35fb81

3 files changed

Lines changed: 38 additions & 22 deletions

File tree

src/Element/Service/ExecutionEngine/ElementReferenceService.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace Pimcore\Bundle\StudioBackendBundle\Element\Service\ExecutionEngine;
1515

1616
use Exception;
17-
use Generator;
1817
use Pimcore\Bundle\GenericExecutionEngineBundle\Agent\JobExecutionAgentInterface;
1918
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\Job;
2019
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\JobStep;
@@ -28,6 +27,7 @@
2827
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
2928
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs;
3029
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
30+
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\ChunkGeneratorTrait;
3131
use Pimcore\Model\Asset;
3232
use Pimcore\Model\DataObject\AbstractObject;
3333
use Pimcore\Model\Document;
@@ -39,6 +39,8 @@
3939
*/
4040
final readonly class ElementReferenceService implements ElementReferenceServiceInterface
4141
{
42+
use ChunkGeneratorTrait;
43+
4244
private const int REWRITE_REFERENCES_BATCH_SIZE = 500;
4345

4446
public function __construct(
@@ -162,14 +164,4 @@ private function rewriteDataObjectReferences(
162164
$object->setUserModification($user->getId());
163165
$object->save();
164166
}
165-
166-
// TODO: replace with ChunkGeneratorTrait once https://github.com/pimcore/studio-backend-bundle/pull/1800 is merged
167-
private function chunkGenerator(array $items, int $size): Generator
168-
{
169-
$total = count($items);
170-
171-
for ($i = 0; $i < $total; $i += $size) {
172-
yield array_slice($items, $i, $size);
173-
}
174-
}
175167
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* This source file is available under the terms of the
6+
* Pimcore Open Core License (POCL)
7+
* Full copyright and license information is available in
8+
* LICENSE.md which is distributed with this source code.
9+
*
10+
* @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com)
11+
* @license Pimcore Open Core License (POCL)
12+
*/
13+
14+
namespace Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait;
15+
16+
use Generator;
17+
use function array_slice;
18+
use function count;
19+
20+
/**
21+
* @internal
22+
*/
23+
trait ChunkGeneratorTrait
24+
{
25+
private function chunkGenerator(array $items, int $size): Generator
26+
{
27+
$total = count($items);
28+
29+
for ($i = 0; $i < $total; $i += $size) {
30+
yield array_slice($items, $i, $size);
31+
}
32+
}
33+
}

src/Tag/Service/ExecutionEngine/BatchService.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Pimcore\Bundle\StudioBackendBundle\Tag\Service\ExecutionEngine;
1515

16-
use Generator;
1716
use Pimcore\Bundle\GenericExecutionEngineBundle\Agent\JobExecutionAgentInterface;
1817
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\Job;
1918
use Pimcore\Bundle\GenericExecutionEngineBundle\Model\JobStep;
@@ -27,6 +26,7 @@
2726
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\EnvironmentVariables;
2827
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Jobs;
2928
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\StepConfig;
29+
use Pimcore\Bundle\StudioBackendBundle\ExecutionEngine\Util\Trait\ChunkGeneratorTrait;
3030
use Pimcore\Bundle\StudioBackendBundle\Security\Service\SecurityServiceInterface;
3131
use Pimcore\Bundle\StudioBackendBundle\Tag\ExecutionEngine\AutomationAction\Messenger\Messages\BatchTagOperationMessage;
3232
use Pimcore\Bundle\StudioBackendBundle\Tag\MappedParameter\BatchOperationParameters;
@@ -42,6 +42,7 @@
4242
final readonly class BatchService implements BatchServiceInterface
4343
{
4444
use ElementProviderTrait;
45+
use ChunkGeneratorTrait;
4546

4647
private const int BATCH_TAG_SIZE = 500;
4748

@@ -115,14 +116,4 @@ private function getJobStepName(string $operation): string
115116
JobSteps::ELEMENT_BATCH_TAG_ASSIGN->value :
116117
JobSteps::ELEMENT_BATCH_TAG_REPLACE->value;
117118
}
118-
119-
// TODO: replace with ChunkGeneratorTrait once https://github.com/pimcore/studio-backend-bundle/pull/1800 is merged
120-
private function chunkGenerator(array $items, int $size): Generator
121-
{
122-
$total = count($items);
123-
124-
for ($i = 0; $i < $total; $i += $size) {
125-
yield array_slice($items, $i, $size);
126-
}
127-
}
128119
}

0 commit comments

Comments
 (0)