Passed
Push — master ( d0bd04...1327da )
by Nicolaas
02:25
created

SalesforceDefaultContactField::BetterValue()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 16
rs 9.6111
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
    /**
17
     * Singular name for CMS
18
     * @var string
19
     */
20
    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...
21
22
    /**
23
     * Plural name for CMS
24
     * @var string
25
     */
26
    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...
27
28
    /**
29
     *
30
     * @var array
31
     */
32
    private static $db = [
1 ignored issue
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
33
        'Key' => 'Varchar',
34
        'Value' => 'Varchar',
35
        'IsFilter' => 'Boolean',
36
    ];
37
38
    /**
39
     * Defines summary fields commonly used in table columns
40
     * as a quick overview of the data for this dataobject
41
     * @var array
42
     */
43
    private static $summary_fields = [
1 ignored issue
show
introduced by
The private property $summary_fields is not used, and could be removed.
Loading history...
44
        'Key' => 'Field Name',
45
        'Value' => 'Field Value',
46
        'IsFilter.Nice' => 'Is Filter',
47
    ];
48
49
    /**
50
     *
51
     * @return string
52
     */
53
    public function getTitle()
54
    {
55
        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...
56
    }
57
58
59
    public function requireDefaultRecords()
60
    {
61
        foreach($this->Config()->get('default_records') as $key => $value) {
62
            $filter = [
63
                'Key' => $key
64
            ];
65
66
            $obj = SalesforceDefaultContactField::get()->filter($filter)->first();
67
            if(! $obj) {
68
                $obj = SalesforceDefaultContactField::create($filter);
69
                $obj->Value = $value;
70
                $obj->write();
71
            }
72
73
        }
74
    }
75
76
    /**
77
     * @return mixed
78
     */
79
    public function BetterValue()
80
    {
81
        if(strtolower($this->Value) === 'true') {
82
            return true;
83
        }
84
        if(strtolower($this->Value) === 'false') {
85
            return false;
86
        }
87
        if(floatval($this->Value)) {
88
            return floatval($this->Value);
89
        }
90
        if(intval($this->Value)) {
91
            return intval($this->Value);
92
        }
93
94
        return trim($this->Value);
95
    }
96
97
98
99
100
}
101