Test Failed
Branch master (3c461d)
by Mohamed
103:57
created

DataList   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 40
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 3 1
A __construct() 0 5 1
A add() 0 3 1
1
<?php
2
3
namespace Moo\HasOneSelector\ORM;
4
5
use Exception;
6
use Moo\HasOneSelector\Form\GridField;
7
use SilverStripe\ORM\DataList as BaseDataList;
8
use SilverStripe\ORM\DataObject;
9
10
/**
11
 * Class DataList is data list to manage add/remove managed object from the
12
 * HasOneSelectorField class
13
 */
14
class DataList extends BaseDataList
15
{
16
    /**
17
     * @var GridField
18
     */
19
    protected $gridField;
20
21
    /**
22
     * HasOneSelectorDataList constructor.
23
     * @param GridField $gridField
24
     */
25
    public function __construct(GridField $gridField)
26
    {
27
        $this->gridField = $gridField;
28
29
        parent::__construct($gridField->getDataClass());
30
    }
31
32
    /**
33
     * Set the current selected record into the has one relation
34
     *
35
     * @param  DataObject $item
36
     * @return void
37
     * @throws Exception
38
     */
39
    public function add($item)
40
    {
41
        $this->gridField->setRecord($item);
42
    }
43
44
    /**
45
     * Clear the record within the has one relation
46
     *
47
     * @param  DataObject $item
48
     * @return void
49
     * @throws Exception
50
     */
51
    public function remove($item)
52
    {
53
        $this->gridField->setRecord(null);
54
    }
55
}
56