<?php


namespace App\Model\Facades\Readers;


use App\Model\Entities\Security\User;
use App\Model\Services\GameService;
use App\Model\Services\UserService;

class UserReader extends BaseReader
{
    /** @var UserService */
    protected $userService;
    /** @var GameService */
    protected $gameService;

    public function __construct(UserService $userService, GameService $gameService)
    {
        $this->userService = $userService;
        $this->gameService = $gameService;
    }

    /**
     * @param int $id
     * @return User
     */
    public function findUserById(int $id) : User
    {
        $user = $this->userService->findUserById($id);
        $this->validateEntity($user);

        return $user;
    }

    public function validateUser($user) : User
    {
        if ($user instanceof User) {
            return $user;
        }

        return $this->findUserById($user);
    }

    public function getUsersGames($user)
    {
        $userEntity = $this->validateUser($user);

        return $this->gameService->getUsersGames($userEntity);
    }
}