Passed
Push — master ( 761b8d...0c3b7f )
by Nicolaas
02:11
created

MySalesforceContactConfigApi::array_to_html()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * returns a bunch of form fields that inform the user about the configuration
4
 * of the connection and communication with Salesforce.
5
 */
6
class MySalesforceContactConfigApi extends Object
7
{
8
9
    /**
10
     *
11
     * @var array
12
     */
13
    private static $site_wide_fields_to_send_on_creation = [];
1 ignored issue
show
introduced by
The private property $site_wide_fields_to_send_on_creation is not used, and could be removed.
Loading history...
14
15
    /**
16
     *
17
     * @var array
18
     */
19
    private static $site_wide_fields_to_send_on_update = [];
1 ignored issue
show
introduced by
The private property $site_wide_fields_to_send_on_update is not used, and could be removed.
Loading history...
20
21
    /**
22
     *
23
     * @var array
24
     */
25
    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...
26
27
28
    /**
29
     *
30
     * @var array
31
     */
32
    private static $run_time_fields_to_send_on_creation = [];
33
34
    /**
35
     *
36
     * @param  array|DataList|string $mixed fields to send
37
     *
38
     */
39
    public static function add_fields_to_send_on_creation($mixed)
40
    {
41
        $array = self::mixed_to_array($mixed);
0 ignored issues
show
Bug introduced by
It seems like $mixed can also be of type string; however, parameter $mixed of MySalesforceContactConfigApi::mixed_to_array() does only seem to accept DataList|array|null, 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

41
        $array = self::mixed_to_array(/** @scrutinizer ignore-type */ $mixed);
Loading history...
42
43
        self::$run_time_fields_to_send_on_creation += $array;
44
    }
45
46
    /**
47
     *
48
     * @var array
49
     */
50
    private static $run_time_fields_to_send_on_update = [];
51
52
    /**
53
     *
54
     * @param  array|DataList|string $mixed fields to send
55
     *
56
     * @return array
57
     */
58
    public static function add_fields_to_send_on_update($mixed)
59
    {
60
        $array = self::mixed_to_array($mixed);
0 ignored issues
show
Bug introduced by
It seems like $mixed can also be of type string; however, parameter $mixed of MySalesforceContactConfigApi::mixed_to_array() does only seem to accept DataList|array|null, 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

60
        $array = self::mixed_to_array(/** @scrutinizer ignore-type */ $mixed);
Loading history...
61
62
        self::$run_time_fields_to_send_on_update += $array;
63
    }
64
65
    /**
66
     *
67
     * @var array
68
     */
69
    private static $run_time_fields_for_filter = [];
70
71
    /**
72
     *
73
     * @param  array|DataList|string $mixed fields to send
74
     *
75
     * @return array
76
     */
77
    public static function add_fields_to_use_for_filter($mixed)
78
    {
79
        $array = self::mixed_to_array($mixed);
0 ignored issues
show
Bug introduced by
It seems like $mixed can also be of type string; however, parameter $mixed of MySalesforceContactConfigApi::mixed_to_array() does only seem to accept DataList|array|null, 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

79
        $array = self::mixed_to_array(/** @scrutinizer ignore-type */ $mixed);
Loading history...
80
81
        self::$run_time_fields_for_filter += $array;
82
    }
83
84
85
    /**
86
     *
87
     * @param  array|DataList|null $mixed fields to send
88
     *
89
     * @return array
90
     */
91
    public static function get_fields_to_send_on_creation($mixed = null)
92
    {
93
        $array = self::mixed_to_array($mixed);
94
        return array_merge(
95
            Config::inst()->get('MySalesforceContactConfigApi', 'site_wide_fields_to_send_on_creation'),
1 ignored issue
show
Bug introduced by
It seems like Config::inst()->get('MyS...s_to_send_on_creation') 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

95
            /** @scrutinizer ignore-type */ Config::inst()->get('MySalesforceContactConfigApi', 'site_wide_fields_to_send_on_creation'),
Loading history...
96
            $array,
97
            self::$run_time_fields_to_send_on_creation
98
        );
99
    }
100
101
    /**
102
     *
103
     * @param  array|DataList|null $mixed fields to send
104
     *
105
     * @return array
106
     */
107
    public static function get_fields_to_send_on_update($mixed = null)
108
    {
109
        $array = self::mixed_to_array($mixed);
110
111
        return array_merge(
112
            Config::inst()->get('MySalesforceContactConfigApi', 'site_wide_fields_to_send_on_update'),
1 ignored issue
show
Bug introduced by
It seems like Config::inst()->get('MyS...lds_to_send_on_update') 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

112
            /** @scrutinizer ignore-type */ Config::inst()->get('MySalesforceContactConfigApi', 'site_wide_fields_to_send_on_update'),
Loading history...
113
            $array,
114
            self::$run_time_fields_to_send_on_update
115
        );
116
    }
117
118
    /**
119
     *
120
     * @param  array|DataList|null $mixed fields to send
121
     *
122
     * @return array|DataList|null
123
     */
124
    public static function get_fields_for_filter($mixed = null)
125
    {
126
        $array = self::mixed_to_array($mixed);
127
128
        return array_merge(
129
            Config::inst()->get('MySalesforceContactConfigApi', 'site_wide_filter_values'),
1 ignored issue
show
Bug introduced by
It seems like Config::inst()->get('MyS...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

129
            /** @scrutinizer ignore-type */ Config::inst()->get('MySalesforceContactConfigApi', 'site_wide_filter_values'),
Loading history...
130
            $array,
131
            self::$run_time_fields_for_filter
132
        );
133
    }
134
135
    /**
136
     *
137
     * @param  DataList|array|null $mixed
138
     * @return array
139
     */
140
    protected static function mixed_to_array($mixed = null)
141
    {
142
        if(is_object($mixed)) {
143
            $array = [];
144
            foreach($mixed as $object) {
145
                $array[trim($object->Key)] = $object->BetterValueHumanReadable();
146
            }
147
        } elseif($mixed === null) {
148
            $array = [];
149
        } elseif(is_string($mixed)) {
0 ignored issues
show
introduced by
The condition is_string($mixed) is always false.
Loading history...
150
            $array = [ $mixed ];
151
        } elseif(! is_array($array)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $array seems to be never defined.
Loading history...
152
            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

152
            user_error('Variable './** @scrutinizer ignore-call */ vardump($mixed).'Should be an array');
Loading history...
153
        }
154
155
        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...
156
    }
157
158
    public static function login_details_field(
159
        $title = 'Account Used',
160
        $fieldName = 'SalesforceLoginDetailsField'
161
    )
162
    {
163
        $userName = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'username');
164
        $password = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'password');
165
        $security_token = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'security_token');
