Failed Conditions
Pull Request — master (#7210)
by Michael
12:25
created

OptimisticLockFailed::lockFailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Exception;
6
7
/**
8
 * An OptimisticLockFailed is thrown when a version check on an object
9
 * that uses optimistic locking through a version field fails.
10
 */
11
final class OptimisticLockFailed extends \RuntimeException implements ORMException
12
{
13
    /** @var object|null */
14
    private $entity;
15
16
    /**
17
     * @param string $msg
18
     * @param object $entity
19
     */
20 10
    public function __construct($msg, $entity)
21
    {
22 10
        parent::__construct($msg);
23 10
        $this->entity = $entity;
24 10
    }
25
26
    /**
27
     * Gets the entity that caused the exception.
28
     *
29
     * @return object|null
30
     */
31 5
    public function getEntity()
32
    {
33 5
        return $this->entity;
34
    }
35
36
    /**
37
     * @param object $entity
38
     * @return OptimisticLockFailed
39
     */
40 5
    public static function lockFailed($entity)
41
    {
42 5
        return new self('The optimistic lock on an entity failed.', $entity);
43
    }
44
45
    /**
46
     * @param object $entity
47
     * @param int    $expectedLockVersion
48
     * @param int    $actualLockVersion
49
     * @return OptimisticLockFailed
50
     */
51 2
    public static function lockFailedVersionMismatch($entity, $expectedLockVersion, $actualLockVersion)
52
    {
53 2
        $expectedLockVersion = ($expectedLockVersion instanceof \DateTime) ? $expectedLockVersion->getTimestamp() : $expectedLockVersion;
0 ignored issues
show
introduced by
$expectedLockVersion is never a sub-type of DateTime.
Loading history...
54 2
        $actualLockVersion   = ($actualLockVersion instanceof \DateTime) ? $actualLockVersion->getTimestamp() : $actualLockVersion;
0 ignored issues
show
introduced by
$actualLockVersion is never a sub-type of DateTime.
Loading history...
55
56 2
        return new self('The optimistic lock failed, version ' . $expectedLockVersion . ' was expected, but is actually ' . $actualLockVersion, $entity);
57
    }
58
59
    /**
60
     * @param string $entityName
61
     *
62
     * @return OptimisticLockFailed
63
     */
64 3
    public static function notVersioned($entityName)
65
    {
66 3
        return new self('Cannot obtain optimistic lock on unversioned entity ' . $entityName, null);
67
    }
68
}
69