<?php

namespace App\Classes\Modules\Milestones\Processors;

use App\Classes\Modules\Milestones\DataTransferObjects\MilestoneObject;
use App\Classes\Modules\Milestones\Services\CreatesMilestone;
use App\Classes\Modules\Milestones\Standards\Rules\CanCreateMilestone;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

class CreateMilestoneProcessor
{
    /** @var CreatesMilestone */
    private $createsMilestone;

    /** @var CanCreateMilestone */
    private $canCreateMilestone;

    /**
     * CreateMilestoneProcessor constructor.
     * @param CreatesMilestone $createsMilestone
     * @param CanCreateMilestone $canCreateMilestone
     */
    public function __construct(CanCreateMilestone $canCreateMilestone, CreatesMilestone $createsMilestone)
    {
        $this->canCreateMilestone = $canCreateMilestone;
        $this->createsMilestone = $createsMilestone;
    }


    /**
     * @param Request $request
     * @return Model
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(Request $request): Model
    {
        $userMilestoneObject = new MilestoneObject(
            $request->input('id'),
            $request->input('name'),
            $request->input('description')
        );

        $this->canCreateMilestone->passes($userMilestoneObject);

        return $this->createsMilestone->execute($userMilestoneObject);
    }

}
