Passed
Push — master ( a5593b...e12b6b )
by Nicolaas
02:15
created

field_showing_fields_to_send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * this can be linked to pages / other objects using many_many relationships
5
 * so that you can send default record values to Salesforce
6
 */
7
class SalesForceDefaultContactField extends DataObject
8
{
9
10
11
12
13
    /**
14
     * Needs to link to a many-many relationship (SalesforceDefaultContactFields)
15
     * @param  array $array fields to send
16
     *
17
     * @return CheckboxSetField
18
     */
19
    public static function select_field(
20
        $fieldName = 'SalesforceDefaultContactFields',
21
        $title = 'Default Fields'
22
    )
23
    {
24
        $htmlArray = [];
25
        foreach($array as $field => $value) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $array seems to be never defined.
Loading history...
26
            $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')';
27
        }
28
29
        return CheckboxSetField::create(
30
            $fieldName,
31
            $title,
32
            SalesForceDefaultContactField::get()->map->toArray()
0 ignored issues
show
Bug introduced by
The method toArray() does not exist on null. ( Ignorable by Annotation )

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

32
            SalesForceDefaultContactField::get()->map->/** @scrutinizer ignore-call */ 
33
                                                       toArray()

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...
33
        )
34
            ->setDescription('
35
                These default fields are always sent to Salesforce, you can
36
                <a href="/admin/salesforceadmin">Edit or Add to the List</a> as required.
37
                Please change with care.
38
            ')
39
        ;
40
    }
41
42
    /**
43
     *
44
     * @param  array $array fields to send
45
     *
46
     * @return LiteralField
47
     */
48
    public static function field_showing_fields_to_send($array)
49
    {
50
        $htmlArray = [];
51
        foreach($array as $field => $value) {
52
            $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')';
53
        }
54
55
        return LiteralField::create(
56
            'FieldsToSendToSalesforce',
57
            '<h2>Fields Added:</h2><p>'.implode('</p><p>', $htmlArray).'</p>'
58
        );
59
    }
60
61
62
    /**
63
     * Singular name for CMS
64
     * @var string
65
     */
66
    private static $singular_name = 'Default Contact Field';
1 ignored issue
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
67
68
    /**
69
     * Plural name for CMS
70
     * @var string
71
     */
72
    private static $plural_name = 'Default Contact Fields';
1 ignored issue
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
73
74
    /**
75
     *
76
     * @var array
77
     */
78
    private static $db = [
1 ignored issue
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
79
        'Key' => 'Varchar',
80
        'Value' => 'Varchar'
81
    ];
82
83
    /**
84
     * Defines summary fields commonly used in table columns
85
     * as a quick overview of the data for this dataobject
86
     * @var array
87
     */
88
    private static $summary_fields = [
1 ignored issue
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
89
        'Key' => 'Field Name',
90
        'Value' => 'Field Value'
91
    ];
92
93
    /**
94
     *
95
     * @return string
96
     */
97
    public function getTitle()
98
    {
99
        return $this->Key . ' - '.$this->Value;
1 ignored issue
show
Bug Best Practice introduced by
The property Key does not exist on SalesForceDefaultContactField. Since you implemented __get, consider adding a @property annotation.
Loading history...
100
    }
101
102
    /**
103
     * contact fields that should be created by default...
104
     * @var array
105
     */
106
    private static $defaults_records = [];
1 ignored issue
show
introduced by
The private property $defaults_records is not used, and could be removed.
Loading history...
107
108
    public function requireDefaultRecords()
109
    {
110
        foreach($this->Config()->get('default_records') as $key => $value) {
111
            $filter = [
112
                'Key' => $key
113
            ];
114
115
            $obj = SalesForceDefaultContactField::get()->filter($filter)->first();
116
            if(! $obj) {
117
                $obj->create($filter);
118
                $obj->Value = $value;
119
                $obj->write();
120
            }
121
122
        }
123
    }
124
125
    /**
126
     * @return mixed
127
     */
128
    public function BetterValue()
129
    {
130
        if(strtolower($this->Value) === 'true') {
131
            return true;
132
        }
133
        if(strtolower($this->Value) === 'false') {
134
            return false;
135
        }
136
        if(floatval($this->Value)) {
137
            return floatval($this->Value);
138
        }
139
        if(intval($this->Value)) {
140
            return intval($this->Value);
141
        }
142
    }
143
144
145
146
}
147