<commit_before><?php namespace Ovide\Libs\Mvc;

/**
 * Description of RestController
 * @author <NAME> <<EMAIL>>
 */
class RestController extends \Phalcon\Mvc\Controller
{

    const OK              = 200;
    const CREATED         = 201;
    const ACCEPTED        = 202;
    const NO_CONTENT      = 204;

    const MOVED           = 301;
    const NOT_MODIFIED    = 304;

    const BAD_REQUEST     = 400;
    const UNAUTHORIZED    = 401;
    const FORBIDDEN       = 403;
    const NOT_FOUND       = 404;
    const NOT_ALLOWED     = 405;
    const NOT_ACCEPTABLE  = 406;
    const CONFLICT        = 409;
    const GONE            = 410;
    const UNSUPPORTED_MT  = 415;

    const INTERNAL_ERROR  = 500;
    const NOT_IMPLEMENTED = 501;
    const UNAVAILABLE     = 502;

    protected static $status = array(
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        204 => 'No Content',

        301 => 'Moved Permanently',
        304 => 'Not Modified',

        400 => 'Bad Request',
        401 => 'Unauthorized',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method not allowed',
        406 => 'Not Acceptable',
        409 => 'Conflict',
        410 => 'Gone',
        415 => 'Unsupported media type',

        500 => 'Internal server error',
        501 => 'Not implemented',
        503 => 'Service unavailable',
    );


    public function index($id=null)
    {
        $method = $this->request->getMethod();
        try {
            switch ($method) {
                case 'GET':
                    if ($id === '') {
                        $this->_get();
                    } else {
                        $this->_getOne($id);
                    }
                    break;
                case 'POST':
                    $this->_post();
                    break;
                case 'PUT':
                    $this->_put($id);
                    break;
                case 'DELETE':
                    $this->_delete($id);
                    break;
                default:
                    $this->response(['message' => 'Method not allowed'], self::NOT_ALLOWED);
                    break;
            }
        } catch (\Exception $ex) {
            $this->response([
                'message' => $ex->getMessage(),
                'code'    => $ex->getCode(),
                'type'    => \get_class($ex),
                'file'    => $ex->getFile(),
                'line'    => $ex->getLine()
            ], self::INTERNAL_ERROR);
        }
        return $this->response;
    }

    protected function response($content=null, $code=null, $message=null)
    {
        if ($content) {
            self::buildBody($this->response, $content);
            if (!$code) $code = self::OK;
        } else if (!$code) $code = self::NO_CONTENT;
        if (!$message) $message = self::$status[$code];
        $this->response->setStatusCode($code, $message);
    }

    protected static function buildBody(\Phalcon\HTTP\ResponseInterface &$rsp, $content)
    {
        $rsp->setContentType('application/json');
        $rsp->setJsonContent($content);
    }
    
    public static function notFound(\Phalcon\HTTP\ResponseInterface &$rsp)
    {
        $rsp->setStatusCode(self::NOT_FOUND, self::$status[self::NOT_FOUND]);
        self::buildBody($rsp, ['message' => self::$status[self::NOT_FOUND]]);
    }

    protected function _getOne($id)<commit_msg>Add ETag<commit_after><?php namespace Ovide\Libs\Mvc;

/**
 * Description of RestController
 * @author <NAME> <<EMAIL>>
 */
class RestController extends \Phalcon\Mvc\Controller
{

    const OK              = 200;
    const CREATED         = 201;
    const ACCEPTED        = 202;
    const NO_CONTENT      = 204;

    const MOVED           = 301;
    const NOT_MODIFIED    = 304;

    const BAD_REQUEST     = 400;
    const UNAUTHORIZED    = 401;
    const FORBIDDEN       = 403;
    const NOT_FOUND       = 404;
    const NOT_ALLOWED     = 405;
    const NOT_ACCEPTABLE  = 406;
    const CONFLICT        = 409;
    const GONE            = 410;
    const UNSUPPORTED_MT  = 415;

    const INTERNAL_ERROR  = 500;
    const NOT_IMPLEMENTED = 501;
    const UNAVAILABLE     = 502;

    protected static $status = array(
        200 => 'OK',
        201 => 'Created',
        202 => 'Accepted',
        204 => 'No Content',

        301 => 'Moved Permanently',
        304 => 'Not Modified',

        400 => 'Bad Request',
        401 => 'Unauthorized',
        403 => 'Forbidden',
        404 => 'Not Found',
        405 => 'Method not allowed',
        406 => 'Not Acceptable',
        409 => 'Conflict',
        410 => 'Gone',
        415 => 'Unsupported media type',

        500 => 'Internal server error',
        501 => 'Not implemented',
        503 => 'Service unavailable',
    );


    public function index($id=null)
    {
        $method = $this->request->getMethod();
        try {
            switch ($method) {
                case 'GET':
                    if ($id === '') {
                        $this->_get();
                    } else {
                        $this->_getOne($id);
                    }
                    break;
                case 'POST':
                    $this->_post();
                    break;
                case 'PUT':
                    $this->_put($id);
                    break;
                case 'DELETE':
                    $this->_delete($id);
                    break;
                default:
                    $this->response(['message' => 'Method not allowed'], self::NOT_ALLOWED);
                    break;
            }
        } catch (\Exception $ex) {
            $this->response([
                'message' => $ex->getMessage(),
                'code'    => $ex->getCode(),
                'type'    => \get_class($ex),
                'file'    => $ex->getFile(),
                'line'    => $ex->getLine()
            ], ($ex instanceof \RuntimeException) ? $ex->getCode() : self::INTERNAL_ERROR);
        }
        return $this->response;
    }

    protected function response($content=null, $code=null, $message=null)
    {
        if ($content) {
            $et = $this->request->getHeader('ETAG');
            $net = md5(serialize($content));
            $this->response->setHeader('ETag', $net);
            if ($et === $net) {
                return $this->response(null, self::NOT_MODIFIED);
            }
            self::buildBody($this->response, $content);
            if (!$code) $code = self::OK;
        } else if (!$code) $code = self::NO_CONTENT;
        if (!$message) $message = self::$status[$code];
        $this->response->setStatusCode($code, $message);
    }

    protected static function buildBody(\Phalcon\HTTP\ResponseInterface &$rsp, $content)
    {
        $rsp->setContentType('application/json');
        $rsp->setJsonContent($content);
    }
    
    public static function notFound(\Phalcon\HTTP\ResponseInterface &$rsp)
    {
        $rsp->setStatusCode(self::NOT_FOUND, self::$status[self::NOT_FOUND]);
        self::buildBody($rsp, ['message' => self::$status[self::NOT_FOUND]]);
    }

    protected function _getOne($id)
{'fraction_non_alphanumeric': 0.1404658948249407, 'fraction_numerical': 0.03026921467429209, 'mean_word_length': 1.5389518413597734, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3191757', 'n_tokens_mistral': 2236, 'n_tokens_neox': 2007, 'n_words': 452}