This is a piece of code THAT I wrote this morning

Part of an API project

Updates the value of the status field of a record

class State extends Base
{
    / / operation
    private $action = [
        'keep'= > [0.2]./ / 0 = > 2
        'back'= > [2.0]./ / 2 = > 0
        'fail'= > [1.4]./ / 1 = > 4
        'lose'= > [3.4]  / / 3 = > 4
    ];

    function __construct(ContainerInterface $app)
    {
        parent::__construct($app);
    }

    public function __invoke($request, $response, $args)
    {
        $json = [];

        if(! array_key_exists($args['action'].$this->action) ) {
            return $this->respond(20[],'Illegal operation, optional action values are: keep, back, fail, lose.');
        }

        $query = $this->app->db->table('record')
            ->where(
                [
                    ['ruid', $args['ruid']],
                    ['status'.$this->action[$args['action']] [0]]
                ]
            )
            ->update(['status'= >$this->action[$args['action']] [1]]);

        if ($query) {
            return $this->respond(0, $json);
        }

        return $this->respond(21[],'Operation failed.'); }}Copy the code