Failed Conditions
CANCELLED  
Pull Request — master (#7095)
by Benjamin
10:13
created

SequenceGenerator::isPostInsertGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Sequencing;
6
7
use Doctrine\ORM\EntityManagerInterface;
8
use Serializable;
9
use function serialize;
10
use function unserialize;
11
12
/**
13
 * Represents an ID generator that uses a database sequence.
14
 */
15
class SequenceGenerator implements Generator, Serializable
16
{
17
    /**
18
     * The allocation size of the sequence.
19
     *
20
     * @var int
21
     */
22
    private $allocationSize;
23
24
    /**
25
     * The name of the sequence.
26
     *
27
     * @var string
28
     */
29
    private $sequenceName;
30
31
    /**
32
     * @var int
33
     */
34
    private $nextValue = 0;
35
36
    /**
37
     * @var int|null
38
     */
39
    private $maxValue;
40
41
    public function __construct(string $sequenceName, int $allocationSize)
42
    {
43
        $this->sequenceName   = $sequenceName;
44
        $this->allocationSize = $allocationSize;
45
    }
46
47 7
    /**
48
     * {@inheritdoc}
49 7
     */
50 7
    public function generate(EntityManagerInterface $em, ?object $entity)
51 7
    {
52
        if ($this->maxValue === null || $this->nextValue === $this->maxValue) {
53
            // Allocate new values
54
            $conn = $em->getConnection();
55
            $sql  = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->sequenceName);
56 1
57
            // Using `query` to force usage of the master server in MasterSlaveConnection
58 1
            $this->nextValue = (int) $conn->query($sql)->fetchColumn();
59
            $this->maxValue  = $this->nextValue + $this->allocationSize;
60 1
        }
61 1
62
        return $this->nextValue++;
63
    }
64 1
65 1
    /**
66
     * Gets the maximum value of the currently allocated bag of values.
67
     */
68 1
    public function getCurrentMaxValue() : ?int
69
    {
70
        return $this->maxValue;
71
    }
72
73
    /**
74
     * Gets the next value that will be returned by generate().
75
     */
76 1
    public function getNextValue() : int
77
    {
78 1
        return $this->nextValue;
79
    }
80
81
    public function serialize() : string
82
    {
83
        return serialize(
84
            [
85
                'allocationSize' => $this->allocationSize,
86 1
                'sequenceName'   => $this->sequenceName,
87
            ]
88 1
        );
89
    }
90
91
    /**
92
     * @param string $serialized
93
     */
94
    public function unserialize($serialized) : void
95
    {
96
        $array = unserialize($serialized);
97
98
        $this->sequenceName   = $array['sequenceName'];
99
        $this->allocationSize = $array['allocationSize'];
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function isPostInsertGenerator() : bool
106
    {
107
        return false;
108
    }
109
}
110