166
        $wsdl_location = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'wsdl_location');
167
        $array = [
168
            'UserName' => $userName,
169
            'Password' => substr($password, 0, 2) . '...'.substr($password,  -2),
1 ignored issue
show
Bug introduced by
It seems like $password can also be of type array; however, parameter $string of substr() does only seem to accept string, 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

169
            'Password' => substr(/** @scrutinizer ignore-type */ $password, 0, 2) . '...'.substr($password,  -2),
Loading history...
170
            'Token' => substr($security_token, 0, 2) . '...'.substr($security_token,  -2),
171
            'WSDLLocation' => $wsdl_location,
172
        ];
173
        return LiteralField::create(
174
            $fieldName,
175
            '<h2>'.$title.'</h2>'.
176
            self::array_to_html($array)
177
        );
178
    }
179
    /**
180
     * Field with list of contact record types shown
181
     * @param  string $fieldName
182
     * @param  string $title
183
     *
184
     * @return LiteralField
185
     */
186
    public static function list_of_contact_record_types_field(
187
        $fieldName = 'ListOfContactRecordTypes',
0 ignored issues
show
Unused Code introduced by
The parameter $fieldName is not used and could be removed. ( Ignorable by Annotation )

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

187
        /** @scrutinizer ignore-unused */ $fieldName = 'ListOfContactRecordTypes',

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
188
        $title = 'Contact Record Types Available'
189
    )
190
    {
191
        $data = MySalesforceContactApi::retrieve_contact_record_types();
192
193
        return LiteralField::create(
194
            'ListOfContactRecordTypes',
195
            '<h2>'.$title.'</h2>'.
196
            '<pre>'.print_r($data, 1).'</pre>'
197
        );
198
    }
199
200
    /**
201
     * Needs to link to a many-many relationship (SalesforceDefaultContactFields)
202
     * @param  array $array fields to send
203
     *
204
     * @return CheckboxSetField
205
     */
206
    public static function select_default_contact_fields_field($fieldName, $title, $desc = '')
207
    {
208
        $count = SalesforceDefaultContactField::get()->count();
209
        if($count === 0) {
210
            return HiddenField::create(
0 ignored issues
show
Bug Best Practice introduced by
The expression return HiddenField::create($fieldName, $title) returns the type HiddenField which is incompatible with the documented return type CheckboxSetField.
Loading history...
211
                $fieldName,
212
                $title
213
            );
214
        }
215
        return CheckboxSetField::create(
216
            $fieldName,
217
            $title,
218
            SalesforceDefaultContactField::get()->map()->toArray()
219
        )
220
            ->setDescription('
221
                '.$desc.'
222
                <br />
223
                You can
224
                <a href="/admin/salesforceadmin/'.SalesforceDefaultContactField::class.'/">Add or Edit the options</a>
225
                as required. Please change with care.
226
            ');
227
    }
228
229
    /**
230
     *
231
     * @param  array|DataList|null $array fields to send
232
     * @param  string $fieldName fields to send
233
     * @param  string $title fields to send
234
     *
235
     * @return LiteralField
236
     */
237
    public static function fields_to_send_field(
238
        $type,
239
        $mixed = null,
240
        $fieldName = 'FieldsToSendToSalesforce',
241
        $title  = 'Default Fields for'
242
    )
243
    {
244
        $fieldName .= ucfirst($type);
245
        $title .= ' '.ucfirst($type);
246
247
        switch( $type ) {
248
            case 'create':
249
                $array = self::get_fields_to_send_on_creation($mixed);
250
                break;
251
            case 'update':
252
                $array = self::get_fields_to_send_on_update($mixed);
253
                break;
254
            case 'filter':
255
                $array = self::get_fields_for_filter($mixed);
256
                break;
257
            default:
258
                user_error('type needs to be create, update or filter - currently set to: '.$type);
259
                break;
260
        }
261
        return LiteralField::create(
262
            $fieldName,
263
            '<h2>'.$title.'</h2>'.
264
            self::array_to_html($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...
265
        );
266
    }
267
268
    /**
269
     *
270
     * @param  [type] $array [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
271
     * @return [type]        [description]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
272
     */
273
    protected static function array_to_html($array) : string
274
    {
275
        $htmlArray = [];
276
        foreach($array as $field => $value) {
277
            $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')';
278
        }
279
        if(count($htmlArray) == 0) {
280
            $htmlArray[] = 'none';
281
        }
282
283
        return '<p>- '.implode('</p><p> - ', $htmlArray).'</p>';
284
    }
285
}
286