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 (#190)
by joseph
21:51
created

AbstractBulkProcess::__destruct()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 4
nc 3
nop 0
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 20
rs 10
c 0
b 0
f 0
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
abstract class AbstractBulkProcess
9
{
10
    /**
11
     * @var EntityManagerInterface
12
     */
13
    protected $entityManager;
14
15
    protected $entitiesToSave = [];
16
17
    protected $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
48
        if ([] !== $this->entitiesToSave) {
49
            $this->doSave();
50
            $this->freeResources();
51
        }
52
        if (false === $this->gcWasEnabled) {
53
            return;
54
        }
55
        gc_enable();
56
    }
57
58
    abstract protected function doSave(): void;
59
60
    protected function freeResources()
61
    {
62
        gc_enable();
63
        foreach ($this->entitiesToSave as $entity) {
64
            $this->entityManager->detach($entity);
65
        }
66
        $this->entitiesToSave = [];
67
        gc_collect_cycles();
68
        gc_disable();
69
    }
70
71
    public function addEntityToSave(EntityInterface $entity)
72
    {
73
        if (false === $this->started) {
74
            $this->startBulkProcess();
75
        }
76
        $this->entitiesToSave[] = $entity;
77
        $this->bulkSaveIfChunkBigEnough();
78
    }
79
80
    public function startBulkProcess(): self
81
    {
82
        gc_disable();
83
        $this->started = true;
84
        $this->ended   = false;
85
86
        return $this;
87
    }
88
89
    protected function bulkSaveIfChunkBigEnough()
90
    {
91
        $size = count($this->entitiesToSave);
92
        if ($size >= $this->chunkSize) {
93
            $this->doSave();
94
            $this->freeResources();
95
        }
96
    }
97
98
    /**
99
     * This will prevent any notification on changed properties
100
     *
101
     * @param array|EntityInterface[] $entities
102
     *
103
     * @return $this
104
     */
105
    public function prepareEntitiesForBulkUpdate(array $entities)
106
    {
107
        foreach ($entities as $entity) {
108
            $entity->removePropertyChangedListeners();
109
        }
110
111
        return $this;
112
    }
113
114
    public function addEntitiesToSave(array $entities)
115
    {
116
        $entitiesToSaveBackup = $this->entitiesToSave;
117
        $chunks               = array_chunk($entities, $this->chunkSize, true);
118
        foreach ($chunks as $chunk) {
119
            $this->entitiesToSave = $chunk;
120
            $this->bulkSaveIfChunkBigEnough();
121
        }
122
        $this->entitiesToSave = array_merge($this->entitiesToSave, $entitiesToSaveBackup);
123
        $this->bulkSaveIfChunkBigEnough();
124
    }
125
126
    /**
127
     * @return int
128
     */
129
    public function getChunkSize(): int
130
    {
131
        return $this->chunkSize;
132
    }
133
134
    /**
135
     * @param int $chunkSize
136
     *
137
     * @return $this
138
     */
139
    public function setChunkSize(int $chunkSize): self
140
    {
141
        $this->chunkSize = $chunkSize;
142
143
        return $this;
144
    }
145
}
146