diff options
Diffstat (limited to 'vendor/doctrine/orm/src/OptimisticLockException.php')
-rw-r--r-- | vendor/doctrine/orm/src/OptimisticLockException.php | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/OptimisticLockException.php b/vendor/doctrine/orm/src/OptimisticLockException.php new file mode 100644 index 0000000..f84e134 --- /dev/null +++ b/vendor/doctrine/orm/src/OptimisticLockException.php | |||
@@ -0,0 +1,55 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM; | ||
6 | |||
7 | use DateTimeInterface; | ||
8 | use Doctrine\ORM\Exception\ORMException; | ||
9 | use Exception; | ||
10 | use Throwable; | ||
11 | |||
12 | /** | ||
13 | * An OptimisticLockException is thrown when a version check on an object | ||
14 | * that uses optimistic locking through a version field fails. | ||
15 | */ | ||
16 | class OptimisticLockException extends Exception implements ORMException | ||
17 | { | ||
18 | public function __construct( | ||
19 | string $msg, | ||
20 | private readonly object|string|null $entity, | ||
21 | Throwable|null $previous = null, | ||
22 | ) { | ||
23 | parent::__construct($msg, 0, $previous); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Gets the entity that caused the exception. | ||
28 | */ | ||
29 | public function getEntity(): object|string|null | ||
30 | { | ||
31 | return $this->entity; | ||
32 | } | ||
33 | |||
34 | /** @param object|class-string $entity */ | ||
35 | public static function lockFailed(object|string $entity): self | ||
36 | { | ||
37 | return new self('The optimistic lock on an entity failed.', $entity); | ||
38 | } | ||
39 | |||
40 | public static function lockFailedVersionMismatch( | ||
41 | object $entity, | ||
42 | int|string|DateTimeInterface $expectedLockVersion, | ||
43 | int|string|DateTimeInterface $actualLockVersion, | ||
44 | ): self { | ||
45 | $expectedLockVersion = $expectedLockVersion instanceof DateTimeInterface ? $expectedLockVersion->getTimestamp() : $expectedLockVersion; | ||
46 | $actualLockVersion = $actualLockVersion instanceof DateTimeInterface ? $actualLockVersion->getTimestamp() : $actualLockVersion; | ||
47 | |||
48 | return new self('The optimistic lock failed, version ' . $expectedLockVersion . ' was expected, but is actually ' . $actualLockVersion, $entity); | ||
49 | } | ||
50 | |||
51 | public static function notVersioned(string $entityName): self | ||
52 | { | ||
53 | return new self('Cannot obtain optimistic lock on unversioned entity ' . $entityName, null); | ||
54 | } | ||
55 | } | ||