Passed
Push — master ( 14ed89...8aec5a )
by Nicolaas
01:56
created

SalesforceDefaultContactField::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
     *
22
     * @param  array|DataList|null $mixed fields to send
23
     *
24
     * @return array
25
     */
26
    public static function get_fields_to_send($mixed = null)
27
    {
28
        $array = self::mixed_to_array($mixed);
29
30
        return array_merge(
31
            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

31
            /** @scrutinizer ignore-type */ Config::inst()->get('SalesforceDefaultContactField', 'site_wide_fields_to_send'),
Loading history...
32
            $array
33
        );
34
    }
35
36
    /**
37
     *
38
     * @param  array|DataList|null $mixed fields to send
39
     *
40
     * @return array|DataList|null
41
     */
42
    public static function get_fields_for_filter($mixed = null)
43
    {
44
        $array = self::mixed_to_array($mixed);
45
46
        return array_merge(
47
            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

47
            /** @scrutinizer ignore-type */ Config::inst()->get('SalesforceDefaultContactField', 'site_wide_filter_values'),
Loading history...
48
            $array
49
        );
50
    }
51
52
    /**
53
     * Singular name for CMS
54
     * @var string
55
     */
56
    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...
57
58
    /**
59
     * Plural name for CMS
60
     * @var string
61
     */
62
    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...
63
64
    /**
65
     *
66
     * @var array
67
     */
68
    private static $db = [
1 ignored issue
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
69
        'Key' => 'Varchar',
70
        'Value' => 'Varchar',
71
        'IsFilter' => 'Boolean',
72
    ];
73
74
    /**
75
     * Defines summary fields commonly used in table columns
76
     * as a quick overview of the data for this dataobject
77
     * @var array
78
     */
79
    private static $summary_fields = [
1 ignored issue
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
80
        'Key' => 'Field Name',
81
        'Value' => 'Field Value',
82
        'IsFilter.Nice' => 'Is Filter',
83
    ];
84
85
    /**
86
     *
87
     * @return string
88
     */
89
    public function getTitle()
90
    {
91
        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...
92
    }
93
94
95
    public function requireDefaultRecords()
96
    {
97
        foreach($this->Config()->get('default_records') as $key => $value) {
98
            $filter = [
99
                'Key' => $key
100
            ];
101
102
            $obj = SalesforceDefaultContactField::get()->filter($filter)->first();
103
            if(! $obj) {
104
                $obj = SalesforceDefaultContactField::create($filter);
105
                $obj->Value = $value;
106
                $obj->write();
107
            }
108
109
        }
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115
    public function BetterValue()
116
    {
117
        if(strtolower($this->Value) === 'true') {
118
            return true;
119
        }
120
        if(strtolower($this->Value) === 'false') {
121
            return false;
122
        }
123
        if(floatval($this->Value)) {
124
            return floatval($this->Value);
125
        }
126
        if(intval($this->Value)) {
127
            return intval($this->Value);
128
        }
129
130
        return trim($this->Value);
131
    }
132
133
134
    /**
135
     *
136
     * @param  DataList|array|null $mixed
137
     * @return array
138
     */
139
    protected static function mixed_to_array($mixed = null) : array
140
    {
141
        if($mixed === null) {
142
            $array = [];
143
        }
144
        elseif($mixed instanceof DataList) {
145
            $array = [];
146
            foreach($mixed as $object) {
147
                $array[trim($object->Key)] = $object->BetterValue();
148
            }
149
        } elseif(is_array($mixed)) {
0 ignored issues
show
introduced by
The condition is_array($mixed) is always true.
Loading history...
150
            $array = $mixed;
151
        } else {
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
159
160
161
}
162