GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#137)
by joseph
60:53 queued 35:20
created

BulkEntitySaver   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 115
ccs 0
cts 56
cp 0
rs 10
c 0
b 0
f 0
wmc 19

10 Methods

Rating   Name   Duplication   Size   Complexity  
A freeResources() 0 9 2
A startBulkProcess() 0 7 1
A endBulkProcess() 0 11 3
A setChunkSize() 0 5 1
A bulkSaveIfChunkBigEnough() 0 7 2
A addEntityToSave() 0 7 2
A getChunkSize() 0 3 1
A doSave() 0 8 2
A __construct() 0 4 1
A __destruct() 0 7 4
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Savers;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
7
8
class BulkEntitySaver
9
{
10
    /**
11
     * @var EntityManagerInterface
12
     */
13
    protected $entityManager;
14
15
    private $entitiesToSave = [];
16
17
    private $chunkSize = 1000;
18
19
    /**
20
     * @var bool
21
     */
22
    private $gcWasEnabled;
23
24
    private $started = false;
25
    private $ended   = false;
26
27
    public function __construct(EntityManagerInterface $entityManager)
28
    {
29
        $this->entityManager = $entityManager;
30
        $this->gcWasEnabled  = gc_enabled();
31
    }
32
33
    public function __destruct()
34
    {
35
        if (true === $this->started && false === $this->ended) {
36
            if (!$this->entityManager->isOpen()) {
37
                throw new \RuntimeException('Error in ' . __METHOD__ . ': Entity Manager has been closed');
38
            }
39
            $this->endBulkProcess();
40
        }
41
    }
42
43
    public function endBulkProcess(): void
44
    {
45
        $this->started = false;
46
        $this->ended   = true;
47
        if ([] !== $this->entitiesToSave) {
48
            $this->doSave();
49
        }
50
        if (false === $this->gcWasEnabled) {
51
            return;
52
        }
53
        gc_enable();
54
    }
55
56
    private function doSave()
57
    {
58
        foreach ($this->entitiesToSave as $entity) {
59
            $this->entityManager->persist($entity);
60
        }
61
62
        $this->entityManager->flush();
63
        $this->freeResources();
64
    }
65
66
    private function freeResources()
67
    {
68
        gc_enable();
69
        foreach ($this->entitiesToSave as $entity) {
70
            $this->entityManager->detach($entity);
71
        }
72
        $this->entitiesToSave = [];
73
        gc_collect_cycles();
74
        gc_disable();
75
    }
76
77
    public function addEntityToSave(EntityInterface $entity)
78
    {
79
        if (false === $this->started) {
80
            $this->startBulkProcess();
81
        }
82
        $this->entitiesToSave[] = $entity;
83
        $this->bulkSaveIfChunkBigEnough();
84
    }
85
86
    public function startBulkProcess(): self
87
    {
88
        gc_disable();
89
        $this->started = true;
90
        $this->ended   = false;
91
92
        return $this;
93
    }
94
95
    private function bulkSaveIfChunkBigEnough()
96
    {
97
        end($this->entitiesToSave);
98
        $key  = key($this->entitiesToSave);
99
        $size = $key + 1;
100
        if (($size % $this->chunkSize) === 0) {
101
            $this->doSave();
102
        }
103
    }
104
105
    /**
106
     * @return int
107
     */
108
    public function getChunkSize(): int
109
    {
110
        return $this->chunkSize;
111
    }
112
113
    /**
114
     * @param int $chunkSize
115
     *
116
     * @return BulkEntitySaver
117
     */
118
    public function setChunkSize(int $chunkSize): BulkEntitySaver
119
    {
120
        $this->chunkSize = $chunkSize;
121
122
        return $this;
123
    }
124
}
125