OptimisticLockException.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /**
  21. * An OptimisticLockException is thrown when a version check on an object
  22. * that uses optimistic locking through a version field fails.
  23. *
  24. * @author Roman Borschel <roman@code-factory.org>
  25. * @author Benjamin Eberlei <kontakt@beberlei.de>
  26. * @since 2.0
  27. */
  28. class OptimisticLockException extends ORMException
  29. {
  30. /**
  31. * @var object|null
  32. */
  33. private $entity;
  34. /**
  35. * @param string $msg
  36. * @param object $entity
  37. */
  38. public function __construct($msg, $entity)
  39. {
  40. parent::__construct($msg);
  41. $this->entity = $entity;
  42. }
  43. /**
  44. * Gets the entity that caused the exception.
  45. *
  46. * @return object|null
  47. */
  48. public function getEntity()
  49. {
  50. return $this->entity;
  51. }
  52. /**
  53. * @param object $entity
  54. *
  55. * @return OptimisticLockException
  56. */
  57. public static function lockFailed($entity)
  58. {
  59. return new self("The optimistic lock on an entity failed.", $entity);
  60. }
  61. /**
  62. * @param object $entity
  63. * @param int $expectedLockVersion
  64. * @param int $actualLockVersion
  65. *
  66. * @return OptimisticLockException
  67. */
  68. public static function lockFailedVersionMismatch($entity, $expectedLockVersion, $actualLockVersion)
  69. {
  70. $expectedLockVersion = ($expectedLockVersion instanceof \DateTime) ? $expectedLockVersion->getTimestamp() : $expectedLockVersion;
  71. $actualLockVersion = ($actualLockVersion instanceof \DateTime) ? $actualLockVersion->getTimestamp() : $actualLockVersion;
  72. return new self("The optimistic lock failed, version " . $expectedLockVersion . " was expected, but is actually ".$actualLockVersion, $entity);
  73. }
  74. /**
  75. * @param string $entityName
  76. *
  77. * @return OptimisticLockException
  78. */
  79. public static function notVersioned($entityName)
  80. {
  81. return new self("Cannot obtain optimistic lock on unversioned entity " . $entityName, null);
  82. }
  83. }