Passed
Push — master ( e12b6b...5d580f )
by Nicolaas
02:13
created

SalesForceDefaultContactField::mixed_to_array()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
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
     * contact fields that should be created by default...
12
     * @var array
13
     */
14
    private static $defaults_records = [];
1 ignored issue
show
introduced by
The private property $defaults_records is not used, and could be removed.
Loading history...
15
16
    private static $site_wide_fields_to_send = [];
1 ignored issue
show
introduced by
The private property $site_wide_fields_to_send is not used, and could be removed.
Loading history...
17
18
    private static $site_wide_filter_values = [];
1 ignored issue
show
introduced by
The private property $site_wide_filter_values is not used, and could be removed.
Loading history...
19
20
    /**
21
     * Needs to link to a many-many relationship (SalesforceDefaultContactFields)
22
     * @param  array $array fields to send
23
     *
24
     * @return CheckboxSetField
25
     */
26
    public static function select_field(
27
        $fieldName = 'SalesforceDefaultContactFields',
28
        $title = 'Default Fields'
29
    )
30
    {
31
        $htmlArray = [];
32
        foreach($array as $field => $value) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $array seems to be never defined.
Loading history...
33
            $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')';
34
        }
35
36
        return CheckboxSetField::create(
37
            $fieldName,
38
            $title,
39
            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

39
            SalesForceDefaultContactField::get()->map->/** @scrutinizer ignore-call */ 
40
                                                       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...
40
        )
41
            ->setDescription('
42
                These default fields are always sent to Salesforce, you can
43
                <a href="/admin/salesforceadmin">Edit or Add to the List</a> as required.
44
                Please change with care.
45
            ')
46
        ;
47
    }
48
49
    /**
50
     *
51
     * @param  array|DataList|null $array fields to send
52
     * @param  string $title fields to send
53
     *
54
     * @return LiteralField
55
     */
56
    public static function fields_to_send_field(
57
        $mixed = null,
58
        $title  = 'Fields Added'
59
    )
60
    {
61
        $array = self::get_fields_to_send($mixed);
62
        $htmlArray = [];
63
        foreach($array as $field => $value) {
64
            $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')';
65
        }
66
        if(!count($htmlArray) == 0) {
67
            $htmlArray[] = 'none';
68
        }
69
        return LiteralField::create(
70
            'FieldsToSendToSalesforce',
71
            '<h2>'.$title.'</h2><p>- '.implode('</p><p>', $htmlArray).'</p>'
72
        );
73
    }
74
    /**
75
     *
76
     * @param  array|DataList|null $array fields to send
77
     * @param  string $title fields to send
78
     *
79
     * @return LiteralField
80
     */
81
    public static function fields_to_filter_field(
82
        $mixed = null,
83
        $title  = 'Filter Fields'
84
    )
85
    {
86
        $array = self::get_fields_to_send($mixed);
87
        $htmlArray = [];
88
        foreach($array as $field => $value) {
89
            $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')';
90
        }
91
        if(!count($htmlArray) == 0) {
92
            $htmlArray[] = 'none';
93
        }
94
        return LiteralField::create(
95
            'FieldsToFilterForSalesforce',
96
            '<h2>'.$title.'</h2><p>- '.implode('</p><p>', $htmlArray).'</p>'
97
        );
98
    }
99
100
    /**
101
     *
102
     * @param  array|DataList|null $mixed fields to send
103
     *
104
     * @return array
105
     */
106
    public static function get_fields_to_send($mixed = null)
107
    {
108
        $array = self::mixed_to_array($mixed);
109
110
        return array_merge(
111
            Config::inst()->get('SalesForceDefaultContactField', 'site_wide_fields_to_send'),
0 ignored issues
show
Bug introduced by
It seems like Config::inst()->get('Sal...e_wide_fields_to_send') can also be of type boolean and double and integer and string; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

111
            /** @scrutinizer ignore-type */ Config::inst()->get('SalesForceDefaultContactField', 'site_wide_fields_to_send'),
Loading history...
112
            $array
113
        );
114
    }
115
116
    /**
117
     *
118
     * @param  array|DataList|null $mixed fields to send
119
     *
120
     * @return array|DataList|null
121
     */
122
    public static function get_fields_for_filter($mixed = null)
123
    {
124
        $array = self::mixed_to_array($mixed);
125
126
        return array_merge(
127
            Config::inst()->get('SalesForceDefaultContactField', 'site_wide_filter_values'),
0 ignored issues
show
Bug introduced by
It seems like Config::inst()->get('Sal...te_wide_filter_values') can also be of type boolean and double and integer and string; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

127
            /** @scrutinizer ignore-type */ Config::inst()->get('SalesForceDefaultContactField', 'site_wide_filter_values'),
Loading history...
128
            $array
129
        );
130
    }
131
132
    protected static function mixed_to_array($mixed = null)
133
    {
134
        if($mixed === null) {
135
            $array = [];
136
        }
137
        elseif($mixed instanceof DataList) {
138
            $array = [];
139
            foreach($mixed as $object) {
140
                $array[trim($object->Field)] = $object->BetterValue();
141
            }
142
        } elseif(! is_array($array)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $array seems to be never defined.
Loading history...
143
            user_error('Variable '.vardump($mixed).'Should be an array');
0 ignored issues
show
Bug introduced by
The function vardump was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

143
            user_error('Variable './** @scrutinizer ignore-call */ vardump($mixed).'Should be an array');
Loading history...
144
        }
145
146
        return $array;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $array does not seem to be defined for all execution paths leading up to this point.
Loading history...
147
    }
148
149
150
    /**
151
     * Singular name for CMS
152
     * @var string
153
     */
154
    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...
155
156
    /**
157
     * Plural name for CMS
158
     * @var string
159
     */
160
    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...
161
162
    /**
163
     *
164
     * @var array
165
     */
166
    private static $db = [
1 ignored issue
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
167
        'Key' => 'Varchar',
168
        'Value' => 'Varchar'
169
    ];
170
171
    /**
172
     * Defines summary fields commonly used in table columns
173
     * as a quick overview of the data for this dataobject
174
     * @var array
175
     */
176
    private static $summary_fields = [
1 ignored issue
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
177
        'Key' => 'Field Name',
178
        'Value' => 'Field Value'
179
    ];
180
181
    /**
182
     *
183
     * @return string
184
     */
185
    public function getTitle()
186
    {
187
        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...
188
    }
189
190
191
    public function requireDefaultRecords()
192
    {
193
        foreach($this->Config()->get('default_records') as $key => $value) {
194
            $filter = [
195
                'Key' => $key
196
            ];
197
198
            $obj = SalesForceDefaultContactField::get()->filter($filter)->first();
199
            if(! $obj) {
200
                $obj->create($filter);
201
                $obj->Value = $value;
202
                $obj->write();
203
            }
204
205
        }
206
    }
207
208
    /**
209
     * @return mixed
210
     */
211
    public function BetterValue()
212
    {
213
        if(strtolower($this->Value) === 'true') {
214
            return true;
215
        }
216
        if(strtolower($this->Value) === 'false') {
217
            return false;
218
        }
219
        if(floatval($this->Value)) {
220
            return floatval($this->Value);
221
        }
222
        if(intval($this->Value)) {
223
            return intval($this->Value);
224
        }
225
226
        return trim($this->Value);
227
    }
228
229
230
231
}
232