<?php

namespace App\Classes\Modules\Transports\ControllersLogic;


use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Transports\Services\FetchesTransport;
use App\Classes\Modules\Transports\Standards\Rules\CanFetchTransport;
use App\Http\Resources\TransportResource;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FetchTransportLogic extends AbstractControllerLogic
{

    /**
     * @return array
     */
    protected function notification():array {
        return [
            'title' => 'Retrieved Transport',
            'message' => 'You have successfully retrieved a Transport'
        ];
    }

    /** @var CanFetchTransport */
    private $canFetchTransport;

    /** @var FetchesTransport */
    private $fetchesTransport;

    /**
     * FetchTransportControllersLogic constructor.
     * @param CanFetchTransport $canFetchTransport
     * @param FetchesTransport $fetchesTransport
     */
    public function __construct(CanFetchTransport $canFetchTransport, FetchesTransport $fetchesTransport)
    {
        $this->canFetchTransport = $canFetchTransport;
        $this->fetchesTransport = $fetchesTransport;
    }


    /**
     * @param Request $request
     * @return JsonResponse
     * @throws ErrorException
     */
    public function logic(Request $request) : JsonResponse
    {
        $this->canFetchTransport->passes();

        $query = $this->fetchesTransport->execute(['id' => $request->route('id')]);

        return $this->resourceResponse(new TransportResource($query));
    }

}
