ChunkingStrategy::chunk()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Malef\Associate\DoctrineOrm\Loader\ChunkingStrategy;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
7
class ChunkingStrategy
8
{
9
    const DEFAULT_CHUNK_SIZE = 1000;
10
11
    /**
12
     * @var int
13
     */
14
    protected $chunkSize;
15
16
    public function __construct(int $chunkSize = self::DEFAULT_CHUNK_SIZE)
17
    {
18
        $this->chunkSize = $chunkSize;
19
    }
20
21
    /**
22
     * @return ArrayCollection[]
23
     */
24
    public function chunk(ArrayCollection $entities): array
25
    {
26
        $chunks = [];
27
        $entitiesCount = count($entities);
28
        for ($offset = 0; $offset < $entitiesCount; $offset += $this->chunkSize) {
29
            $chunks[] = new ArrayCollection($entities->slice($offset, $this->chunkSize));
30
        }
31
32
        return $chunks;
33
    }
34
}
35