Passed
Push — sheepy/elevation-configuration ( bdeab0...bcf7bc )
by Marco
18:34
created

DataResolveTrait   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 19
eloc 49
c 3
b 0
f 0
dl 0
loc 136
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveArrayData() 0 10 4
A resolveList() 0 18 4
A resolveDataObject() 0 29 6
A resolveField() 0 24 5
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
    protected function resolveArrayData()
41
    {
42
        if (empty($this->columnName)) {
43
            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
        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
            return $this->component->{$this->columnName};
48
        }
49
        $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
     * Resolves a DataList values
54
     * @return array|mixed
55
     * @throws LogicException
56
     */
57
    protected function resolveList()
58
    {
59
        if (empty($this->columnName)) {
60
            return $this->component->toNestedArray();
61
        }
62
        // Inspect $component for element $relation
63
        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

63
        if ($this->component->/** @scrutinizer ignore-call */ hasMethod($this->columnName)) {
Loading history...
64
            $relation = $this->columnName;
65
66
            return self::identify($this->component->$relation(), $this->columns);
67
        }
68
        $data = [];
69
        array_unshift($this->columns, $this->columnName);
70
        foreach ($this->component as $component) {
71
            $data[] = self::identify($component, $this->columns);
72
        }
73
74
        return $data;
75
    }
76
77
    /**
78
     * Resolves a Single field in the database.
79
     * @return mixed
80
     * @throws LogicException
81
     */
82
    protected function resolveField()
83
    {
84
        if ($this->columnName) {
85
            // @todo could be simplified if possible?
86
            if ($this->component->hasMethod($this->columnName)) {
87
                $method = $this->columnName;
88
            } elseif ($this->component->hasMethod("get{$this->columnName}")) {
89
                $method = "get{$this->columnName}";
90
            } else {
91
                throw new LogicException(
92
                    sprintf('Method, "%s" not found on "%s"', $this->columnName, $this->shortName)
93
                );
94
            }
95
96
            $value =  $this->component->$method();
97
        } else {
98
            $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

98
            /** @scrutinizer ignore-call */ 
99
            $value = $this->component->getValue();
Loading history...
99
        }
100
101
        if (!empty($this->columns)) {
102
            $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

102
            $this->cannotIdentifyException(/** @scrutinizer ignore-type */ $this->component, $this->columns);
Loading history...
103
        }
104
105
        return $value;
106
    }
107
108
    /**
109
     * Resolves a DataObject value
110
     * @return mixed
111
     * @throws LogicException
112
     */
113
    protected function resolveDataObject()
114
    {
115
        if (empty($this->columnName)) {
116
            return $this->component->toMap();
117
        }
118
        // Inspect component for element $relation
119
        if ($this->component->hasMethod($this->columnName)) {
120
            $relation = $this->columnName;
121
            // We hit a direct method that returns a non-object
122
            if (!is_object($this->component->$relation())) {
123
                return $this->component->$relation();
124
            }
125
126
            return self::identify($this->component->$relation(), $this->columns);
127
        }
128
        // Inspect component has attribute
129
        if ($this->component->hasField($this->columnName)) {
130
            $data = $this->component->{$this->columnName};
131
            $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

131
            /** @scrutinizer ignore-call */ 
132
            $dbObject = $this->component->dbObject($this->columnName);
Loading history...
132
            if ($dbObject) {
133
                // @todo do we need to set the value?
134
                $dbObject->setValue($data);
135
136
                return self::identify($dbObject, $this->columns);
137
            }
138
139
            return $data;
140
        }
141
        $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

141
        $this->cannotIdentifyException(/** @scrutinizer ignore-type */ $this->component, [$this->columnName]);
Loading history...
142
    }
143
144
    /**
145
     * @param DataObject|ArrayData|SS_List $component
146
     * @param array $columns
147
     *
148
     * @return void
149
     * @throws LogicException
150
     */
151
    abstract protected function cannotIdentifyException($component, $columns = []): void;
152
}
153