DataResolver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
c 3
b 0
f 0
dl 0
loc 77
ccs 24
cts 24
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A cannotIdentifyException() 0 7 1
A __construct() 0 10 3
A identify() 0 16 3
1
<?php
2
/**
3
 * class DataResolver|Firesphere\SolrSearch\Helpers\DataResolver Identify content or relational content of a DataObject
4
 *
5
 * @package Firesphere\Solr\Search
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SolrSearch\Helpers;
11
12
use Firesphere\SolrSearch\Traits\DataResolveTrait;
13
use LogicException;
14
use SilverStripe\Core\ClassInfo;
15
use SilverStripe\ORM\ArrayList;
16
use SilverStripe\ORM\DataObject;
17
use SilverStripe\ORM\FieldType\DBField;
18
use SilverStripe\ORM\SS_List;
19
use SilverStripe\View\ArrayData;
20
21
/**
22
 * Class DataResolver
23
 *
24
 * @package Firesphere\Solr\Search
25
 */
26
class DataResolver
27
{
28
    use DataResolveTrait;
29
30
    /**
31
     * Supported object types
32
     *
33
     * @var array map of objects to methods
34
     */
35
    private static $objTypes = [
36
        DataObject::class => 'DataObject',
37
        ArrayData::class  => 'ArrayData',
38
        SS_List::class    => 'List',
39
        DBField::class    => 'Field',
40
    ];
41
42
    /**
43
     * DataResolver constructor.
44
     *
45
     * @param DataObject|ArrayList|SS_List|DBField $component
46
     * @param array|string $columns
47
     */
48 21
    public function __construct($component, $columns = [])
49
    {
50 21
        if (!is_array($columns)) {
51 19
            $columns = str_replace('.', '_', $columns);
52 19
            $columns = array_filter(explode('_', $columns));
53
        }
54 21
        $this->columns = $columns;
55 21
        $this->component = $component;
56 21
        $this->columnName = $this->columns ? array_shift($this->columns) : null;
57 21
        $this->shortName = ClassInfo::shortName($component);
58 21
    }
59
60
    /**
61
     * Identify the given object's columns
62
     *
63
     * @param DataObject|ArrayData|SS_List|DBField $obj
64
     * @param array|string $columns
65
     *
66
     * @return mixed
67
     * @throws LogicException
68
     */
69 22
    public static function identify($obj, $columns = [])
70
    {
71
        /** @var {@link self::$objTypes} $type */
72 22
        foreach (self::$objTypes as $type => $method) {
73 22
            if ($obj instanceof $type) {
74 21
                $method = 'resolve' . $method;
75
76 21
                $self = new self($obj, $columns);
77 21
                $result = $self->{$method}();
78 17
                gc_collect_cycles();
79
80 21
                return $result;
81
            }
82
        }
83
84 1
        throw new LogicException(sprintf('Class: %s is not supported.', ClassInfo::shortName($obj)));
85
    }
86
87
    /**
88
     * An error occured, so log it
89
     *
90
     * @param DataObject|ArrayData|SS_List $component
91
     * @param array $columns
92
     *
93
     * @return void
94
     * @throws LogicException
95
     */
96 9
    protected function cannotIdentifyException($component, $columns = []): void
97
    {
98 9
        throw new LogicException(
99 9
            sprintf(
100 9
                'Cannot identify, "%s" from class "%s"',
101 9
                implode('.', $columns),
102 9
                ClassInfo::shortName($component)
103
            )
104
        );
105
    }
106
}
107