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 ( 75bdf9...8faa57 )
by joseph
83:56 queued 81:04
created

AbstractBulkProcess::freeResources()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

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