summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/OptimisticLockException.php
blob: f84e134b05e8dc970212dee50641b7d0732cefa4 (plain)
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
55
<?php

declare(strict_types=1);

namespace Doctrine\ORM;

use DateTimeInterface;
use Doctrine\ORM\Exception\ORMException;
use Exception;
use Throwable;

/**
 * An OptimisticLockException is thrown when a version check on an object
 * that uses optimistic locking through a version field fails.
 */
class OptimisticLockException extends Exception implements ORMException
{
    public function __construct(
        string $msg,
        private readonly object|string|null $entity,
        Throwable|null $previous = null,
    ) {
        parent::__construct($msg, 0, $previous);
    }

    /**
     * Gets the entity that caused the exception.
     */
    public function getEntity(): object|string|null
    {
        return $this->entity;
    }

    /** @param object|class-string $entity */
    public static function lockFailed(object|string $entity): self
    {
        return new self('The optimistic lock on an entity failed.', $entity);
    }

    public static function lockFailedVersionMismatch(
        object $entity,
        int|string|DateTimeInterface $expectedLockVersion,
        int|string|DateTimeInterface $actualLockVersion,
    ): self {
        $expectedLockVersion = $expectedLockVersion instanceof DateTimeInterface ? $expectedLockVersion->getTimestamp() : $expectedLockVersion;
        $actualLockVersion   = $actualLockVersion instanceof DateTimeInterface ? $actualLockVersion->getTimestamp() : $actualLockVersion;

        return new self('The optimistic lock failed, version ' . $expectedLockVersion . ' was expected, but is actually ' . $actualLockVersion, $entity);
    }

    public static function notVersioned(string $entityName): self
    {
        return new self('Cannot obtain optimistic lock on unversioned entity ' . $entityName, null);
    }
}