<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Money
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (($request->method() != 'POST') && ($request->method() != 'PUT')) {
            return $next($request);
        }
        $parameters = [
            'amount','price', 'price_two'
        ];
        foreach ($parameters as $parameter) {
            if (!$request->has($parameter)) {
                continue;
            }
            $amount= $request->get($parameter);
            $amount = $amount ? intval($amount)*100:null;
            $request->request->set($parameter, $amount);
        }
        return $next($request);
    }
}
