public function postToEvents($series) { $raw = file_get_contents('php://input'); $raw = trim($raw); $data = (array) json_decode($raw); if (empty($data)) { $o = new Output(); $o->return400('Empty raw data or malformed JSON.'); } $event = new EventEnvelope(); try { $event->loadFromArray($data); } catch (\RuntimeException $e) { $o = new Output(); $o->return400($e->getMessage()); } $kc = new KeyControl($this->config); if ($kc->checkKeys($event->apiKey, $event->source) === false) { $o = new Output(); $o->return403('Invalid api key provided.'); } $seriesName = str_replace('-', ':', $series); try { $cache = new ArraySeriesCache($this->config); } catch (\RuntimeException $e) { $o = new Output(); $o->return500('Exception while initializing SeriesCache: ' . $e->getMessage()); exit; // for PHPstrom to stop annoying on Storage construction } $s = new Storage($this->config, $cache); try { $s->storeEvent($event, $seriesName); } catch (\Exception $e) { $o = new Output(); $o->return500('Exception while saving: ' . $e->getMessage()); } $o = new Output(); $o->return200(); }
<?php use Otik\Config; use Tracy\Debugger; use Otik\Output; require_once 'vendor/autoload.php'; $config = new Config(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config'); Debugger::enable(Debugger::DETECT, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'log', $config->get('dev.email')); $router = new AltoRouter(); $router->map('OPTIONS', '/series/[*:series]/events', 'Options::optionsForEvents', 'optionsForEvents'); $router->map('POST', '/series/[*:series]/events', 'Event::postToEvents', 'postToEvents'); if (isset($_REQUEST['_method'])) { $matched = $router->match(null, $_REQUEST['_method']); } else { $matched = $router->match(); } if ($matched === false) { $o = new Output(); $o->return404(); } list($wantedController, $wantedAction) = explode('::', $matched['target']); $controllerClassName = 'Otik\\' . $wantedController; if (!class_exists($controllerClassName)) { throw new RuntimeException('Target class (' . $controllerClassName . ') doesn\'t exists or cann\'t be autoloaded'); } $controller = new $controllerClassName($config); if (!method_exists($controller, $wantedAction)) { throw new RuntimeException('Target class doesn\'t have method ' . $wantedAction . '.'); } call_user_func_array(array($controller, $wantedAction), $matched['params']);