Failed Conditions
Push — master ( 2ccf23...d791f7 )
by Michael
10:40
created

OptimisticLockException   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 60
ccs 14
cts 14
cp 1
rs 10
c 1
b 0
f 0
wmc 7

5 Methods

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