1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 
<?php

declare(strict_types=1);

namespace Wtf;

class Auth extends Root
{
    /**
     * Log in user.
     *
     * Return result, based by selected session storage
     *
     * @param string $login
     * @param string $password
     *
     * @return mixed
     */
    public function login(string $login, string $password)
    {
        $user = $this->auth_repository->login($login, $password);

        if (null === $user) {
            return null;
        }

        if ($this->container->has('user')) {
            unset($this->container['user']);
        }
        $this->container['user'] = $user;

        return $this->auth_storage->setUser($user);
    }

    /**
     * Generate special code for user who forgot password.
     *
     * @return string code
     */
    public function forgot(string $login): string
    {
        return $this->auth_repository->forgot($login);
    }

    /**
     * Reset user password by code.
     *
     * @param string $code         Return value of self::forgot()
     * @param string $new_password New password for user
     *
     * @return bool
     */
    public function reset(string $code, string $new_password): bool
    {
        return $this->auth_repository->reset($code, $new_password);
    }

    /**
     * Check if current user is logged in.
     *
     * @return bool
     */
    public function isLoggedIn(): bool
    {
        return $this->auth_storage->isLoggedIn();
    }

    /**
     * Get current user.
     *
     * @return null|Root
     */
    public function getUser(): ?Root
    {
        return $this->auth_storage->getUser();
    }

    public function logout(): void
    {
        $this->auth_storage->logout();
    }
}