<?php

namespace App\Classes\Modules\Companies\Services;

use App\Classes\General\Eloquent\AbstractUpdateRecord;
use App\Classes\Modules\Companies\DataTransferObjects\CompanyProfileObject;
use App\Models\Company;

class UpdatesCompanyProfile extends AbstractUpdateRecord
{

    /**
     * @param Company $model
     * @param CompanyProfileObject $object
     * @return \Illuminate\Database\Eloquent\Model
     * @throws \App\Classes\Exceptions\MalformedRequestException
     */
    public function execute(Company $model, CompanyProfileObject $object)
    {
        $model->type = $object->getCompanyType();
        
        $contact = $model->contacts()->first();
        if ($contact) {
            $contact->email = $object->getEmail();
            $contact->phone = $object->getPhone();
            $contact->save();
        }

        return $this->handler($model);

    }
}