| 13682 |
anikendra |
1 |
<?php
|
| 13633 |
anikendra |
2 |
App::uses('HttpSocket', 'Network/Http');
|
|
|
3 |
|
|
|
4 |
class FarAwaySource extends DataSource {
|
|
|
5 |
|
|
|
6 |
/**
|
|
|
7 |
* An optional description of your datasource
|
|
|
8 |
*/
|
|
|
9 |
public $description = 'A far away datasource';
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Our default config options. These options will be customized in our
|
|
|
13 |
* ``app/Config/database.php`` and will be merged in the ``__construct()``.
|
|
|
14 |
*/
|
|
|
15 |
public $config = array(
|
| 13682 |
anikendra |
16 |
'apiHost' => '',
|
| 13633 |
anikendra |
17 |
);
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* If we want to create() or update() we need to specify the fields
|
|
|
21 |
* available. We use the same array keys as we do with CakeSchema, eg.
|
|
|
22 |
* fixtures and schema migrations.
|
|
|
23 |
*/
|
|
|
24 |
protected $_schema = array(
|
| 13682 |
anikendra |
25 |
'sku' => array(
|
| 13633 |
anikendra |
26 |
'type' => 'integer',
|
|
|
27 |
'null' => false,
|
|
|
28 |
'key' => 'primary',
|
|
|
29 |
'length' => 11,
|
|
|
30 |
),
|
| 13682 |
anikendra |
31 |
'startDate' => array(
|
|
|
32 |
'type' => 'integer',
|
|
|
33 |
'null' => false,
|
|
|
34 |
'length' => 20,
|
| 13633 |
anikendra |
35 |
),
|
| 13682 |
anikendra |
36 |
'endDate' => array(
|
|
|
37 |
'type' => 'integer',
|
|
|
38 |
'null' => false,
|
|
|
39 |
'length' => 20,
|
| 13633 |
anikendra |
40 |
),
|
| 13682 |
anikendra |
41 |
'scheme_amount' => array(
|
|
|
42 |
'type' => 'float',
|
|
|
43 |
'null' => false,
|
|
|
44 |
// 'length' => 8,
|
|
|
45 |
),
|
| 13633 |
anikendra |
46 |
);
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Create our HttpSocket and handle any config tweaks.
|
|
|
50 |
*/
|
|
|
51 |
public function __construct($config) {
|
|
|
52 |
parent::__construct($config);
|
|
|
53 |
$this->Http = new HttpSocket();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Since datasources normally connect to a database there are a few things
|
|
|
58 |
* we must change to get them to work without a database.
|
|
|
59 |
*/
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* listSources() is for caching. You'll likely want to implement caching in
|
|
|
63 |
* your own way with a custom datasource. So just ``return null``.
|
|
|
64 |
*/
|
|
|
65 |
public function listSources($data = null) {
|
|
|
66 |
return null;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* describe() tells the model your schema for ``Model::save()``.
|
|
|
71 |
*
|
|
|
72 |
* You may want a different schema for each model but still use a single
|
|
|
73 |
* datasource. If this is your case then set a ``schema`` property on your
|
|
|
74 |
* models and simply return ``$model->schema`` here instead.
|
|
|
75 |
*/
|
|
|
76 |
public function describe($model) {
|
| 13682 |
anikendra |
77 |
return $model->schema;
|
|
|
78 |
// return $this->_schema;
|
| 13633 |
anikendra |
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* calculate() is for determining how we will count the records and is
|
|
|
83 |
* required to get ``update()`` and ``delete()`` to work.
|
|
|
84 |
*
|
|
|
85 |
* We don't count the records here but return a string to be passed to
|
|
|
86 |
* ``read()`` which will do the actual counting. The easiest way is to just
|
|
|
87 |
* return the string 'COUNT' and check for it in ``read()`` where
|
|
|
88 |
* ``$data['fields'] === 'COUNT'``.
|
|
|
89 |
*/
|
|
|
90 |
public function calculate(Model $model, $func, $params = array()) {
|
|
|
91 |
return 'COUNT';
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Implement the R in CRUD. Calls to ``Model::find()`` arrive here.
|
|
|
96 |
*/
|
|
|
97 |
public function read(Model $model, $queryData = array(),
|
|
|
98 |
$recursive = null) {
|
|
|
99 |
/**
|
|
|
100 |
* Here we do the actual count as instructed by our calculate()
|
|
|
101 |
* method above. We could either check the remote source or some
|
|
|
102 |
* other way to get the record count. Here we'll simply return 1 so
|
|
|
103 |
* ``update()`` and ``delete()`` will assume the record exists.
|
|
|
104 |
*/
|
|
|
105 |
if ($queryData['fields'] === 'COUNT') {
|
|
|
106 |
return array(array(array('count' => 1)));
|
|
|
107 |
}
|
|
|
108 |
/**
|
|
|
109 |
* Now we get, decode and return the remote data.
|
|
|
110 |
*/
|
|
|
111 |
$queryData['conditions']['apiKey'] = $this->config['apiKey'];
|
|
|
112 |
$json = $this->Http->get(
|
| 13682 |
anikendra |
113 |
'http://104.200.25.40:8057/discountInfo/getAllSkuSchemeDetails',
|
| 13633 |
anikendra |
114 |
$queryData['conditions']
|
|
|
115 |
);
|
|
|
116 |
$res = json_decode($json, true);
|
|
|
117 |
if (is_null($res)) {
|
|
|
118 |
$error = json_last_error();
|
|
|
119 |
throw new CakeException($error);
|
|
|
120 |
}
|
|
|
121 |
return array($model->alias => $res);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Implement the C in CRUD. Calls to ``Model::save()`` without $model->id
|
|
|
126 |
* set arrive here.
|
|
|
127 |
*/
|
|
|
128 |
public function create(Model $model, $fields = null, $values = null) {
|
|
|
129 |
$data = array_combine($fields, $values);
|
|
|
130 |
$data['apiKey'] = $this->config['apiKey'];
|
|
|
131 |
$json = $this->Http->post('http://example.com/api/set.json', $data);
|
|
|
132 |
$res = json_decode($json, true);
|
|
|
133 |
if (is_null($res)) {
|
|
|
134 |
$error = json_last_error();
|
|
|
135 |
throw new CakeException($error);
|
|
|
136 |
}
|
|
|
137 |
return true;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Implement the U in CRUD. Calls to ``Model::save()`` with $Model->id
|
|
|
142 |
* set arrive here. Depending on the remote source you can just call
|
|
|
143 |
* ``$this->create()``.
|
|
|
144 |
*/
|
|
|
145 |
public function update(Model $model, $fields = null, $values = null,
|
|
|
146 |
$conditions = null) {
|
|
|
147 |
return $this->create($model, $fields, $values);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
/**
|
|
|
151 |
* Implement the D in CRUD. Calls to ``Model::delete()`` arrive here.
|
|
|
152 |
*/
|
|
|
153 |
public function delete(Model $model, $id = null) {
|
|
|
154 |
$json = $this->Http->get('http://example.com/api/remove.json', array(
|
|
|
155 |
'id' => $id[$model->alias . '.id'],
|
|
|
156 |
'apiKey' => $this->config['apiKey'],
|
|
|
157 |
));
|
|
|
158 |
$res = json_decode($json, true);
|
|
|
159 |
if (is_null($res)) {
|
|
|
160 |
$error = json_last_error();
|
|
|
161 |
throw new CakeException($error);
|
|
|
162 |
}
|
|
|
163 |
return true;
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
}
|