<?php

namespace App\Classes\Modules\Companies\Services;

use App\Classes\Exceptions\MalformedRequestException;
use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Models\Company;
use App\Models\Segment;
use Illuminate\Database\QueryException;

class AssignsCompanyToSegment extends AbstractUpdateRecord
{
    /**
     * @param Company $company
     * @param Segment $segment
     * @return \Illuminate\Database\Eloquent\Model
     * @throws MalformedRequestException
     */
    public function execute(Company $company, Segment $segment)
    {
        try {

            $company->segments()->attach($segment);

            return $company;

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


    }
}