Failed Conditions
Pull Request — master (#7008)
by Grégoire
13:34 queued 01:47
created

lockFailedVersionMismatch()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 4
nop 3
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 12
rs 9.4285
c 1
b 0
f 0
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
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\ORMException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
{
13
    /**
14
     * @var object|null
15
     */
16
    private $entity;
17
18
    /**
19
     * @param string $msg
20
     * @param object $entity
21
     */
22
    public function __construct($msg, $entity)
23
    {
24
        parent::__construct($msg);
25
        $this->entity = $entity;
26
    }
27
28
    /**
29
     * Gets the entity that caused the exception.
30
     *
31
     * @return object|null
32
     */
33
    public function getEntity()
34
    {
35
        return $this->entity;
36
    }
37
38
    /**
39
     * @param object $entity
40
     *
41
     * @return OptimisticLockException
42
     */
43
    public static function lockFailed($entity)
44
    {
45
        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
    public static function lockFailedVersionMismatch($entity, $expectedLockVersion, $actualLockVersion)
56
    {
57
        $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
        $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
        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
    public static function notVersioned($entityName)
69
    {
70
        return new self('Cannot obtain optimistic lock on unversioned entity ' . $entityName, null);
71
    }
72
}
73