ArrayObjectPersister   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 77
ccs 30
cts 30
cp 1
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A removeObject() 0 6 1
A persistObject() 0 13 2
A generateNextId() 0 8 3
A __construct() 0 8 1
A updateObject() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper\Persister;
6
7
use Doctrine\Common\Collections\ArrayCollection;
8
use Doctrine\SkeletonMapper\Mapping\ClassMetadataInterface;
9
use Doctrine\SkeletonMapper\ObjectManagerInterface;
10
use Doctrine\SkeletonMapper\UnitOfWork\ChangeSet;
11
use function max;
12
13
class ArrayObjectPersister extends BasicObjectPersister
14
{
15
    /** @var ArrayCollection */
16
    protected $objects;
17
18 25
    public function __construct(
19
        ObjectManagerInterface $objectManager,
20
        ArrayCollection $objects,
21
        string $className
22
    ) {
23 25
        parent::__construct($objectManager, $className);
24
25 25
        $this->objects = $objects;
26 25
    }
27
28
    /**
29
     * @param object $object
30
     *
31
     * @return mixed[]
32
     */
33 10
    public function persistObject($object) : array
34
    {
35 10
        $data = $this->preparePersistChangeSet($object);
36
37 10
        $class = $this->getClassMetadata();
38
39 10
        if (! isset($data[$class->getIdentifier()[0]])) {
40 5
            $data[$class->getIdentifier()[0]] = $this->generateNextId($class);
41
        }
42
43 10
        $this->objects[$data[$class->getIdentifier()[0]]] = $data;
44
45 10
        return $data;
46
    }
47
48
    /**
49
     * @param object $object
50
     *
51
     * @return mixed[]
52
     */
53 9
    public function updateObject($object, ChangeSet $changeSet) : array
54
    {
55 9
        $changeSet = $this->prepareUpdateChangeSet($object, $changeSet);
56
57 9
        $class      = $this->getClassMetadata();
58 9
        $identifier = $this->getObjectIdentifier($object);
59
60 9
        $objectData = $this->objects[$identifier[$class->getIdentifier()[0]]];
61
62 9
        foreach ($changeSet as $key => $value) {
63 9
            $objectData[$key] = $value;
64
        }
65
66 9
        $this->objects[$objectData[$class->getIdentifier()[0]]] = $objectData;
67
68 9
        return $objectData;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $objectData could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
71
    /**
72
     * @param object $object
73
     */
74 4
    public function removeObject($object) : void
75
    {
76 4
        $class      = $this->getClassMetadata();
77 4
        $identifier = $this->getObjectIdentifier($object);
78
79 4
        unset($this->objects[$identifier[$class->getIdentifier()[0]]]);
80 4
    }
81
82 5
    private function generateNextId(ClassMetadataInterface $class) : int
83
    {
84 5
        $ids = [];
85 5
        foreach ($this->objects as $objectData) {
86 3
            $ids[] = $objectData[$class->getIdentifier()[0]];
87
        }
88
89 5
        return $ids !== [] ? (int) (max($ids) + 1) : 1;
90
    }
91
}
92