<?php

namespace App\Http\Middleware;

use Closure;
use App\Classes\Modules\Transactions\Processors\CheckStorageInvoiceTransactionProcessor;
use Illuminate\Http\Request;

class CheckForStorageInvoiceByOrderId
{

    /** @var CheckStorageInvoiceTransactionProcessor */
    private $storageInvoiceTransactionProcessor;


    public function __construct(CheckStorageInvoiceTransactionProcessor $storageInvoiceTransactionProcessor)
    {
        $this->storageInvoiceTransactionProcessor = $storageInvoiceTransactionProcessor;
    }


    /**
     * Handle an incoming request.
     *
     * @param  Request $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        $orderId = $request->route('id');
        $storages = $this->storageInvoiceTransactionProcessor->execute($orderId);
        $request->merge(['storages' => $storages]);
        return $next($request);
    }
}
