<?php

namespace App\Classes\Modules\Companies\Services;

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

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

            $companyConnection->segments()->detach($segment);

            return $companyConnection;

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


    }
}