Passed
Push — master ( 164543...a5d39d )
by Nicolaas
02:20
created

MySalesforceContactConfigApi   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
c 1
b 0
f 0
dl 0
loc 177
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A login_details_field() 0 19 1
A array_to_html() 0 11 3
A list_of_contact_record_types_field() 0 11 1
A all_fields() 0 36 1
A select_default_contact_fields_field() 0 19 2
A fields_to_send_field() 0 29 4
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
     * [all_fields description]
11
     * @param  DataObject $dataObject
12
     * @param  [type] $createFields
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...
13
     * @param  [type] $updateFields
14
     * @param  [type] $filterFields
15
     * @return array
16
     */
17
    public static function all_fields($dataObject, $createFields, $updateFields, $filterFields)
18
    {
19
        return [
20
            self::fields_to_send_field(
0 ignored issues
show
Bug introduced by
The method fields_to_send_field() does not exist on MySalesforceContactConfigApi. ( Ignorable by Annotation )

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

20
            self::/** @scrutinizer ignore-call */ 
21
                  fields_to_send_field(

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...
21
                'create',
22
                $this->$createFields()
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using $this inside a static method is generally not recommended and can lead to errors in newer PHP versions.
Loading history...
23
            ),
24
25
            self::select_default_contact_fields_field(
0 ignored issues
show
Bug introduced by
The method select_default_contact_fields_field() does not exist on MySalesforceContactConfigApi. ( Ignorable by Annotation )

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

25
            self::/** @scrutinizer ignore-call */ 
26
                  select_default_contact_fields_field(

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...
26
                $createFields,
27
                'Select ...'
28
            ),
29
30
            self::fields_to_send_field(
31
                'update',
32
                $this->$updateFields()
33
            ),
34
35
            self::select_default_contact_fields_field(
36
                $updateFields,
37
                'Select ...'
38
            ),
39
40
            self::fields_to_send_field(
41
                'filter',
42
                $this->$filterFields()
43
            ),
44
45
            self::select_default_contact_fields_field(
46
                $filterFields,
47
                'Select ...'
48
            ),
49
50
            self::list_of_contact_record_types_field(),
0 ignored issues
show
Bug introduced by
The method list_of_contact_record_types_field() does not exist on MySalesforceContactConfigApi. ( Ignorable by Annotation )

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

50
            self::/** @scrutinizer ignore-call */ 
51
                  list_of_contact_record_types_field(),

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...
51
52
            self::login_details_field(),
0 ignored issues
show
Bug introduced by
The method login_details_field() does not exist on MySalesforceContactConfigApi. ( Ignorable by Annotation )

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

52
            self::/** @scrutinizer ignore-call */ 
53
                  login_details_field(),

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...
53
        ];
54
    }
55
56
    public static function login_details_field(
57
        $title = 'Account Used',
58
        $fieldName = 'SalesforceLoginDetailsField'
59
    )
60
    {
61
        $userName = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'username');
62
        $password = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'password');
63
        $security_token = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'security_token');
64
        $wsdl_location = Config::inst()->get('MySalesforcePartnerApiConnectionOnly', 'wsdl_location');
65
        $array = [
66
            'UserName' => $userName,
67
            'Password' => substr($password, 0, 2) . '...'.substr($password,  -2),
68
            'Token' => substr($security_token, 0, 2) . '...'.substr($security_token,  -2),
69
            'WSDLLocation' => $wsdl_location,
70
        ];
71
        return LiteralField::create(
72
            $fieldName,
73
            '<h2>'.$title.'</h2>'.
74
            self::array_to_html($array)
0 ignored issues
show
Bug introduced by
The method array_to_html() does not exist on MySalesforceContactConfigApi. ( Ignorable by Annotation )

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

74
            self::/** @scrutinizer ignore-call */ 
75
                  array_to_html($array)

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...
75
        );
76
    }
77
    /**
78
     * Field with list of contact record types shown
79
     * @param  string $fieldName
80
     * @param  string $title
81
     *
82
     * @return LiteralField
83
     */
84
    public static function list_of_contact_record_types_field(
85
        $fieldName = 'ListOfContactRecordTypes',
86
        $title = 'Contact Record Types Available'
87
    )
88
    {
89
        $data = MySalesforceContactApi::retrieve_contact_record_types();
90
91
        return LiteralField::create(
92
            $fieldName,
93
            '<h2>'.$title.'</h2>'.
94
            '<pre>'.print_r($data, 1).'</pre>'
95
        );
96
    }
97
98
    /**
99
     * Needs to link to a many-many relationship (SalesforceDefaultContactFields)
100
     * @param  array $array fields to send
101
     *
102
     * @return FormField
103
     */
104
    public static function select_default_contact_fields_field($fieldName, $title, $desc = '')
105
    {
106
        $count = SalesforceDefaultContactField::get()->count();
107
        if($count === 0) {
108
            return HiddenField::create(
109
                $fieldName,
110
                $title
111
            );
112
        }
113
        return CheckboxSetField::create(
114
            $fieldName,
115
            $title,
116
            SalesforceDefaultContactField::get()->map()->toArray()
117
        )
118
            ->setDescription('
119
                '.$desc.'
120
                <br />
121
                You can
122
                <a href="/admin/salesforceadmin/'.SalesforceDefaultContactField::class.'/">Add or Edit the options</a>
123
                as required. Please change with care.
124
            ');
125
    }
126
127
    /**
128
     *
129
     * @param  array|DataList|null $array fields to send
130
     * @param  string $fieldName fields to send
131
     * @param  string $title fields to send
132
     *
133
     * @return LiteralField
134
     */
135
    public static function fields_to_send_field(
136
        $type,
137
        $mixed = null,
138
        $fieldName = 'FieldsToSendToSalesforce',
139
        $title  = 'Default Fields for'
140
    )
141
    {
142
        $fieldName .= ucfirst($type);
143
        $title .= ' '.ucfirst($type);
144
145
        $array = [];
146
        switch( $type ) {
147
            case 'create':
148
                $array = MySalesforceContactConfigApi::get_fields_to_send_on_creation($mixed);
149
                break;
150
            case 'update':
151
                $array = MySalesforceContactConfigApi::get_fields_to_send_on_update($mixed);
152
                break;
153
            case 'filter':
154
                $array = MySalesforceContactConfigApi::get_fields_for_filter($mixed);
155
                break;
156
            default:
157
                user_error('type needs to be create, update or filter - currently set to: '.$type);
158
                break;
159
        }
160
        return LiteralField::create(
161
            $fieldName,
162
            '<h2>'.$title.'</h2>'.
163
            self::array_to_html($array)
164
        );
165
    }
166
167
    /**
168
     *
169
     * @param  array $array
170
     * @return string
171
     */
172
    protected static function array_to_html($array)
173
    {
174
        $htmlArray = [];
175
        foreach($array as $field => $value) {
176
            $htmlArray[] = $field.' = '.$value;
177
        }
178
        if(count($htmlArray) == 0) {
179
            $htmlArray[] = 'none';
180
        }
181
182
        return '<p>- '.implode('</p><p> - ', $htmlArray).'</p>';
183
    }
184
}
185