Passed
Push — hans/code-cleanup ( 3e12c7...fc06c8 )
by Simon
04:41
created

DataResolveTrait::getMethodValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
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
 * @package Firesphere\SolrSearch\Traits
15
 */
16
trait DataResolveTrait
17
{
18
    /**
19
     * @var DataObject|ArrayList|SS_List|DBField
20
     */
21
    protected $component;
22
    /**
23
     * @var array
24
     */
25
    protected $columns = [];
26
    /**
27
     * @var mixed|string|null
28
     */
29
    protected $columnName = '';
30
    /**
31
     * @var string
32
     */
33
    protected $shortName;
34
35
    /**
36
     * Resolves an ArrayData value
37
     * @return mixed
38
     * @throws LogicException
39
     */
40 4
    protected function resolveArrayData()
41
    {
42 4
        if (empty($this->columnName)) {
43 1
            return $this->component->toMap();
44
        }
45
        // Inspect component has attribute
46 3
        if (empty($this->columns) && $this->component->hasField($this->columnName)) {
47 2
            return $this->component->{$this->columnName};
48
        }
49 1
        $this->cannotIdentifyException($this->component, array_merge([$this->columnName], $this->columns));
50
    }
51
52
    /**
53
     * @param DataObject|ArrayData|SS_List $component
54
     * @param array $columns
55
     *
56
     * @return void
57
     * @throws LogicException
58
     */
59
    abstract protected function cannotIdentifyException($component, $columns = []): void;
60
61
    /**
62
     * Resolves a DataList values
63
     * @return array|mixed
64
     * @throws LogicException
65
     */
66 3
    protected function resolveList()
67
    {
68 3
        if (empty($this->columnName)) {
69 3
            return $this->component->toNestedArray();
70
        }
71
        // Inspect $component for element $relation
72 2
        if ($this->component->hasMethod($this->columnName)) {
73 2
            $relation = $this->columnName;
74
75 2
            return self::identify($this->component->$relation(), $this->columns);
76
        }
77 1
        $data = [];
78 1
        array_unshift($this->columns, $this->columnName);
79 1
        foreach ($this->component as $component) {
80 1
            $data[] = self::identify($component, $this->columns);
81
        }
82
83 1
        return $data;
84
    }
85
86
    /**
87
     * Resolves a Single field in the database.
88
     * @return mixed
89
     * @throws LogicException
90
     */
91 8
    protected function resolveField()
92
    {
93 8
        if ($this->columnName) {
94
            // @todo could be simplified if possible?
95 4
            if ($this->component->hasMethod($this->columnName)) {
96 3
                $method = $this->columnName;
97 2
            } elseif ($this->component->hasMethod("get{$this->columnName}")) {
98 1
                $method = "get{$this->columnName}";
99
            } else {
100 1
                throw new LogicException(
101 1
                    sprintf('Method, "%s" not found on "%s"', $this->columnName, $this->shortName)
102
                );
103
            }
104
105 3
            $value = $this->component->$method();
106
        } else {
107 6
            $value = $this->component->getValue();
108
        }
109
110 7
        if (!empty($this->columns)) {
111 1
            $this->cannotIdentifyException($this->component, $this->columns);
112
        }
113
114 6
        return $value;
115
    }
116
117
    /**
118
     * Resolves a DataObject value
119
     * @return mixed
120
     * @throws LogicException
121
     */
122 13
    protected function resolveDataObject()
123
    {
124 13
        if (empty($this->columnName)) {
125 2
            return $this->component->toMap();
126
        }
127
        // Inspect component for element $relation
128 12
        if ($this->component->hasMethod($this->columnName)) {
129 2
            return $this->getMethodValue();
130
        }
131
        // Inspect component has attribute
132 12
        if ($this->component->hasField($this->columnName)) {
133 11
            return $this->getFieldValue();
134
        }
135 5
        $this->cannotIdentifyException($this->component, [$this->columnName]);
1 ignored issue
show
Bug introduced by
It seems like $this->component can also be of type SilverStripe\ORM\FieldType\DBField; however, parameter $component of Firesphere\SolrSearch\Tr...nnotIdentifyException() does only seem to accept SilverStripe\ORM\DataObj...erStripe\View\ArrayData, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        $this->cannotIdentifyException(/** @scrutinizer ignore-type */ $this->component, [$this->columnName]);
Loading history...
136
    }
137
138
    /**
139
     * @return mixed
140
     */
141 2
    protected function getMethodValue()
142
    {
143 2
        $relation = $this->columnName;
144
        // We hit a direct method that returns a non-object
145 2
        if (!is_object($this->component->$relation())) {
146 1
            return $this->component->$relation();
147
        }
148
149 1
        return self::identify($this->component->$relation(), $this->columns);
150
    }
151
152
    /**
153
     * @return mixed
154
     */
155 11
    protected function getFieldValue()
156
    {
157 11
        $data = $this->component->{$this->columnName};
158 11
        $dbObject = $this->component->dbObject($this->columnName);
159 11
        if ($dbObject) {
160
            // @todo do we need to set the value?
161 8
            $dbObject->setValue($data);
162
163 8
            return self::identify($dbObject, $this->columns);
164
        }
165
166 3
        return $data;
167
    }
168
}
169