Passed
Push — hans/code-cleaning ( b3cc5f...fbec82 )
by Simon
04:59 queued 02:36
created

DataResolveTrait   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Test Coverage

Coverage 96.36%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 53
dl 0
loc 180
ccs 53
cts 55
cp 0.9636
rs 10
c 4
b 0
f 0
wmc 22

7 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveDataObject() 0 14 4
A resolveField() 0 15 3
A checkHasMethod() 0 13 3
A getMethodValue() 0 9 2
A resolveArrayData() 0 10 4
A resolveList() 0 18 4
A getFieldValue() 0 11 2
1
<?php
2
/**
3
 * Trait DataResolveTrait|Firesphere\SolrSearch\Traits\DataResolveTrait Used to extract methods from
4
 * the {@link \Firesphere\SolrSearch\Helpers\DataResolver} to make the code more readable
5
 *
6
 * @package Firesphere\SolrSearch\Traits
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
namespace Firesphere\SolrSearch\Traits;
12
13
use LogicException;
14
use SilverStripe\ORM\ArrayList;
15
use SilverStripe\ORM\DataObject;
16
use SilverStripe\ORM\FieldType\DBField;
17
use SilverStripe\ORM\SS_List;
18
use SilverStripe\View\ArrayData;
19
20
/**
21
 * Trait ResolveTrait All resolver methods for the DataResolver
22
 *
23
 * @package Firesphere\SolrSearch\Traits
24
 */
25
trait DataResolveTrait
26
{
27
    /**
28
     * Component to resolve
29
     *
30
     * @var DataObject|ArrayList|SS_List|DBField
31
     */
32
    protected $component;
33
    /**
34
     * Columns to resolve
35
     *
36
     * @var array
37
     */
38
    protected $columns = [];
39
    /**
40
     * Column to resolve
41
     *
42
     * @var mixed|string|null
43
     */
44
    protected $columnName = '';
45
    /**
46
     * ShortName of a class
47
     *
48
     * @var string
49
     */
50
    protected $shortName;
51
52
    /**
53
     * Resolves an ArrayData value
54
     *
55
     * @return mixed
56
     * @throws LogicException
57
     */
58 4
    protected function resolveArrayData()
59
    {
60 4
        if (empty($this->columnName)) {
61 1
            return $this->component->toMap();
62
        }
63
        // Inspect component has attribute
64 3
        if (empty($this->columns) && $this->component->hasField($this->columnName)) {
65 2
            return $this->component->{$this->columnName};
66
        }
67 1
        $this->cannotIdentifyException($this->component, array_merge([$this->columnName], $this->columns));
68
    }
69
70
    /**
71
     * Each class using this trait should have a way to throw unidentifiable objects or items
72
     *
73
     * @param DataObject|ArrayData|SS_List $component
74
     * @param array $columns
75
     *
76
     * @return void
77
     * @throws LogicException
78
     */
79
    abstract protected function cannotIdentifyException($component, $columns = []): void;
80
81
    /**
82
     * Resolves a DataList values
83
     *
84
     * @return array|mixed
85
     * @throws LogicException
86
     */
87 4
    protected function resolveList()
88
    {
89 4
        if (empty($this->columnName)) {
90 3
            return $this->component->toNestedArray();
91
        }
92
        // Inspect $component for element $relation
93 3
        if ($this->component->hasMethod($this->columnName)) {
94 2
            $relation = $this->columnName;
95
96 2
            return self::identify($this->component->$relation(), $this->columns);
97
        }
98 2
        $data = [];
99 2
        array_unshift($this->columns, $this->columnName);
100 2
        foreach ($this->component as $component) {
101 2
            $data[] = self::identify($component, $this->columns);
102
        }
103
104 2
        return $data;
105
    }
106
107
    /**
108
     * Resolves a Single field in the database.
109
     *
110
     * @return mixed
111
     * @throws LogicException
112
     */
113 11
    protected function resolveField()
114
    {
115 11
        if ($this->columnName) {
116 4
            $method = $this->checkHasMethod();
117
118 3
            $value = $this->component->$method();
119
        } else {
120 9
            $value = $this->component->getValue();
121
        }
122
123 10
        if (!empty($this->columns)) {
124 1
            $this->cannotIdentifyException($this->component, $this->columns);
125
        }
126
127 9
        return $value;
128
    }
129
130
    /**
131
     * Check if a component has the method instead of it being a property
132
     *
133
     * @return null|mixed|string
134
     * @throws LogicException
135
     */
136 4
    protected function checkHasMethod()
137
    {
138 4
        if ($this->component->hasMethod($this->columnName)) {
139 3
            $method = $this->columnName;
140 2
        } elseif ($this->component->hasMethod("get{$this->columnName}")) {
141 1
            $method = "get{$this->columnName}";
142
        } else {
143 1
            throw new LogicException(
144 1
                sprintf('Method, "%s" not found on "%s"', $this->columnName, $this->shortName)
145
            );
146
        }
147
148 3
        return $method;
149
    }
150
151
    /**
152
     * Resolves a DataObject value
153
     *
154
     * @return mixed
155
     * @throws LogicException
156
     */
157 16
    protected function resolveDataObject()
158
    {
159 16
        if (empty($this->columnName)) {
160 2
            return $this->component->toMap();
161
        }
162
        // Inspect component for element $relation
163 15
        if ($this->component->hasMethod($this->columnName)) {
164 3
            return $this->getMethodValue();
165
        }
166
        // Inspect component has attribute
167 15
        if ($this->component->hasField($this->columnName)) {
168 14
            return $this->getFieldValue();
169
        }
170 7
        $this->cannotIdentifyException($this->component, [$this->columnName]);
171
    }
172
173
    /**
174
     * Get the value for a method
175
     *
176
     * @return mixed
177
     */
178 3
    protected function getMethodValue()
179
    {
180 3
        $relation = $this->columnName;
181
        // We hit a direct method that returns a non-object
182 3
        if (!is_object($this->component->$relation())) {
183 1
            return $this->component->$relation();
184
        }
185
186 2
        return self::identify($this->component->$relation(), $this->columns);
187
    }
188
189
    /**
190
     * Get the value for a field
191
     *
192
     * @return mixed
193
     */
194 14
    protected function getFieldValue()
195
    {
196 14
        $data = $this->component->{$this->columnName};
197 14
        $dbObject = $this->component->dbObject($this->columnName);
198 14
        if ($dbObject) {
199 11
            $dbObject->setValue($data);
200
201 11
            return self::identify($dbObject, $this->columns);
202
        }
203
204 3
        return $data;
205
    }
206
}
207