Passed
Push — hans/Index-all-fluent-options ( 33af16...2afa88 )
by Simon
07:48
created

DataResolveTrait::resolveDataObject()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 10
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
namespace Firesphere\SolrSearch\Traits;
4
5
use LogicException;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\FieldType\DBField;
9
use SilverStripe\ORM\SS_List;
10
use SilverStripe\View\ArrayData;
11
12
/**
13
 * Trait ResolveTrait All resolver methods for the DataResolver
14
 *
15
 * @package Firesphere\SolrSearch\Traits
16
 */
17
trait DataResolveTrait
18
{
19
    /**
20
     * @var DataObject|ArrayList|SS_List|DBField
21
     */
22
    protected $component;
23
    /**
24
     * @var array
25
     */
26
    protected $columns = [];
27
    /**
28
     * @var mixed|string|null
29
     */
30
    protected $columnName = '';
31
    /**
32
     * @var string
33
     */
34
    protected $shortName;
35
36
    /**
37
     * Resolves an ArrayData value
38
     *
39
     * @return mixed
40
     * @throws LogicException
41
     */
42 4
    protected function resolveArrayData()
43
    {
44 4
        if (empty($this->columnName)) {
45 1
            return $this->component->toMap();
46
        }
47
        // Inspect component has attribute
48 3
        if (empty($this->columns) && $this->component->hasField($this->columnName)) {
49 2
            return $this->component->{$this->columnName};
50
        }
51 1
        $this->cannotIdentifyException($this->component, array_merge([$this->columnName], $this->columns));
52
    }
53
54
    /**
55
     * @param DataObject|ArrayData|SS_List $component
56
     * @param array $columns
57
     *
58
     * @return void
59
     * @throws LogicException
60
     */
61
    abstract protected function cannotIdentifyException($component, $columns = []): void;
62
63
    /**
64
     * Resolves a DataList values
65
     *
66
     * @return array|mixed
67
     * @throws LogicException
68
     */
69 3
    protected function resolveList()
70
    {
71 3
        if (empty($this->columnName)) {
72 3
            return $this->component->toNestedArray();
73
        }
74
        // Inspect $component for element $relation
75 2
        if ($this->component->hasMethod($this->columnName)) {
76 2
            $relation = $this->columnName;
77
78 2
            return self::identify($this->component->$relation(), $this->columns);
79
        }
80 1
        $data = [];
81 1
        array_unshift($this->columns, $this->columnName);
82 1
        foreach ($this->component as $component) {
83 1
            $data[] = self::identify($component, $this->columns);
84
        }
85
86 1
        return $data;
87
    }
88
89
    /**
90
     * Resolves a Single field in the database.
91
     *
92
     * @return mixed
93
     * @throws LogicException
94
     */
95 10
    protected function resolveField()
96
    {
97 10
        if ($this->columnName) {
98
            // @todo could be simplified if possible?
99 4
            if ($this->component->hasMethod($this->columnName)) {
100 3
                $method = $this->columnName;
101 2
            } elseif ($this->component->hasMethod("get{$this->columnName}")) {
102 1
                $method = "get{$this->columnName}";
103
            } else {
104 1
                throw new LogicException(
105 1
                    sprintf('Method, "%s" not found on "%s"', $this->columnName, $this->shortName)
106
                );
107
            }
108
109 3
            $value = $this->component->$method();
110
        } else {
111 8
            $value = $this->component->getValue();
112
        }
113
114 9
        if (!empty($this->columns)) {
115 1
            $this->cannotIdentifyException($this->component, $this->columns);
116
        }
117
118 8
        return $value;
119
    }
120
121
    /**
122
     * Resolves a DataObject value
123
     *
124
     * @return mixed
125
     * @throws LogicException
126
     */
127 15
    protected function resolveDataObject()
128
    {
129 15
        if (empty($this->columnName)) {
130 2
            return $this->component->toMap();
131
        }
132
        // Inspect component for element $relation
133 14
        if ($this->component->hasMethod($this->columnName)) {
134 2
            return $this->getMethodValue();
135
        }
136
        // Inspect component has attribute
137 14
        if ($this->component->hasField($this->columnName)) {
138 13
            return $this->getFieldValue();
139
        }
140 7
        $this->cannotIdentifyException($this->component, [$this->columnName]);
141
    }
142
143
    /**
144
     * @return mixed
145
     */
146 2
    protected function getMethodValue()
147
    {
148 2
        $relation = $this->columnName;
149
        // We hit a direct method that returns a non-object
150 2
        if (!is_object($this->component->$relation())) {
151 1
            return $this->component->$relation();
152
        }
153
154 1
        return self::identify($this->component->$relation(), $this->columns);
155
    }
156
157
    /**
158
     * @return mixed
159
     */
160 13
    protected function getFieldValue()
161
    {
162 13
        $data = $this->component->{$this->columnName};
163 13
        $dbObject = $this->component->dbObject($this->columnName);
164 13
        if ($dbObject) {
165
            // @todo do we need to set the value?
166 10
            $dbObject->setValue($data);
167
168 10
            return self::identify($dbObject, $this->columns);
169
        }
170
171 3
        return $data;
172
    }
173
}
174