Passed
Push — master ( 7c40b0...1b7f17 )
by Matthew
02:45
created

LocationExtension::updateCMSFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Dynamic\Locator\React\Extensions;
4
5
use Dynamic\Locator\Location;
6
use SilverStripe\AssetAdmin\Forms\UploadField;
7
use SilverStripe\Assets\Image;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
11
use SilverStripe\ORM\DataExtension;
12
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
13
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
14
15
/**
16
 * Class LocationExtension
17
 * @package Dynamic\Locator\React\Extensions
18
 *
19
 * @property int MarkerIconImageID
20
 * @method Image MarkerIconImage()
21
 *
22
 * @property-read Location|LocationExtension $owner
23
 */
24
class LocationExtension extends DataExtension
25
{
26
27
    /**
28
     * @var array
29
     */
30
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
31
        'MarkerIconImage' => Image::class,
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
38
        'MarkerIconImage',
39
    ];
40
41
    /**
42
     * @var array
43
     */
44
    private static $many_many_extraFields = [
0 ignored issues
show
introduced by
The private property $many_many_extraFields is not used, and could be removed.
Loading history...
45
        'Categories' => [
46
            'Sort' => 'Int',
47
        ],
48
    ];
49
50
    /**
51
     * @param FieldList $fields
52
     */
53
    public function updateCMSFields(FieldList $fields)
54
    {
55
        parent::updateCMSFields($fields);
56
57
        if ($this->owner->ID) {
0 ignored issues
show
Bug Best Practice introduced by
The property ID does not exist on Dynamic\Locator\React\Extensions\LocationExtension. Did you maybe forget to declare it?
Loading history...
58
59
            /** @var GridField $categoryField */
60
            $categoryField = $fields->dataFieldByName('Categories');
61
            $categoryField->getConfig()->removeComponentsByType([
62
                GridFieldSortableHeader::class,
63
            ])->addComponents([
64
                new GridFieldOrderableRows(),
65
                new GridFieldTitleHeader(),
66
            ]);
67
        }
68
    }
69
70
    /**
71
     * @return Image|string
72
     */
73
    public function getMarkerIcon()
74
    {
75
        if ($this->owner->MarkerIconImageID) {
76
            return $this->owner->MarkerIconImage()->getURL();
77
        }
78
79
        if ($this->owner->Categories()->count()) {
0 ignored issues
show
Bug introduced by
The method Categories() does not exist on Dynamic\Locator\React\Extensions\LocationExtension. ( Ignorable by Annotation )

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

79
        if ($this->owner->/** @scrutinizer ignore-call */ Categories()->count()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
            $icon = $this->owner->Categories()->sort('Sort')->first()->MarkerIconImage();
81
            return $icon->getURL();
82
        }
83
84
        $imageURL = '';
85
        $this->owner->extend('updateMarkerIconURL', $imageURL);
0 ignored issues
show
Bug introduced by
The method extend() does not exist on Dynamic\Locator\React\Extensions\LocationExtension. ( Ignorable by Annotation )

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

85
        $this->owner->/** @scrutinizer ignore-call */ 
86
                      extend('updateMarkerIconURL', $imageURL);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        return $imageURL;
87
    }
88
}
89