<?php

namespace App\Classes\Modules\Accounts\Processors;

use App\Classes\Modules\Accounts\DataTransferObjects\RegistrationObject;
use App\Classes\Modules\Accounts\Services\CreatesUser;
use App\Classes\Modules\Accounts\Standards\Rules\CanRegisterUser;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

class CreateUserProcessor
{
    /** @var CanRegisterUser */
    private $canRegisterUser;

    /** @var CreatesUser */
    private $createsUser;

    /**
     * CreateUserProcessor constructor.
     * @param CanRegisterUser $canRegisterUser
     * @param CreatesUser $createsUser
     */
    public function __construct(CanRegisterUser $canRegisterUser, CreatesUser $createsUser)
    {
        $this->canRegisterUser = $canRegisterUser;
        $this->createsUser = $createsUser;
    }


    /**
     * @param Request $request
     * @param $roleType
     * @param $status
     * @return Model
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(Request $request, $roleType, $status): Model {

        $userObject = new RegistrationObject($request->input('name'), $request->input('email'),
            $request->input('password'), $request->input('password_confirmation'),
            $roleType, $status);

        $this->canRegisterUser->passes($userObject);

        /** @var User $user_query */
        return $this->createsUser->execute($userObject);

    }

}