Passed
Push — hans/elevation ( a01aee...e75503 )
by Simon
06:49
created

Elevation   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
eloc 22
c 3
b 0
f 0
dl 0
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 16 2
A onBeforeWrite() 0 4 1
1
<?php
2
3
namespace Firesphere\SolrSearch\Models;
4
5
use Firesphere\SolrSearch\Indexes\BaseIndex;
6
use Firesphere\SolrSearch\Services\SolrCoreService;
7
use ReflectionException;
8
use SilverStripe\Forms\DropdownField;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\ORM\ManyManyList;
12
13
/**
14
 * Class \Firesphere\SolrSearch\Models\Elevation
15
 *
16
 * @property string $Keyword
17
 * @property string $Index
18
 * @method ManyManyList|ElevatedItem[] Items()
19
 */
20
class Elevation extends DataObject
21
{
22
    /**
23
     * @var string Table name
24
     */
25
    private static $table_name = 'Elevation';
26
27
    /**
28
     * @var array Database column
29
     */
30
    private static $db = [
31
        'Keyword' => 'Varchar(255)',
32
        'Index'   => 'Varchar(255)',
33
    ];
34
35
    /**
36
     * @var array Database relations
37
     */
38
    private static $many_many = [
39
        'Items' => ElevatedItem::class,
40
    ];
41
42
    /**
43
     * @var array Fields to show in the CMS
44
     */
45
    private static $summary_fields = [
46
        'ID',
47
        'Keyword',
48
        'Index',
49
        'Items.Count'
50
    ];
51
52
    /**
53
     * Set up CMS fields
54
     * @todo Unit test this
55
     * @return FieldList
56
     * @throws ReflectionException
57
     * n
58
     */
59
    public function getCMSFields()
60
    {
61
        $fields = parent::getCMSFields();
62
        $indexes = (new SolrCoreService())->getValidIndexes();
63
        $indexList = [];
64
        /** @var BaseIndex $index */
65
        foreach ($indexes as $index) {
66
            $indexList[$index] = (new $index())->getIndexName();
67
        }
68
69
        $fields->addFieldToTab(
70
            'Root.Main',
71
            DropdownField::create('Index', _t(self::class . '.INDEXNAME', 'Solr core'), $indexList)
72
        );
73
74
        return $fields;
75
    }
76
77
    public function onBeforeWrite()
78
    {
79
        // @todo upload XML with Elevation instructions to Solr
80
        parent::onBeforeWrite();
81
    }
82
}
83