Passed
Push — master ( 47e01d...760836 )
by Thomas
12:05
created

HasOneButtonRelationList::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 20
rs 9.8333
cc 2
nc 2
nop 1
1
<?php
2
3
namespace LeKoala\Tabulator;
4
5
use SilverStripe\ORM\ArrayList;
6
use SilverStripe\ORM\DataObject;
7
8
/**
9
 * Class HasOneButtonRelationList
10
 * @link https://github.com/silvershop/silverstripe-hasonefield/blob/main/src/HasOneButtonRelationList.php
11
 */
12
class HasOneButtonRelationList extends ArrayList
13
{
14
    /**
15
     * @var DataObject
16
     */
17
    protected $record;
18
19
    /**
20
     * @var string
21
     */
22
    protected $relationName;
23
24
    /**
25
     * @var DataObject
26
     */
27
    protected $parent;
28
29
    /**
30
     * HasOneButtonRelationList constructor.
31
     * @param DataObject $parent
32
     * @param DataObject $record
33
     * @param string $relationName
34
     */
35
    public function __construct(DataObject $parent, DataObject $record, $relationName)
36
    {
37
        $this->record = $record;
38
        $this->relationName = $relationName;
39
        $this->parent = $parent;
40
41
        parent::__construct([$record]);
42
    }
43
44
    public function add($item)
45
    {
46
        $parent = $this->parent;
47
        // Get the relationship type (has_one or belongs_to)
48
        $relationType = $parent->getRelationType($this->relationName);
49
        switch ($relationType) {
50
            case 'belongs_to':
51
                // If belongs_to, retrieve and write to the has_one side of the relationship
52
                $parent->{$this->relationName} = $item;
53
                $hasOneRecord = $parent->getComponent($this->relationName);
54
                $hasOneRecord->write();
55
                break;
56
            default:
57
                // Otherwise assume has_one, and write to this record
58
                $parent->{$this->relationName} = $item;
59
                $parent->write();
60
                break;
61
        }
62
63
        $this->items = [$item];
64
    }
65
66
    public function remove($item)
67
    {
68
        $parent = $this->parent;
69
        $relationName = $this->relationName;
70
        // Get the relationship type (has_one or belongs_to)
71
        $relationType = $parent->getRelationType($relationName);
72
        switch ($relationType) {
73
            case 'belongs_to':
74
                // If belongs_to, retrieve and write to the has_one side of the relationship
75
                $hasOneRecord = $parent->getComponent($this->relationName);
76
                /** @var DataObject $parentClass */
77
                $parentClass = $parent->getClassName();
78
79
                $schema = $parentClass::getSchema();
80
                $polymorphic = false;
81
                $hasOneFieldName = $schema->getRemoteJoinField(
82
                    $parentClass,
83
                    $relationName,
84
                    $relationType,
85
                    $polymorphic
86
                );
87
88
                $hasOneRecord->{$hasOneFieldName} = null;
89
                $hasOneRecord->write();
90
                break;
91
            default:
92
                // Otherwise assume has_one, and write to this record
93
                $parent->{$relationName} = null;
94
                $parent->write();
95
                break;
96
        }
97
98
        $this->items = [];
99
    }
100
}
101