DataResolver   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 77
rs 10
c 1
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A identify() 0 16 3
A cannotIdentifyException() 0 7 1
1
<?php
2
/**
3
 * class DataResolver|Firesphere\SearchBackend\Helpers\DataResolver Identify content or relational content of a DataObject
4
 *
5
 * @package Firesphere\Search\Backend
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SearchBackend\Helpers;
11
12
use Firesphere\SearchBackend\Traits\IntrospectionTraits\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\Search\Backend
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
    public function __construct($component, $columns = [])
49
    {
50
        if (!is_array($columns)) {
51
            $columns = str_replace('.', '_', $columns);
52
            $columns = array_filter(explode('_', $columns));
53
        }
54
        $this->columns = $columns;
55
        $this->component = $component;
56
        $this->columnName = $this->columns ? array_shift($this->columns) : null;
57
        $this->shortName = ClassInfo::shortName($component);
58
    }
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
    public static function identify($obj, $columns = [])
70
    {
71
        /** @var {@link self::$objTypes} $type */
72
        foreach (self::$objTypes as $type => $method) {
73
            if ($obj instanceof $type) {
74
                $method = 'resolve' . $method;
75
76
                $self = new self($obj, $columns);
77
                $result = $self->{$method}();
78
                gc_collect_cycles();
79
80
                return $result;
81
            }
82
        }
83
84
        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
    protected function cannotIdentifyException($component, $columns = []): void
97
    {
98
        throw new LogicException(
99
            sprintf(
100
                'Cannot identify, "%s" from class "%s"',
101
                implode('.', $columns),
102
                ClassInfo::shortName($component)
103
            )
104
        );
105
    }
106
}
107