Passed
Push — master ( 1327da...26e128 )
by Nicolaas
04:23 queued 02:17
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
95
        return array_merge(
96
            Config::inst()->get('SalesforceDefaultContactField', 'site_wide_fields_to_send_on_creation'),
0 ignored issues
show
Bug introduced by
It seems like Config::inst()->get('Sal...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

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

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

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

154
            user_error('Variable './** @scrutinizer ignore-call */ vardump($mixed).'Should be an array');
Loading history...
155
        }
156
157
        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...
158
    }
159
160
    public static function login_details_field(
161
        $title = 'Account Used',
162
        $fieldName = 'SalesforceLoginDetailsField'
163
    )
164
    {
165
        $userName = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'username');
166
        $password = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'password');
167
        $security_token = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'security_token');
168
        $wsdl_location = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'wsdl_location');
169
        $array = [
170
            'UserName' => $userName,
171
            '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

171
            'Password' => substr(/** @scrutinizer ignore-type */ $password, 0, 2) . '...'.substr($password,  -2),
Loading history...
172
            'Token' => substr($security_token, 0, 2) . '...'.substr($security_token,  -2),
173
            'WSDLLocation' => $wsdl_location,
174
        ];
175
        return LiteralField::create(
176
            $fieldName,
177
            '<h2>'.$title.'</h2>'.
178
            self::array_to_html($array)
179
        );
180
    }
181
    /**
182
     * Field with list of contact record types shown
183
     * @param  string $fieldName
184
     * @param  string $title
185
     *
186
     * @return LiteralField
187
     */
188
    public static function list_of_contact_record_types_field(
189
        $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

189
        /** @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...
190
        $title = 'Contact Record Types Available'
191
    )
192
    {
193
        $data = MySalesforcePartnerApi::retrieve_contact_record_types();
0 ignored issues
show
Bug introduced by
The method retrieve_contact_record_types() does not exist on MySalesforcePartnerApi. Did you maybe mean retrieve()? ( Ignorable by Annotation )

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

193
        /** @scrutinizer ignore-call */ 
194
        $data = MySalesforcePartnerApi::retrieve_contact_record_types();

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