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
Push — master ( a22100...0c1d55 )
by joseph
20s queued 15s
created

AbstractBulkProcess::bulkSaveIfChunkBigEnough()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
ccs 6
cts 7
cp 0.8571
crap 2.0116
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 float
21
     */
22
    protected $secondsToPauseBetweenSaves = 0.0;
23
24
    /**
25
     * @var bool
26
     */
27
    private $gcWasEnabled;
28
29
    private $started = false;
30
    private $ended   = false;
31
32 6
    public function __construct(EntityManagerInterface $entityManager)
33
    {
34 6
        $this->entityManager = $entityManager;
35 6
        $this->gcWasEnabled  = gc_enabled();
36 6
    }
37
38
    public function __destruct()
39
    {
40
        if (true === $this->started && false === $this->ended) {
41
            if (!$this->entityManager->isOpen()) {
42
                throw new \RuntimeException('Error in ' . __METHOD__ . ': Entity Manager has been closed');
43
            }
44
            $this->endBulkProcess();
45
        }
46
    }
47
48 6
    public function endBulkProcess(): void
49
    {
50 6
        $this->started = false;
51 6
        $this->ended   = true;
52
53 6
        if ([] !== $this->entitiesToSave) {
54 3
            $this->doSave();
55 2
            $this->freeResources();
56
        }
57 5
        if (false === $this->gcWasEnabled) {
58 1
            return;
59
        }
60 4
        gc_enable();
61 4
    }
62
63
    abstract protected function doSave(): void;
64
65 5
    protected function freeResources()
66
    {
67 5
        gc_enable();
68 5
        foreach ($this->entitiesToSave as $entity) {
69 5
            $this->entityManager->detach($entity);
70
        }
71 5
        $this->entitiesToSave = [];
72 5
        gc_collect_cycles();
73 5
        gc_disable();
74 5
    }
75
76 1
    public function addEntityToSave(EntityInterface $entity)
77
    {
78 1
        if (false === $this->started) {
79 1
            $this->startBulkProcess();
80
        }
81 1
        $this->entitiesToSave[] = $entity;
82 1
        $this->bulkSaveIfChunkBigEnough();
83 1
    }
84
85 3
    public function startBulkProcess(): self
86
    {
87 3
        gc_disable();
88 3
        $this->started = true;
89 3
        $this->ended   = false;
90
91 3
        return $this;
92
    }
93
94 6
    protected function bulkSaveIfChunkBigEnough()
95
    {
96 6
        $size = count($this->entitiesToSave);
97 6
        if ($size >= $this->chunkSize) {
98 4
            $this->doSave();
99 3
            $this->freeResources();
100 3
            $this->pauseBetweenSaves();
101
102
        }
103 6
    }
104
105
    /**
106
     * If configured, we will pause between starting another round of saves
107
     */
108 3
    private function pauseBetweenSaves(): void
109
    {
110 3
        if (0 >= $this->secondsToPauseBetweenSaves) {
111 3
            return;
112
        }
113
        usleep((int)$this->secondsToPauseBetweenSaves * 1000000);
114
    }
115
116
    /**
117
     * This will prevent any notification on changed properties
118
     *
119
     * @param array|EntityInterface[] $entities
120
     *
121
     * @return $this
122
     */
123 3
    public function prepareEntitiesForBulkUpdate(array $entities)
124
    {
125 3
        foreach ($entities as $entity) {
126 3
            $entity->removePropertyChangedListeners();
127
        }
128
129 3
        return $this;
130
    }
131
132 5
    public function addEntitiesToSave(array $entities)
133
    {
134 5
        $entitiesToSaveBackup = $this->entitiesToSave;
135 5
        $chunks               = array_chunk($entities, $this->chunkSize, true);
136 5
        foreach ($chunks as $chunk) {
137 5
            $this->entitiesToSave = $chunk;
138 5
            $this->bulkSaveIfChunkBigEnough();
139
        }
140 5
        $this->entitiesToSave = array_merge($this->entitiesToSave, $entitiesToSaveBackup);
141 5
        $this->bulkSaveIfChunkBigEnough();
142 5
    }
143
144
    /**
145
     * @return int
146
     */
147
    public function getChunkSize(): int
148
    {
149
        return $this->chunkSize;
150
    }
151
152
    /**
153
     * @param int $chunkSize
154
     *
155
     * @return $this
156
     */
157 4
    public function setChunkSize(int $chunkSize): self
158
    {
159 4
        $this->chunkSize = $chunkSize;
160
161 4
        return $this;
162
    }
163
164
    /**
165
     * @param float $secondsToPauseBetweenSaves
166
     */
167
    public function setSecondsToPauseBetweenSaves(float $secondsToPauseBetweenSaves): void
168
    {
169
        $this->secondsToPauseBetweenSaves = $secondsToPauseBetweenSaves;
170
    }
171
}
172