<?php

namespace App\Classes\Modules\Announcements\Services;

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

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

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

            return $announcement;

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


    }
}