<?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 App\Models\User;
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->getCompany()->employees()->attach($object->getUser(), ['status' => $object->getStatus()]);

            return $object->getCompany();

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

    }
}