Passed
Push — master ( f487dc...584555 )
by Nicolaas
02:26
created

SalesforceDefaultContactField::getCMSFields()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 28
c 2
b 0
f 0
nc 6
nop 0
dl 0
loc 39
rs 8.8497
1
<?php
2
3
use SilverStripe\Forms\DropdownField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, DropdownField. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
The type SilverStripe\Forms\DropdownField 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...
4
/**
5
 * this can be linked to pages / other objects using many_many relationships
6
 * so that you can send default record values to Salesforce.
7
 * It is NOT for user specific ones, but for generic fields.
8
 *
9
 * This class knows what format salesforce expects.
10
 */
11
class SalesforceDefaultContactField extends DataObject
12
{
13
14
    /**
15
     * contact fields that should be created by default...
16
     * @var array
17
     */
18
    private static $defaults_records = [];
19
20
    /**
21
     * Singular name for CMS
22
     * @var string
23
     */
24
    private static $singular_name = 'Default Contact Field';
25
26
    /**
27
     * Plural name for CMS
28
     * @var string
29
     */
30
    private static $plural_name = 'Default Contact Fields';
31
32
    /**
33
     *
34
     * @var array
35
     */
36
    private static $db = [
37
        'Key' => 'Varchar',
38
        'Value' => 'Varchar',
39
        'ValueType' => 'Enum("String,Number,YesOrTrue,NoOrFalse,CurrentDate,CurrentDateAndTime", "String")',
40
    ];
41
42
    /**
43
     * Defines summary fields commonly used in table columns
44
     * as a quick overview of the data for this dataobject
45
     * @var array
46
     */
47
    private static $summary_fields = [
48
        'Key' => 'Field Name',
49
        'Value' => 'Field Value',
50
        'ValueType' => 'Value Type',
51
    ];
52
    /**
53
     * Defines summary fields commonly used in table columns
54
     * as a quick overview of the data for this dataobject
55
     * @var array
56
     */
57
    /**
58
     * Defines a default list of filters for the search context
59
     * @var array
60
     */
61
    private static $searchable_fields = [
62
        'Key',
63
        'Value',
64
        'ValueType',
65
    ];
66
67
    /**
68
     *
69
     * @return string
70
     */
71
    public function getTitle()
72
    {
73
        return $this->Key . ' = '.$this->BetterValueHumanReadable() . ' ('.$this->ValueType.')';
74
    }
75
76
77
    public function requireDefaultRecords()
78
    {
79
        foreach($this->Config()->get('default_records') as $key => $value) {
80
            $filter = [
81
                'Key' => $key
82
            ];
83
84
            $obj = SalesforceDefaultContactField::get()->filter($filter)->first();
85
            if(! $obj) {
86
                $obj = SalesforceDefaultContactField::create($filter);
87
                $obj->Value = $value;
88
                $obj->write();
89
            }
90
91
        }
92
    }
93
94
    /**
95
     * @return mixed
96
     */
97
    public function BetterValue()
98
    {
99
        switch($this->ValueType) {
100
            case 'Number':
101
                return floatval($this->Value);
102
            case 'CurrentDate':
103
                return Date('Y-m-d');
104
            case 'CurrentDateAndTime':
105
                return Date('Y-m-d h:i:s');
106
            case 'YesOrTrue':
107
                return 'true';
108
            case 'NoOrFalse':
109
                return 'false';
110
            default:
111
                return trim($this->Value);
112
        }
113
    }
114
115
    /**
116
     * @return mixed
117
     */
118
    public function BetterValueHumanReadable()
119
    {
120
        return $this->BetterValue();
121
    }
122
123
    /**
124
     * CMS Fields
125
     * @return FieldList
126
     */
127
    public function getCMSFields()
128
    {
129
        $fields = parent::getCMSFields();
130
        $fields->addFieldsToTab(
131
            'Root.Main',
132
            [
133
                TextField::create(
134
                    'Value',
135
                    'Value'
136
                ),
137
                DropdownField::create(
138
                    'ValueType',
139
                    'Type / Predefined Value',
140
                    $this->dbObject('ValueType')->enumValues()
141
                ),
142
                ReadonlyField::create(
143
                    'BetterValueHumanReadableNice',
144
                    'Calculated Value',
145
                    $this->BetterValueHumanReadable()
146
                ),
147
            ]
148
        );
149
        switch($this->ValueType) {
150
            case 'CurrentDate':
151
            case 'CurrentDateAndTime':
152
            case 'YesOrTrue':
153
            case 'NoOrFalse':
154
                return $fields->removeFieldFromTab(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $fields->removeFieldFromTab('Root.Main', 'Value') targeting FieldList::removeFieldFromTab() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
155
                    'Root.Main',
156
                    'Value'
157
                );
158
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
159
            case 'Number':
160
                return $fields->removeFieldFromTab(
0 ignored issues
show
Bug introduced by
Are you sure the usage of $fields->removeFieldFrom...eate('Value', 'Value')) targeting FieldList::removeFieldFromTab() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
161
                    'Root.Main',
162
                    NumericField::create('Value', 'Value')
163
                );
164
        }
165
        return $fields;
166
    }
167
168
169
}
170