<?php

declare(strict_types=1);

namespace App\Framework\Database\Example;

use App\Framework\Database\Repository\EntityRepository;

/**
 * Beispiel für Repository mit User-Entity
 */
final class UserRepository extends EntityRepository
{
    protected string $entityClass = User::class;

    /**
     * Findet User nach Email
     */
    public function findByEmail(string $email): ?User
    {
        return $this->findOneBy(['email' => $email]);
    }

    /**
     * Erstellt einen neuen User
     */
    public function create(string $name, ?string $email = null): User
    {
        $user = new User($name, $email);
        return $this->save($user);
    }

    /**
     * Ändert den Namen eines Users
     */
    public function changeName(User $user, string $newName): User
    {
        $updatedUser = $user->withName($newName);
        return $this->save($updatedUser);
    }

    /**
     * Ändert die Email eines Users
     */
    public function changeEmail(User $user, ?string $newEmail): User
    {
        $updatedUser = $user->withEmail($newEmail);
        return $this->save($updatedUser);
    }
}
