<?php

namespace App\Classes\Modules\Accounts\Services;


use App\Classes\Exceptions\MalformedRequestException;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\QueryException;
use Illuminate\Support\Str;

class GeneratesEmailVerificationAttempt
{

    /**
     * @param User $user
     * @return Model
     * @throws MalformedRequestException
     */
    public function execute(User $user): Model {
        try {

            return $user->emailVerification()->create([
                'token'          => Str::random(25),
                'is_complete'    => false,
                'is_active'      => true,
                'is_sent'        => true
            ]);

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

}
