summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/OptimisticLockException.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/orm/src/OptimisticLockException.php')
-rw-r--r--vendor/doctrine/orm/src/OptimisticLockException.php55
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
3declare(strict_types=1);
4
5namespace Doctrine\ORM;
6
7use DateTimeInterface;
8use Doctrine\ORM\Exception\ORMException;
9use Exception;
10use 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 */
16class 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}