BasicObjectPersister   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 78.56%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 22
c 2
b 0
f 0
dl 0
loc 90
ccs 22
cts 28
cp 0.7856
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getObjectIdentifier() 0 5 1
A getClassMetadata() 0 7 2
A preparePersistChangeSet() 0 9 2
A __construct() 0 4 1
A prepareUpdateChangeSet() 0 7 2
A getClassName() 0 3 1
A assignIdentifier() 0 7 2
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