Exemplo n.º 1
0
 /**
  * Currently only version 1 is supported
  */
 public function testValidVersions()
 {
     $payload = new Payload(array('v' => 0));
     $this->assertFalse($payload->isVersionSupported());
     $payload = new Payload(array('v' => 1));
     $this->assertTrue($payload->isVersionSupported());
     $payload = new Payload(array('v' => 2));
     $this->assertFalse($payload->isVersionSupported());
 }
Exemplo n.º 2
0
 public function exec()
 {
     $debug = $this->app->request->get('debug') ? true : false;
     // Get JSON Body
     if ($this->app->request->getMethod() === 'POST') {
         $postBody = file_get_contents('php://input');
         if (!$postBody) {
             return $this->error('No data', 400);
         }
         $payload_data = json_decode($postBody, true);
     } else {
         $payload_data = json_decode($this->app->request->get('payload') ?: '', true);
     }
     // Convert into standardized payload
     $payload = new Payload($payload_data);
     if (!$payload->isVersionSupported()) {
         return $this->error('Payload version ' . $payload->version . ' not supported');
     }
     // Verify Event Credentials
     if (!$payload->event) {
         return $this->error('No Event specified', 400);
     }
     // Verify Bucket Credentials
     if (!$payload->bucket) {
         return $this->error('No Bucket specified', 400);
     }
     $bucket_id = $payload->bucket;
     $bucket = Bucket::findById($bucket_id);
     if ($bucket === null) {
         return $this->error('Invalid Bucket', 404);
     }
     // Save Event
     $insert = array();
     $insert['t'] = $payload->time;
     $insert['e'] = $payload->event;
     $insert['d'] = $payload->data;
     // Save Data to log
     $id = null;
     try {
         $collection = $this->app->mongo->selectCollection($bucket->event_collection);
         $collection->insert($insert);
         $id = (string) $insert['_id'];
     } catch (\Exception $e) {
         // TODO: Queue event for later processing
         return $this->error('Database Exception', 503);
     }
     // Output Results
     $output = array('data' => array('id' => $id));
     if ($debug) {
         $output['debug'] = array('payload' => $payload->asArray());
     }
     return $output;
 }