<?php

namespace App\Classes\Modules\Vouchers\Services;


use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\Modules\Vouchers\DataTransferObjects\VoucherCampaignObject;
use App\Models\VoucherCampaign;

class CreatesVoucherCampaign extends AbstractUpdateRecord
{
    /** @var CheckIfVoucherCampaignExists */
    private $campaignExists;

    /**
     * CreatesVoucherCampaign constructor.
     * @param CheckIfVoucherCampaignExists $campaignExists
     */
    public function __construct(CheckIfVoucherCampaignExists $campaignExists)
    {
        $this->campaignExists = $campaignExists;
    }

    /**
     * @param VoucherCampaignObject $object
     * @return \Illuminate\Database\Eloquent\Model|null
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(VoucherCampaignObject $object) {
        if(!$this->campaignExists->execute($object->getCampaignId()))
        {
            $model = new VoucherCampaign();
            $model->campaign_id = $object->getCampaignId();
            $model->name = $object->getName();
            $model->description = $object->getDescription();
            $model->limit_per_month = $object->getLimitPerMonth();
            return $this->handler($model);
        }
        return null;
    }
}
