OptimisticLockException.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM;
  20. use DateTimeInterface;
  21. /**
  22. * An OptimisticLockException is thrown when a version check on an object
  23. * that uses optimistic locking through a version field fails.
  24. */
  25. class OptimisticLockException extends ORMException
  26. {
  27. /** @var object|null */
  28. private $entity;
  29. /**
  30. * @param string $msg
  31. * @param object|null $entity
  32. */
  33. public function __construct($msg, $entity)
  34. {
  35. parent::__construct($msg);
  36. $this->entity = $entity;
  37. }
  38. /**
  39. * Gets the entity that caused the exception.
  40. *
  41. * @return object|null
  42. */
  43. public function getEntity()
  44. {
  45. return $this->entity;
  46. }
  47. /**
  48. * @param object $entity
  49. *
  50. * @return OptimisticLockException
  51. */
  52. public static function lockFailed($entity)
  53. {
  54. return new self('The optimistic lock on an entity failed.', $entity);
  55. }
  56. /**
  57. * @param object $entity
  58. * @param int $expectedLockVersion
  59. * @param int $actualLockVersion
  60. *
  61. * @return OptimisticLockException
  62. */
  63. public static function lockFailedVersionMismatch($entity, $expectedLockVersion, $actualLockVersion)
  64. {
  65. $expectedLockVersion = $expectedLockVersion instanceof DateTimeInterface ? $expectedLockVersion->getTimestamp() : $expectedLockVersion;
  66. $actualLockVersion = $actualLockVersion instanceof DateTimeInterface ? $actualLockVersion->getTimestamp() : $actualLockVersion;
  67. return new self('The optimistic lock failed, version ' . $expectedLockVersion . ' was expected, but is actually ' . $actualLockVersion, $entity);
  68. }
  69. /**
  70. * @param string $entityName
  71. *
  72. * @return OptimisticLockException
  73. */
  74. public static function notVersioned($entityName)
  75. {
  76. return new self('Cannot obtain optimistic lock on unversioned entity ' . $entityName, null);
  77. }
  78. }