Passed
Pull Request — 4 (#8209)
by Ingo
09:07
created

HasManyList::remove()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 3
nop 1
dl 0
loc 19
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Subclass of {@link DataList} representing a has_many relation.
9
 */
10
class HasManyList extends RelationList
11
{
12
13
    /**
14
     * @var string
15
     */
16
    protected $foreignKey;
17
18
    /**
19
     * Create a new HasManyList object.
20
     * Generation of the appropriate record set is left up to the caller, using the normal
21
     * {@link DataList} methods.  Addition arguments are used to support {@@link add()}
22
     * and {@link remove()} methods.
23
     *
24
     * @param string $dataClass The class of the DataObjects that this will list.
25
     * @param string $foreignKey The name of the foreign key field to set the ID filter against.
26
     */
27
    public function __construct($dataClass, $foreignKey)
28
    {
29
        parent::__construct($dataClass);
30
31
        $this->foreignKey = $foreignKey;
32
    }
33
34
    /**
35
     * Gets the field name which holds the related object ID.
36
     *
37
     * @return string
38
     */
39
    public function getForeignKey()
40
    {
41
        return $this->foreignKey;
42
    }
43
44
    /**
45
     * @param null|int $id
46
     * @return array
47
     */
48
    protected function foreignIDFilter($id = null)
49
    {
50
        if ($id === null) {
51
            $id = $this->getForeignID();
52
        }
53
54
        // Apply relation filter
55
        $key = DataObject::getSchema()->sqlColumnForField($this->dataClass(), $this->getForeignKey());
56
        if (is_array($id)) {
0 ignored issues
show
introduced by
The condition is_array($id) is always false.
Loading history...
57
            return array("$key IN (" . DB::placeholders($id) . ")"  => $id);
58
        } elseif ($id !== null) {
0 ignored issues
show
introduced by
The condition $id !== null is always true.
Loading history...
59
            return array($key => $id);
60
        }
61
        return null;
62
    }
63
64
    /**
65
     * Adds the item to this relation.
66
     *
67
     * It does so by setting the relationFilters.
68
     *
69
     * @param DataObject|int $item The DataObject to be added, or its ID
70
     */
71
    public function add($item)
72
    {
73
        if (is_numeric($item)) {
74
            $item = DataObject::get_by_id($this->dataClass, $item);
75
        } elseif (!($item instanceof $this->dataClass)) {
76
            user_error("HasManyList::add() expecting a $this->dataClass object, or ID value", E_USER_ERROR);
77
        }
78
79
        $foreignID = $this->getForeignID();
80
81
        // Validate foreignID
82
        if (!$foreignID) {
83
            user_error("HasManyList::add() can't be called until a foreign ID is set", E_USER_WARNING);
84
            return;
85
        }
86
        if (is_array($foreignID)) {
0 ignored issues
show
introduced by
The condition is_array($foreignID) is always false.
Loading history...
87
            user_error("HasManyList::add() can't be called on a list linked to mulitple foreign IDs", E_USER_WARNING);
88
            return;
89
        }
90
91
        $foreignKey = $this->foreignKey;
92
        $item->$foreignKey = $foreignID;
93
94
        $item->write();
95
    }
96
97
    /**
98
     * Remove an item from this relation.
99
     *
100
     * Doesn't actually remove the item, it just clears the foreign key value.
101
     *
102
     * @param int $itemID The ID of the item to be removed.
103
     */
104
    public function removeByID($itemID)
105
    {
106
        $item = $this->byID($itemID);
107
108
        return $this->remove($item);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->remove($item) targeting SilverStripe\ORM\HasManyList::remove() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
109
    }
110
111
    /**
112
     * Remove an item from this relation.
113
     * Doesn't actually remove the item, it just clears the foreign key value.
114
     *
115
     * @param DataObject $item The DataObject to be removed
116
     * @todo Maybe we should delete the object instead?
117
     */
118
    public function remove($item)
119
    {
120
        if (!($item instanceof $this->dataClass)) {
121
            throw new InvalidArgumentException(
122
                "HasManyList::remove() expecting a $this->dataClass object, or ID",
123
                E_USER_ERROR
124
            );
125
        }
126
127
        // Don't remove item which doesn't belong to this list
128
        $foreignID = $this->getForeignID();
129
        $foreignKey = $this->getForeignKey();
130
131
        if (empty($foreignID)
132
            || (is_array($foreignID) && in_array($item->$foreignKey, $foreignID))
133
            || $foreignID == $item->$foreignKey
134
        ) {
135
            $item->$foreignKey = null;
136
            $item->write();
137
        }
138
    }
139
}
140