OneToOne   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 27
c 2
b 0
f 0
dl 0
loc 48
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A attachMatchesToEntity() 0 12 2
A addActionOnSave() 0 13 3
A addActionOnDelete() 0 15 3
1
<?php
2
3
namespace Sirius\Orm\Relation;
4
5
use Sirius\Orm\Action\BaseAction;
6
use Sirius\Orm\Entity\EntityInterface;
7
use Sirius\Orm\Entity\StateEnum;
8
9
class OneToOne extends OneToMany
10
{
11
    public function attachMatchesToEntity(EntityInterface $nativeEntity, array $result)
12
    {
13
        // no point in linking entities if the native one is deleted
14
        if ($nativeEntity->getPersistenceState() == StateEnum::DELETED) {
15
            return;
16
        }
17
18
        $nativeId = $this->getEntityId($this->nativeMapper, $nativeEntity, array_keys($this->keyPairs));
19
20
        $found = $result[$nativeId] ?? [];
21
22
        $this->nativeMapper->setEntityAttribute($nativeEntity, $this->name, $found[0] ?? null);
23
    }
24
25
    protected function addActionOnDelete(BaseAction $action)
26
    {
27
        // no cascade delete? treat it as a save
28
        if (! $this->isCascade()) {
29
            $this->addActionOnSave($action);
30
        } else {
31
            $foreignEntity = $this->nativeMapper
32
                                  ->getEntityAttribute($action->getEntity(), $this->name);
33
34
            if ($foreignEntity) {
35
                $remainingRelations = $this->getRemainingRelations($action->getOption('relations'));
36
                $deleteAction       = $this->foreignMapper
37
                    ->newDeleteAction($foreignEntity, ['relations' => $remainingRelations]);
38
                $action->prepend($deleteAction);
39
                $action->append($this->newSyncAction($action->getEntity(), $foreignEntity, 'delete'));
40
            }
41
        }
42
    }
43
44
    protected function addActionOnSave(BaseAction $action)
45
    {
46
        if (! $this->relationWasChanged($action->getEntity())) {
47
            return;
48
        }
49
        $foreignEntity = $this->nativeMapper->getEntityAttribute($action->getEntity(), $this->name);
50
        if ($foreignEntity) {
51
            $remainingRelations = $this->getRemainingRelations($action->getOption('relations'));
52
            $saveAction         = $this->foreignMapper
53
                ->newSaveAction($foreignEntity, ['relations' => $remainingRelations]);
54
            $saveAction->addColumns($this->getExtraColumnsForAction());
55
            $action->prepend($saveAction);
56
            $action->append($this->newSyncAction($action->getEntity(), $foreignEntity, 'save'));
57
        }
58
    }
59
}
60