Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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