-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
209 lines (163 loc) · 5.68 KB
/
index.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
<?php
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
// init & configs
$app = new \Slim\Slim(array(
'templates.path' => './views'
));
require "config/env.php";
$m = new Mongo("mongodb://localhost");
$db = $m->selectDB('squaretestdb');
// FIXME: for php 5.4 create a static class DB
$app->config('db', $db);
// routes
$app->get('/', function() use ($app){
// just for show/debug
$db = $app->config('db');
$users = new MongoCollection($db, 'users');
$activities = new MongoCollection($db, 'activities');
$events = new MongoCollection($db, 'events');
$app->render("docs.php", array(
'users' => $users,
'activities' => $activities,
'events' => $events
)
);
});
// TODO: create a Response class, extend it and make this an InvalidResponse
function invalid_response($app, $error='') {
$app->halt(400, json_encode(array("error" => $error)));
}
// PHP 5.4
// class Event {
// public static $types = ["enter", "click", "exit"];
// }
// TODO: move each route in a different file for a more modular structure
// TODO: refactor parameters check
// renamed /track into /events that it follows the REST principle of "directories"
// e.g.: /resources_collection_name/collection_id - /collection/id - /posts/1
// and POST, PUT, DELETE /collection/id
// then GET /collection and POST /collection
// the collection name is usually plural as in URLs (it makes sense if you think urls more like directories / levels)
$app->get('/activities/:activity_id', function($activity_id) use ($app){
// parameters checks
if ( !preg_match("/[\w\d]{4,9}/i", $activity_id) ) {
invalid_response($app, "You need to pass a valid activity");
}
// TODO: refactor collections
$db = $app->config('db');
$activities = new MongoCollection($db, 'activities');
$activity = $activities->findOne( array('id' => $activity_id) );
unset($activity["_id"]);
echo json_encode($activity);
});
$app->get('/users/:user_id', function($user_id) use ($app){
// parameters checks
if ( !is_numeric($user_id) ) {
invalid_response($app, "You need to pass integers as arguments");
}
// TODO: refactor collections
$db = $app->config('db');
$users = new MongoCollection($db, 'users');
$user = $users->findOne( array('id' => intval($user_id)) );
unset($user["_id"]);
echo json_encode($user);
});
// note: renamed /track to /events as it make more sense
// TODO: change method from PUT to POST as it's the creation of an event and not an update to a resource (event)
// FIXME: move :activity_id, :user_id, :event_type into post sent parameters so the url is only /events
$app->put('/events/:activity_id/:user_id/:event_type', function($activity_id, $user_id, $event_type) use ($app){
// parameters checks
if ( !is_numeric($user_id) ) {
invalid_response($app, "You need to pass integers as arguments");
}
// PHP 5.4
//if ( !in_array($event_type, Event::$types) ) {
$event_types = array("enter", "click", "exit"); // 5.3
if ( !in_array($event_type, $event_types) ) {
invalid_response($app, ":event_type needs to be in: ".print_r(Event::$types));
}
// parameters checks
if ( !preg_match("/[\w\d]{4,9}/i", $activity_id) ) {
invalid_response($app, "You need to pass a valid activity");
}
$req = $app->request;
// TODO: refactor collections
$db = $app->config('db');
$activities = new MongoCollection($db, 'activities');
$users = new MongoCollection($db, 'users');
$events = new MongoCollection($db, 'events');
$event = array(
"activity_id" => $activity_id,
"user_id" => $user_id,
"event_type" => $event_type,
"created_at" => new MongoDate(),
"user_agent" => $req->getUserAgent(),
"ip_address" => $req->getIp()
);
$events->insert($event);
$activities->update(
array('id' => $activity_id),
array(
'$inc' => array("counters.$event_type" => 1)//,
// "updated_at" => new MongoDate()
)
);
$users->update(
array('id' => $user_id),
array('$inc' => array("counters.$activity_id.$event_type" => 1))
);
// TODO: skipped those two:
// - Setting, on document creation, a creation time field to the current timestamp.
// - Setting a last update field to the current timestamp.
// echo $event;
echo json_encode(array("success" => "true"));
});
// TODO: enable only in development
$app->get('/seed_db', function() use ($app){
// TODO: refactor collections
$db = $app->config('db');
$activities = new MongoCollection($db, 'activities');
$users = new MongoCollection($db, 'users');
$events = new MongoCollection($db, 'events');
$activities->remove( array());
$users->remove( array());
$events->remove( array());
$activity = array(
"id" => "ff7x",
"counters" => array( "enter" => 0, "click" => 0, "exit" => 0 ),
"created_at" => new MongoDate()
);
$activities->insert($activity);
$activity = array(
"id" => "tr2x",
"counters" => array( "enter" => 0, "click" => 0, "exit" => 0 ),
"created_at" => new MongoDate()
);
$activities->insert($activity);
$event = array(
"id" => 1,
"user_id" => 1,
"activity_id" => "ff7x",
"event_type" => "click",
"user_agent" => "Mozilla Firefox",
"ip_address" => "127.0.0.1"
);
$events->insert($event);
$user = array(
"id" => 1,
"counters" => array(
"ffx7" => array( "enter" => 0, "click" => 0, "exit" => 0 ),
"tr2x" => array( "enter" => 0, "click" => 0, "exit" => 0 ),
),
"created_at" => new MongoDate()
);
$users->insert($user);
echo "db seed was successful!";
});
function update() {
// TODO: set updated_at to time()
}
// run
$app->run();