<?php

namespace App\Classes\Modules\Companies\Processors;


use App\Classes\Modules\Companies\DataTransferObjects\CompanyObject;
use App\Classes\Modules\Companies\Services\CreatesCompany;
use App\Classes\Modules\Companies\Services\GeneratesUniqueAccountNumber;
use App\Classes\Modules\Companies\Standards\Rules\CanCreateCompany;
use Illuminate\Http\Request;

class CreateCompanyProcessor
{

    /** @var CanCreateCompany */
    private $canCreateCompany;

    /** @var CreatesCompany */
    private $createsCompany;

    /** @var GeneratesUniqueAccountNumber */
    private $generatesUniqueAccountNumber;

    /**
     * CreateCompanyLogic constructor.
     * @param CanCreateCompany $canCreateCompany
     * @param CreatesCompany $createsCompany
     * @param GeneratesUniqueAccountNumber $generatesUniqueAccountNumber
     */
    public function __construct(CanCreateCompany $canCreateCompany, CreatesCompany $createsCompany, GeneratesUniqueAccountNumber $generatesUniqueAccountNumber)
    {
        $this->canCreateCompany = $canCreateCompany;
        $this->createsCompany = $createsCompany;
        $this->generatesUniqueAccountNumber = $generatesUniqueAccountNumber;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Database\Eloquent\Model|null
     * @throws \App\Classes\Exceptions\AccessForbiddenException
     * @throws \App\Classes\Exceptions\MalformedRequestException
     * @throws \App\Classes\Exceptions\RequestValidationException
     */
    public function execute(Request $request){

        $object = new CompanyObject($this->generatesUniqueAccountNumber->execute(), $request->input('name'), $request->input('type'));

        $this->canCreateCompany->passes($object);

        return $this->createsCompany->execute($object);
    }

}