<?php

namespace App\Core\Auth\Classes;

use Singleton;

/**
 * Store the user auth and permissions on a singleton
 * so this will be used on ws and web requests
 */
class AuthenticatedUser extends Singleton
{

    private $user;
    private $permissions;

    public static function load(User $user = null)
    {
        $instance = self::getInstance();
        if ($user) {
            $instance->user = $user;
            $instance->permissions = $user->permissions();
        }

        if ($_SESSION['user']) {
            $instance->user = $_SESSION['user'];
            $instance->permissions = $_SESSION['user']['permission'];
        }

        return $instance;
    }

    public static function user()
    {
        return self::getInstance()->user;
    }

    public static function permissions()
    {
        return self::getInstance()->permissions;
    }
}
