<?php

namespace App\Middleware;

use Snaju\Inception\Http\Middleware\MiddlewareInterface;
use Snaju\Inception\Http\Request\WebRequest;
use Snaju\Inception\Http\Response\ResponseHandler;

class AfterRouteMiddleware implements MiddlewareInterface
{

    public function process(WebRequest $request, \Closure $next)
    {
        // Call the next middleware and pass the request object
        $response = $next($request);

        // YOUR CODE GOES HERE. DO DB QUERY, CHANGE RESPONSE, ETC.
        // TODO: Do some process here for route middleware (after)...

        // This will run in sequence with other middleware located in the
        // array on the controller method that has been configured
        // to run as "after" middleware, right after the core of the application
        // before global middleware. So even if first in the entire group, it will
        // run after the main processing, but in sequence with all other "after" middleware.

        // Here is an example of changing the response to set the status code
        // as 404 and merge data with existing data. The `setData` method
        // code be used to clear all data and set new data specified if needed.

        $handler = ResponseHandler::getInstance();
        $handler->make()
            ->setStatusCode(404)
            ->addData([
                'testing' => 'testing middleware. this data was force set in the "AfterRouteMiddleware.php file in \app\middleware',
            ]);

        // Return the response when finished
        return $response;
    }

}