Passed
Push — master ( bbddf1...921bcc )
by Thomas
21:07 queued 09:26
created

HasOneButtonRelationList::forTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 forTemplate()
45
    {
46
        return $this->relationName;
47
    }
48
49
    public function add($item)
50
    {
51
        $parent = $this->parent;
52
        // Get the relationship type (has_one or belongs_to)
53
        $relationType = $parent->getRelationType($this->relationName);
54
        switch ($relationType) {
55
            case 'belongs_to':
56
                // If belongs_to, retrieve and write to the has_one side of the relationship
57
                $parent->{$this->relationName} = $item;
58
                $hasOneRecord = $parent->getComponent($this->relationName);
59
                $hasOneRecord->write();
60
                break;
61
            default:
62
                // Otherwise assume has_one, and write to this record
63
                $parent->{$this->relationName} = $item;
64
                $parent->write();
65
                break;
66
        }
67
68
        $this->items = [$item];
69
    }
70
71
    public function remove($item)
72
    {
73
        $parent = $this->parent;
74
        $relationName = $this->relationName;
75
        // Get the relationship type (has_one or belongs_to)
76
        $relationType = $parent->getRelationType($relationName);
77
        switch ($relationType) {
78
            case 'belongs_to':
79
                // If belongs_to, retrieve and write to the has_one side of the relationship
80
                $hasOneRecord = $parent->getComponent($this->relationName);
81
                /** @var DataObject $parentClass */
82
                $parentClass = $parent->getClassName();
83
84
                $schema = $parentClass::getSchema();
85
                $polymorphic = false;
86
                $hasOneFieldName = $schema->getRemoteJoinField(
87
                    $parentClass,
88
                    $relationName,
89
                    $relationType,
90
                    $polymorphic
91
                );
92
93
                $hasOneRecord->{$hasOneFieldName} = null;
94
                $hasOneRecord->write();
95
                break;
96
            default:
97
                // Otherwise assume has_one, and write to this record
98
                $parent->{$relationName} = null;
99
                $parent->write();
100
                break;
101
        }
102
103
        $this->items = [];
104
    }
105
}
106