Passed
Push — hans/fluent ( f3668d...f73a01 )
by Simon
05:56 queued 03:33
created

DataResolveTrait::resolveList()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.9332
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
 * @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();
1 ignored issue
show
Bug introduced by
The method toMap() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Filterable or SilverStripe\ORM\Relation or SilverStripe\ORM\Limitable or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation. Are you sure you never get one of those? ( Ignorable by Annotation )

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

43
            return $this->component->/** @scrutinizer ignore-call */ toMap();
Loading history...
44
        }
45
        // Inspect component has attribute
46 3
        if (empty($this->columns) && $this->component->hasField($this->columnName)) {
1 ignored issue
show
Bug introduced by
The method hasField() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Filterable or SilverStripe\ORM\Relation or SilverStripe\ORM\Limitable or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation. Are you sure you never get one of those? ( Ignorable by Annotation )

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

46
        if (empty($this->columns) && $this->component->/** @scrutinizer ignore-call */ hasField($this->columnName)) {
Loading history...
47 2
            return $this->component->{$this->columnName};
48
        }
49 1
        $this->cannotIdentifyException($this->component, array_merge([$this->columnName], $this->columns));
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

49
        $this->cannotIdentifyException(/** @scrutinizer ignore-type */ $this->component, array_merge([$this->columnName], $this->columns));
Loading history...
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)) {
1 ignored issue
show
Bug introduced by
The method hasMethod() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Filterable or SilverStripe\ORM\Relation or SilverStripe\ORM\Limitable or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation. Are you sure you never get one of those? ( Ignorable by Annotation )

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

72
        if ($this->component->/** @scrutinizer ignore-call */ hasMethod($this->columnName)) {
Loading history...
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();
1 ignored issue
show
Bug introduced by
The method getValue() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Filterable or SilverStripe\ORM\Relation or SilverStripe\ORM\Limitable or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation or SilverStripe\ORM\Relation. Are you sure you never get one of those? ( Ignorable by Annotation )

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

107
            /** @scrutinizer ignore-call */ 
108
            $value = $this->component->getValue();
Loading history...
108
        }
109
110 7
        if (!empty($this->columns)) {
111 1
            $this->cannotIdentifyException($this->component, $this->columns);
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

111
            $this->cannotIdentifyException(/** @scrutinizer ignore-type */ $this->component, $this->columns);
Loading history...
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
            $relation = $this->columnName;
130
            // We hit a direct method that returns a non-object
131 2
            if (!is_object($this->component->$relation())) {
132 1
                return $this->component->$relation();
133
            }
134
135 1
            return self::identify($this->component->$relation(), $this->columns);
136
        }
137
        // Inspect component has attribute
138 12
        if ($this->component->hasField($this->columnName)) {
139 11
            $data = $this->component->{$this->columnName};
140 11
            $dbObject = $this->component->dbObject($this->columnName);
1 ignored issue
show
Bug introduced by
The method dbObject() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Filterable or SilverStripe\ORM\Limitable. Are you sure you never get one of those? ( Ignorable by Annotation )

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

140
            /** @scrutinizer ignore-call */ 
141
            $dbObject = $this->component->dbObject($this->columnName);
Loading history...
141 11
            if ($dbObject) {
142
                // @todo do we need to set the value?
143 8
                $dbObject->setValue($data);
144
145 8
                return self::identify($dbObject, $this->columns);
146
            }
147
148 3
            return $data;
149
        }
150 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

150
        $this->cannotIdentifyException(/** @scrutinizer ignore-type */ $this->component, [$this->columnName]);
Loading history...
151
    }
152
}
153