Passed
Push — master ( f0da52...a5593b )
by Nicolaas
02:24
created

requireDefaultRecords()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 12
rs 10
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
     *
12
     * @param  array $array fields to send
13
     *
14
     * @return LiteralField
15
     */
16
    public static function field_showing_fields_to_send($array)
17
    {
18
        $htmlArray = [];
19
        foreach($array as $field => $value) {
20
            $htmlArray[] = $field.' = '.$value.' ('.gettype($value).')';
21
        }
22
23
        return LiteralField::create(
24
            'FieldsToSendToSalesforce',
25
            '<h2>Fields Added:</h2><p>'.implode('</p><p>', $htmlArray).'</p>'
26
        );
27
    }
28
29
    /**
30
     *
31
     * @var array
32
     */
33
    private static $db = [
1 ignored issue
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
34
        'Key' => 'Varchar',
35
        'Value' => 'Varchar'
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
    ];
47
48
    /**
49
     *
50
     * @return string
51
     */
52
    public function getTitle()
53
    {
54
        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...
55
    }
56
57
    /**
58
     * contact fields that should be created by default...
59
     * @var array
60
     */
61
    private static $defaults_records = [];
1 ignored issue
show
introduced by
The private property $defaults_records is not used, and could be removed.
Loading history...
62
63
    public function requireDefaultRecords()
64
    {
65
        foreach($this->Config()->get('default_records') as $key => $value) {
66
            $filter = [
67
                'Key' => $key
68
            ];
69
70
            $obj = SalesForceDefaultContactField::get()->filter($filter)->first();
71
            if(! $obj) {
72
                $obj->create($filter);
73
                $obj->Value = $value;
74
                $obj->write();
75
            }
76
77
        }
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83
    public function BetterValue()
84
    {
85
        if(strtolower($this->Value) === 'true') {
86
            return true;
87
        }
88
        if(strtolower($this->Value) === 'false') {
89
            return false;
90
        }
91
        if(floatval($this->Value)) {
92
            return floatval($this->Value);
93
        }
94
        if(intval($this->Value)) {
95
            return intval($this->Value);
96
        }
97
    }
98
99
}
100