BasicObjectPersister::getObjectIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper\Persister;
6
7
use Doctrine\SkeletonMapper\Mapping\ClassMetadataInterface;
8
use Doctrine\SkeletonMapper\ObjectManagerInterface;
9
use Doctrine\SkeletonMapper\UnitOfWork\ChangeSet;
10
use InvalidArgumentException;
11
use function get_class;
12
use function sprintf;
13
14
abstract class BasicObjectPersister extends ObjectPersister
15
{
16
    /** @var ObjectManagerInterface */
17
    protected $objectManager;
18
19
    /** @var string */
20
    protected $className;
21
22
    /** @var ClassMetadataInterface */
23
    protected $class;
24
25 25
    public function __construct(ObjectManagerInterface $objectManager, string $className)
26
    {
27 25
        $this->objectManager = $objectManager;
28 25
        $this->className     = $className;
29 25
    }
30
31
    public function getClassName() : string
32
    {
33
        return $this->className;
34
    }
35
36 15
    public function getClassMetadata() : ClassMetadataInterface
37
    {
38 15
        if ($this->class === null) {
39 15
            $this->class = $this->objectManager->getClassMetadata($this->className);
40
        }
41
42 15
        return $this->class;
43
    }
44
45
    /**
46
     * Prepares an object changeset for persistence.
47
     *
48
     * @param object $object
49
     *
50
     * @return mixed[]
51
     */
52 10
    public function preparePersistChangeSet($object) : array
53
    {
54 10
        if (! $object instanceof PersistableInterface) {
55
            throw new InvalidArgumentException(
56
                sprintf('%s must implement PersistableInterface.', get_class($object))
57
            );
58
        }
59
60 10
        return $object->preparePersistChangeSet();
61
    }
62
63
    /**
64
     * Prepares an object changeset for update.
65
     *
66
     * @param object $object
67
     *
68
     * @return mixed[]
69
     */
70 9
    public function prepareUpdateChangeSet($object, ChangeSet $changeSet) : array
71
    {
72 9
        if (! $object instanceof PersistableInterface) {
73
            throw new InvalidArgumentException(sprintf('%s must implement PersistableInterface.', get_class($object)));
74
        }
75
76 9
        return $object->prepareUpdateChangeSet($changeSet);
77
    }
78
79
    /**
80
     * Assign identifier to object.
81
     *
82
     * @param object  $object
83
     * @param mixed[] $identifier
84
     */
85 9
    public function assignIdentifier($object, array $identifier) : void
86
    {
87 9
        if (! $object instanceof IdentifiableInterface) {
88
            throw new InvalidArgumentException(sprintf('%s must implement IdentifiableInterface.', get_class($object)));
89
        }
90
91 9
        $object->assignIdentifier($identifier);
92 9
    }
93
94
    /**
95
     * @param object $object
96
     *
97
     * @return mixed[] $identifier
98
     */
99 11
    protected function getObjectIdentifier($object) : array
100
    {
101 11
        return $this->objectManager
102 11
            ->getRepository(get_class($object))
103 11
            ->getObjectIdentifier($object);
104
    }
105
}
106