Passed
Push — master ( c40630...f0da52 )
by Nicolaas
08:09 queued 05:18
created

SalesForceContact::onAfterWrite()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 20
rs 8.8333
1
<?php
2
3
use SForce\Wsdl\SaveResult;
4
5
class SalesForceContact extends DataObject
6
{
7
8
    private static $fields_to_send_to_sales_force = [
9
        'FirstName',
10
        'LastName',
11
        'Email',
12
    ];
13
14
    /**
15
     * Singular name for CMS
16
     * @var string
17
     */
18
    private static $singular_name = 'Sales Force Contact';
19
20
    /**
21
     * Plural name for CMS
22
     * @var string
23
     */
24
    private static $plural_name = 'Sales Force Contacts';
25
26
    /**
27
     * SS4 prep!
28
     * @var string
29
     */
30
    private static $table_name = 'SalesForceContact';
31
32
    private static $db = [
33
        'SalesForceIdentifier' => 'Varchar(40)',
34
        'FirstName' => 'Varchar(255)',
35
        'LastName' => 'Varchar(255)',
36
        'Email' => 'Varchar(255)',
37
        'SentToSalesForce' => 'Boolean',
38
        'CommsErrors' => 'Text',
39
    ];
40
41
42
    private static $default_sort = [
43
        'ID' => 'DESC'
44
    ];
45
46
    /**
47
     * Defines a default list of filters for the search context
48
     * @var array
49
     */
50
    private static $searchable_fields = [
51
        'SentToSalesForce' => 'ExactMatchFilter',
52
        'SalesForceIdentifier' => 'PartialMatchField',
53
        'FirstName' => 'PartialMatchField',
54
        'LastName' => 'PartialMatchField',
55
        'Email' => 'PartialMatchField',
56
    ];
57
58
    /**
59
     * Defines summary fields commonly used in table columns
60
     * as a quick overview of the data for this dataobject
61
     * @var array
62
     */
63
    private static $summary_fields = [
64
        'SentToSalesForce' => 'Sent to SF',
65
        'SalesForceIdentifier' => 'SF ID',
66
        'FirstName' => 'First Name',
67
        'LastName' => 'Last Name',
68
        'Email' => 'Email',
69
    ];
70
71
    private static $indexes = [
72
        'Created' => true,
73
        'LastEdited' => true,
74
        'SentToSalesForce' => true,
75
        'SalesForceIdentifier' => true,
76
        'FirstName' => true,
77
        'LastName' => true,
78
        'Email' => true,
79
    ];
80
81
82
    protected $onAfterWriteDone = false;
83
    /**
84
     * Event handler called after writing to the database.
85
     */
86
    public function onAfterWrite()
87
    {
88
        parent::onAfterWrite();
89
        if($this->onAfterWriteDone === false) {
90
            if(! $this->SentToSalesForce) {
1 ignored issue
show
Bug Best Practice introduced by
The property SentToSalesForce does not exist on SalesForceContact. Since you implemented __get, consider adding a @property annotation.
Loading history...
91
                $outcomes = MySalesForcePartnerAPI::create_contact(
92
                    $this->prepareFieldsToSendToSalesForce()
93
                );
94
                if(count($outcomes) === 1) {
0 ignored issues
show
Bug introduced by
It seems like $outcomes can also be of type SForce\Wsdl\SaveResult; however, parameter $var of count() does only seem to accept Countable|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

94
                if(count(/** @scrutinizer ignore-type */ $outcomes) === 1) {
Loading history...
95
                    foreach($outcomes as $outcome) {
96
                        $this->CommsErrors .= $outcome->getErrors()." ||| ";
1 ignored issue
show
Bug Best Practice introduced by
The property CommsErrors does not exist on SalesForceContact. Since you implemented __get, consider adding a @property annotation.
Loading history...
97
                        $this->SentToSalesForce .= $outcome->getSuccess() ? true : false;
98
                        if(! $this->SalesForceIdentifier) {
99
                            $this->SalesForceIdentifier = $outcome->getId();
1 ignored issue
show
Bug Best Practice introduced by
The property SalesForceIdentifier does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
100
                        }
101
                    }
102
                    $this->write();
103
                    $this->onAfterWriteDone = true;
104
                } else {
105
                    user_error('unexpected number of outcomes: '.count($outcomes));
106
                }
107
            }
108
        }
109
    }
110
111
    protected function prepareFieldsToSendToSalesForce()
112
    {
113
        $return = [];
114
        $fields = Config::inst()->get('SalesForceContact', 'fields_to_send_to_sales_force');
115
        foreach($fields as $field) {
116
            if($this->$field) {
117
                $return[$field] = $this->$field;
118
            }
119
        }
120
121
        return $return;
122
    }
123
124
    /**
125
     * CMS Fields
126
     * @return FieldList
127
     */
128
    public function getCMSFields()
129
    {
130
        $fields = parent::getCMSFields();
131
        $fields->replaceField(
132
            'SalesForceIdentifier',
133
            ReadonlyField::create(
134
                'SalesForceIdentifier',
135
                'Sales Force Identifier'
136
            )
137
        );
138
        $fields->replaceField(
139
            'SentToSalesForce',
140
            ReadonlyField::create(
141
                'SentToSalesForce',
142
                'Sent to Sales Force'
143
            )->setRightTitle('Set to TRUE (1) if sent successfully.')
144
        );
145
        $fields->replaceField(
146
            'CommsErrors',
147
            ReadonlyField::create(
148
                'CommsErrors',
149
                'Communication Errors'
150
            )->setRightTitle('Separated by three ||| symbols.')
151
        );
152
        $fields->addFieldToTab(
153
            'Root.Advanced',
154
            LiteralField::create(
155
                'WhatWeSend',
156
                '
157
                    <h2>What is Send</h2>,
158
                    <pre>'.print_r($this->prepareFieldsToSendToSalesForce(), 1).'</pre>
159
                '
160
            )
161
        );
162
        return $fields;
163
    }
164
165
166
}
167