Failed Conditions
Push — master ( fa7802...d60694 )
by Guilherme
09:27
created

SequenceGenerator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 63.33%

Importance

Changes 0
Metric Value
eloc 23
c 0
b 0
f 0
dl 0
loc 99
ccs 19
cts 30
cp 0.6333
rs 10
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A serialize() 0 6 1
A getSequenceName() 0 3 1
A getAllocationSize() 0 3 1
A generate() 0 13 3
A getNextValue() 0 3 1
A getCurrentMaxValue() 0 3 1
A isPostInsertGenerator() 0 3 1
A unserialize() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Sequencing\Generator;
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
    /** @var int */
32
    private $nextValue = 0;
33
34
    /** @var int|null */
35
    private $maxValue;
36
37 23
    public function __construct(string $sequenceName, int $allocationSize)
38
    {
39 23
        $this->sequenceName   = $sequenceName;
40 23
        $this->allocationSize = $allocationSize;
41 23
    }
42
43 5
    public function getSequenceName() : string
44
    {
45 5
        return $this->sequenceName;
46
    }
47
48 5
    public function getAllocationSize() : int
49
    {
50 5
        return $this->allocationSize;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function generate(EntityManagerInterface $em, ?object $entity)
57
    {
58 1
        if ($this->maxValue === null || $this->nextValue === $this->maxValue) {
59
            // Allocate new values
60 1
            $conn = $em->getConnection();
61 1
            $sql  = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->sequenceName);
62
63
            // Using `query` to force usage of the master server in MasterSlaveConnection
64 1
            $this->nextValue = (int) $conn->query($sql)->fetchColumn();
65 1
            $this->maxValue  = $this->nextValue + $this->allocationSize;
66
        }
67
68 1
        return $this->nextValue++;
69
    }
70
71
    /**
72
     * Gets the maximum value of the currently allocated bag of values.
73
     */
74 1
    public function getCurrentMaxValue() : ?int
75
    {
76 1
        return $this->maxValue;
77
    }
78
79
    /**
80
     * Gets the next value that will be returned by generate().
81
     */
82 1
    public function getNextValue() : int
83
    {
84 1
        return $this->nextValue;
85
    }
86
87
    public function serialize() : string
88
    {
89
        return serialize(
90
            [
91
                'allocationSize' => $this->allocationSize,
92
                'sequenceName'   => $this->sequenceName,
93
            ]
94
        );
95
    }
96
97
    /**
98
     * @param string $serialized
99
     */
100
    public function unserialize($serialized) : void
101
    {
102
        $array = unserialize($serialized);
103
104
        $this->sequenceName   = $array['sequenceName'];
105
        $this->allocationSize = $array['allocationSize'];
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function isPostInsertGenerator() : bool
112
    {
113
        return false;
114
    }
115
}
116