<?php

namespace App\Classes\Modules\Segments\Services;

use App\Classes\General\Eloquent\AbstractUpdateRelationshipRecord;
use App\Classes\Modules\Segments\DataTransferObjects\ConstantObject;
use App\Models\SegmentConstant;
use App\Models\Segment;

class CreatesConstant extends AbstractUpdateRelationshipRecord
{
    /**
     * @param Segment $segment
     * @param ConstantObject $object
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Segment $segment, ConstantObject $object)
    {
        $model = new SegmentConstant();
        $model->name = $object->getName();
        $model->reference = $object->getReference();
        $model->detail = json_encode($object->getDetail());

        return $this->handler($segment->constants(), $model);
    }
}