<?php

namespace App\Classes\Modules\Companies\Services;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\Modules\Companies\DataTransferObjects\EmploymentObject;
use Illuminate\Database\QueryException;

class AssignsEmployee extends AbstractUpdateRecord
{
    /**
     * @param EmploymentObject $object
     * @return \Illuminate\Database\Eloquent\Model
     * @throws MalformedRequestException
     */
    public function execute(EmploymentObject $object)
    {
        try {

            $object->getCompanyModule()->employees()->attach($object->getUser(), ['status' => $object->getStatus(),'role_id' => $object->getRoleId()]);

            return $object->getCompanyModule();

        } catch (QueryException $exception){
            throw new MalformedRequestException($exception);
        }

    }
}