GeneratedIntegerIdEntity::isSame()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
ccs 0
cts 9
cp 0
rs 9.6666
cc 4
nc 3
nop 1
crap 20
1
<?php
2
3
namespace Dontdrinkandroot\Entity;
4
5
use Doctrine\Common\Util\ClassUtils;
6
7
/**
8
 * @author Philip Washington Sorst <[email protected]>
9
 */
10
class GeneratedIntegerIdEntity implements IntegerIdEntityInterface
11
{
12
    /** @var int */
13
    private $id;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 12
    public function getId(): ?int
19
    {
20 12
        return $this->id;
21
    }
22
23
    public function isPersisted(): bool
24
    {
25
        return null !== $this->id;
26
    }
27
28
    /**
29
     * Checks if this entity represents the same entity as another one. Usually this is checked via the class and id.
30
     *
31
     * @param mixed $other
32
     *
33
     * @return bool
34
     */
35
    public function isSame($other): bool
36
    {
37
        if (null === $other || !is_object($other)) {
38
            return false;
39
        }
40
41
        $thisClass = ClassUtils::getRealClass(get_class($this));
42
        $otherClass = ClassUtils::getRealClass(get_class($other));
43
44
        if ($thisClass !== $otherClass) {
45
            return false;
46
        }
47
48
        /** @var EntityInterface $otherEntity */
49
        $otherEntity = $other;
50
51
        return $this->getId() === $otherEntity->getId();
52
    }
53
}
54