File size: 1,222 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php

declare(strict_types=1);

namespace Mautic\CoreBundle\Entity;

use Doctrine\DBAL\Types\Types;
use Mautic\CoreBundle\Doctrine\Mapping\ClassMetadataBuilder;

/**
 * This trait provides default implementation of OptimisticLockInterface.
 */
trait OptimisticLockTrait
{
    private int $version = 1;
    private ?int $currentVersion;
    private bool $incrementVersion = false;

    public function getVersion(): int
    {
        return $this->currentVersion ?? $this->version;
    }

    public function setVersion(int $version): void
    {
        $this->currentVersion   = $version;
        $this->incrementVersion = false;
    }

    public function isMarkedForVersionIncrement(): bool
    {
        return $this->incrementVersion;
    }

    public function markForVersionIncrement(): void
    {
        $this->incrementVersion = true;
    }

    public function getVersionField(): string
    {
        return 'version';
    }

    private static function addVersionField(ClassMetadataBuilder $builder): void
    {
        $builder->createField('version', Types::INTEGER)
            ->columnName('version')
            ->option('default', 1)
            ->option('unsigned', true)
            ->build();
    }
}