<?php
namespace Nibiru\Module\Users;
/**
 * @desc this file is for the autoloader to function properly,
 * you might as well use it as the primary user class for your
 * application, the interface an the trait are currently implemented
 * Created by PhpStorm.
 * User: kasdorf
 * Date: 28.08.18
 * Time: 11:22
 */
use Nibiru\Module\Users\Interfaces;
use Nibiru\Module\Users\Traits;
use Nibiru\Registry;
use SplSubject;
use SplObserver;
use SplObjectStorage;

class Users implements Interfaces\Users, SplSubject
{
    use Traits\Users;
    protected static object $usersRegistry;
    protected SplObjectStorage $observers;
    /**
     * Users constructor.
     */
    public function __construct()
    {
        $this->setUsersRegistry();
        $this->observers = new SplObjectStorage();
    }

    /**
     * @desc will attach an observer to the observer storage
     * @param SplObserver $observer
     * @return void
     */
    public function attach(SplObserver $observer): void
    {
        $this->observers->attach($observer);
    }

    /**
     * @desc will detach an observer from the observer storage
     * @param SplObserver $observer
     * @return void
     */
    public function detach(SplObserver $observer): void
    {
        $this->observers->detach($observer);
    }

    /**
     * @desc will notify all observers
     * @return void
     */
    public function notify(): void
    {
        foreach ($this->observers as $observer) {
            $observer->update($this);
        }
    }

    /**
     * @desc will return the user registry object from the users.ini configuration
     * @return object
     */
    protected static function getUsersRegistry(): object
    {
        return self::$usersRegistry;
    }

    /**
     * @desc setter for the user Registry
     */
    protected function setUsersRegistry(): void
    {
        self::$usersRegistry = Registry::getInstance()->loadModuleConfigByName(self::CONFIG_MODULE_NAME);
    }
}