<?php

declare(strict_types=1);

namespace Apexl\Io\User\Commands;

use Apexl\Io\Exceptions\RecordNotFoundException;
use Apexl\Io\System\Commands\DaedalusCommand;
use Apexl\Io\User\Entities\UserEntity;

abstract class AbstractUserCommand extends DaedalusCommand
{
    /**
     * @throws RecordNotFoundException
     */
    protected function getUser(string $idOrEmail): UserEntity
    {
        $user = new UserEntity();

        if (is_numeric($idOrEmail)) {
            $user->load($idOrEmail);
            if ($user->id === null) {
                throw new RecordNotFoundException();
            }

            return $user;
        }

        $user->loadBy('email', $idOrEmail);

        return $user;
    }
}
