-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbraintree_local_app_model.php
229 lines (182 loc) · 5.1 KB
/
braintree_local_app_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* Braintree Local App Model File
*
* Copyright (c) 2010 Anthony Putignano
*
* Distributed under the terms of the MIT License.
* Redistributions of files must retain the above copyright notice.
*
* PHP version 5.2
* CakePHP version 1.3
*
* @package braintree
* @subpackage braintree.models
* @copyright 2010 Anthony Putignano <anthonyp@xonatek.com>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://github.com/anthonyp/braintree
*/
/**
* Braintree Local App Model Class
*
* @package braintree
* @subpackage braintree.models
*/
class BraintreeLocalAppModel extends BraintreeAppModel {
/**
* Should the primary key be automatically generated?
*
* @var boolean
*/
public $autoPK = true;
/**
* Should C*UD calls be synced to the remote API automatically?
*
* @var boolean
*/
public $remote_sync = true;
/**
* Construct
*
* @return void
*/
public function __construct () {
return parent::__construct();
}
/**
* Get the Remote model name that corresponds to this Local model name.
*
* For example, BraintreeTransaction corresponds to BraintreeRemoteTransaction
*
* @return string
*/
private function _getRemoteModelName () {
$entity = str_replace(array('Braintree'), '', $this->name);
$remote_model = 'BraintreeRemote' . $entity;
return $remote_model;
}
/**
* If remote sync has kicked in, this turns remote sync on/off for any related, dependent models
*
* @param bool
* @return void
*/
private function _toggleRemoteSyncOnDependentModels ($enabled=true) {
$model_list = array();
if (!empty($this->hasMany)) {
foreach ($this->hasMany as $model_name => $info) {
if (!is_numeric($model_name)) {
$model_list[] = $model_name;
} elseif (is_string($info)) {
$model_list[] = $model_name;
}
}
}
if (!empty($this->hasOne)) {
foreach ($this->hasOne as $model_name => $info) {
if (!is_numeric($model_name)) {
$model_list[] = $model_name;
} elseif (is_string($info)) {
$model_list[] = $model_name;
}
}
}
foreach ($model_list as $model_name) {
$this->{$model_name}->remote_sync = $enabled;
}
}
/**
* beforeSave
* Accomplishes the following if remote sync is enabled:
* - Sets the primary key to be saved + any fields passed that are in the actual API schema
* - Saves the information to the API
* - Sets the remote ID to the ID for this local model
* - Sets the braintree_merchant_id key for this model to the current merchantId being used
*
* @return bool
*/
public function beforeSave () {
if (empty($this->id) && $this->autoPK) {
$uuid = String::uuid();
$this->id = $this->data[$this->alias][$this->primaryKey] = $uuid;
}
if ($this->remote_sync) {
$remote_model_name = $this->_getRemoteModelName();
if (!isset($this->{$remote_model_name})) {
$this->{$remote_model_name} = ClassRegistry::init('Braintree.' . $remote_model_name);
}
if (!empty($this->id)) {
$this->{$remote_model_name}->id = $this->id;
}
$whitelisted_fields = $this->{$remote_model_name}->_schema;
if ($this->primaryKey != 'id') {
$whitelisted_fields = array_merge(
array($this->primaryKey => array()),
$whitelisted_fields
);
}
foreach ($whitelisted_fields as $field => $schema) {
if (!empty($this->data[$this->alias][$field])) {
$remote_data[$remote_model_name][$field] = $this->data[$this->alias][$field];
}
}
$success = $this->{$remote_model_name}->save($remote_data);
if (!empty($this->{$remote_model_name}->id)) {
$this->id = $this->{$remote_model_name}->id;
$this->data[$this->alias][$this->primaryKey] = $this->id;
$this->{$remote_model_name}->create(false);
$this->{$remote_model_name}->id = null;
}
if (!$success) {
return false;
}
}
if (!empty($this->_schema['braintree_merchant_id'])) {
$merchantId = BraintreeConfig::get('merchantId');
if (empty($merchantId)) {
return false;
}
$this->data[$this->alias]['braintree_merchant_id'] = $merchantId;
}
return true;
}
/**
* beforeDelete
* Accomplishes the following if remote sync is enabled:
* - Deletes the record from the API
* - Turns off remote sync for related, dependent models
*
* @param bool $cascade
* @return bool
*/
public function beforeDelete ($cascade = true) {
if (empty($this->id)) {
return false;
}
if ($this->remote_sync) {
$remote_model_name = $this->_getRemoteModelName();
if (!isset($this->{$remote_model_name})) {
$this->{$remote_model_name} = ClassRegistry::init('Braintree.' . $remote_model_name);
}
$success = $this->{$remote_model_name}->delete($this->id);
if (!$success) {
return false;
}
}
$this->_toggleRemoteSyncOnDependentModels(false);
return true;
}
/**
* afterDelete
* Accomplishes the following if remote sync is enabled:
* - Turns on remote sync for related, dependent models
*
* @param bool $cascade
* @return bool
*/
public function afterDelete () {
$this->_toggleRemoteSyncOnDependentModels(true);
return true;
}
}
?>