getAbsoluteURL()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 2
nc 1
nop 0
1
<?php
2
3
namespace PhpTek\Exodus\Model;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Forms\ReadonlyField;
7
use SilverStripe\ORM\FieldType\DBText;
8
use SilverStripe\ORM\FieldType\DBVarchar;
9
use SilverStripe\Forms\DropdownField;
10
use SilverStripe\ORM\FieldType\DBBoolean;
11
use SilverStripe\ORM\DataObjectSchema;
12
13
/**
14
 * A single import rule that forms part of an ImportSchema
15
 */
16
class StaticSiteContentSourceImportRule extends DataObject
17
{
18
    /**
19
     *
20
     * @var array
21
     */
22
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
23
        "FieldName" => DBVarchar::class,
24
        "CSSSelector" => DBVarchar::class,
25
        "ExcludeCSSSelector" => DBText::class,
26
        "Attribute" => DBVarchar::class,
27
        "PlainText" => DBBoolean::class,
28
        "OuterHTML" => DBBoolean::class,
29
    ];
30
31
    /**
32
     * @var string
33
     */
34
    private static $table_name = 'StaticSiteContentSourceImportRule';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
35
36
    /**
37
     *
38
     * @var array
39
     */
40
    private static $summary_fields = [
0 ignored issues
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
41
        "FieldName",
42
        "PlainText",
43
        "OuterHTML",
44
    ];
45
46
    /**
47
     *
48
     * @var array
49
     */
50
    private static $field_labels = [
0 ignored issues
show
introduced by
The private property $field_labels is not used, and could be removed.
Loading history...
51
        "FieldName" => "Target Field Name",
52
        "CSSSelector" => "CSS Selector",
53
        'ExcludeCSSSelector' => 'Excluded CSS Selector(s)',
54
        "Attribute" => "Element Attribute",
55
        "PlainText" => "Plain Text",
56
        "OuterHTML" => "Outer HTML",
57
    ];
58
59
    /**
60
     *
61
     * @var array
62
     */
63
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
64
        "Schema" => StaticSiteContentSourceImportSchema::class,
65
    ];
66
67
    /**
68
     *
69
     * @return string
70
     */
71
    public function getTitle()
72
    {
73
        return $this->FieldName ?: $this->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property FieldName does not exist on PhpTek\Exodus\Model\Stat...ContentSourceImportRule. Since you implemented __get, consider adding a @property annotation.
Loading history...
74
    }
75
76
    /**
77
     *
78
     * @return string
79
     */
80
    public function getAbsoluteURL()
81
    {
82
        return $this->URLSegment ?: $this->Filename;
0 ignored issues
show
Bug Best Practice introduced by
The property URLSegment does not exist on PhpTek\Exodus\Model\Stat...ContentSourceImportRule. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property Filename does not exist on PhpTek\Exodus\Model\Stat...ContentSourceImportRule. Since you implemented __get, consider adding a @property annotation.
Loading history...
83
    }
84
85
    /**
86
     *
87
     * @return FieldList
0 ignored issues
show
Bug introduced by
The type PhpTek\Exodus\Model\FieldList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
88
     */
89
    public function getCMSFields()
90
    {
91
        $fields = parent::getCMSFields();
92
        $dataType = $this->Schema()->DataType;
0 ignored issues
show
Bug introduced by
The method Schema() does not exist on PhpTek\Exodus\Model\Stat...ContentSourceImportRule. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

92
        $dataType = $this->/** @scrutinizer ignore-call */ Schema()->DataType;
Loading history...
93
94
        if ($dataType) {
95
            $fieldList = singleton(DataObjectSchema::class)
96
                ->fieldSpecs($dataType, DataObjectSchema::DB_ONLY);
97
            $fieldList = array_combine(array_keys($fieldList), array_keys($fieldList));
98
            foreach (
99
                array_merge(
100
                    array_keys(DataObject::config()->get('fixed_fields')),
101
                    array_filter($fieldList, function ($item) {
102
                        return preg_match('#(^Static|ID$)#i', $item);
103
                    }),
104
                    ['Version']
105
                ) as $exclusion
106
            ) {
107
                unset($fieldList[$exclusion]);
108
            }
109
110
            natsort($fieldList);
111
112
            $fieldNameField = DropdownField::create("FieldName", 'Target Field', $fieldList)
113
                ->setEmptyString("(choose)")
114
                ->setDescription('Remote content matched by the CSS selector below is written to this field.');
115
            $fields->insertBefore($fieldNameField, 'CSSSelector');
0 ignored issues
show
Bug introduced by
'CSSSelector' of type string is incompatible with the type SilverStripe\Forms\FormField expected by parameter $item of SilverStripe\Forms\FieldList::insertBefore(). ( Ignorable by Annotation )

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

115
            $fields->insertBefore($fieldNameField, /** @scrutinizer ignore-type */ 'CSSSelector');
Loading history...
116
            $fields->dataFieldByName('CSSSelector')
117
                ->setDescription('A valid CSS selector whose content is written to the "Target Field" above.')
118
                ->setAttribute('style', 'width: 300px;');
119
            $fields->dataFieldByName('ExcludeCSSSelector')
120
                ->setDescription('A list of valid CSS selectors whose content'
121
                . ' should be ignored. This is useful for fine-tuning what is returned in an import.'
122
                . ' Separate multiple exclusions with a newline.');
123
            $fields->dataFieldByName('OuterHTML')
124
                ->setDescription('Use outer HTML (Fetches parent element and content and that of its children)');
125
            $fields->dataFieldByName('PlainText')
126
                ->setDescription('Convert to plain text (Removes markup)');
127
        } else {
128
            $fields->replaceField('FieldName', $fieldName = ReadonlyField::create("FieldName", "Field Name"));
129
            $fieldName->setDescription('Save this rule before being able to add a field name');
130
        }
131
132
        $fields->dataFieldByName('Attribute')->setDescription('Add the element attribute where the desired text'
133
        . ' can be found (such as "alt" or "title") if not found as the selected element\'s text itself.');
134
135
        return $fields;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $fields returns the type SilverStripe\Forms\FieldList which is incompatible with the documented return type PhpTek\Exodus\Model\FieldList.
Loading history...
136
    }
137
}
138