CORE-POS/IS4C

View on GitHub
fannie/classlib2.0/webservices/JsonEndPoint.php

Summary

Maintainability
A
0 mins
Test Coverage
F
51%
<?php

namespace COREPOS\Fannie\API\webservices; 
use \FannieConfig;
use \FannieDispatch;

/**
  FannieDispatch-compatible JSON request handler
  
  Override the get, post, put, and/or delete methods
  to handle requests. All overriden methods should
  return a json-encodable array. The put and post
  methods will received the json-decoded request body
  as an argument.
*/
abstract class JsonEndPoint
{
    protected $dbc;
    protected $config;
    protected $logger;

    public function setLogger($l)
    {
        $this->logger = $l;
    }

    public function setConfig(FannieConfig $c)
    {
        $this->config = $c;
    }

    public function setConnection($db)
    {
        $this->dbc = $db;
    }

    protected function readInput()
    {
        $input = file_get_contents('php://input');
        return json_decode($input, true);
    }

    protected function sendResponse($msg)
    {
        header('Content-type: application/json');
        echo json_encode($msg);
    }

    protected function get()
    {
        header('HTTP/1.0 405 Method Not Allowed');
        return array('error' => 'GET Not implemented');
    }

    protected function post(array $json)
    {
        header('HTTP/1.0 405 Method Not Allowed');
        return array('error' => 'POST Not implemented');
    }

    protected function put(array $json)
    {
        header('HTTP/1.0 405 Method Not Allowed');
        return array('error' => 'PUT Not implemented');
    }

    protected function delete()
    {
        header('HTTP/1.0 405 Method Not Allowed');
        return array('error' => 'DELETE Not implemented');
    }

    public function draw_page()
    {
        $method = filter_input(INPUT_SERVER, 'REQUEST_METHOD');
        switch (strtolower($method)) {
            case 'get':
                return $this->sendResponse($this->get());
            case 'post':
                return $this->sendResponse($this->post($this->readInput()));
            case 'put':
                return $this->sendResponse($this->put($this->readInput()));
            case 'delete':
                return $this->sendResponse($this->delete());
            default:
                header('HTTP/1.0 405 Method Not Allowed');
                echo "Unknown HTTP method";
                break;
        }
    }

    public static function dispatch($class)
    {
        $frames = debug_backtrace();
        if (count($frames) == 1) {
            FannieDispatch::runPage($class);
        }
    }
}