phpOMS/Account/AccountManager.php
Dennis Eichhorn 0a8a5c63c5
Some checks failed
Compress images / calibreapp/image-actions (push) Has been cancelled
CI / general_module_workflow_php (push) Has been cancelled
fix permissions
2025-04-02 14:15:07 +00:00

136 lines
2.5 KiB
PHP

<?php
/**
* Jingga
*
* PHP Version 8.2
*
* @package phpOMS\Account
* @copyright Dennis Eichhorn
* @license OMS License 2.2
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
namespace phpOMS\Account;
use phpOMS\Auth\Auth;
use phpOMS\DataStorage\Session\SessionAbstract;
/**
* Account manager class.
*
* The account manager is used to manage accounts.
*
* @package phpOMS\Account
* @license OMS License 2.2
* @link https://jingga.app
* @since 1.0.0
*/
final class AccountManager implements \Countable
{
/**
* Accounts.
*
* @var Account[]
* @since 1.0.0
*/
private array $accounts = [];
/**
* Session.
*
* @var SessionAbstract
* @since 1.0.0
*/
private SessionAbstract $session;
/**
* Constructor.
*
* @param SessionAbstract $session Session
*
* @since 1.0.0
*/
public function __construct(SessionAbstract $session)
{
$this->session = $session;
}
/**
* Get account.
*
* @param int $id Account id
*
* @return Account
*
* @since 1.0.0
*/
public function get(int $id = 0) : Account
{
if ($id === 0) {
$account = new Account(Auth::authenticate($this->session));
if (!isset($this->accounts[$account->id])) {
$this->accounts[$account->id] = $account;
}
return $account;
}
return $this->accounts[$id] ?? new NullAccount();
}
/**
* Add account.
*
* @param Account $account Account
*
* @return bool Returns true if the account could be added otherwise false is returned
*
* @since 1.0.0
*/
public function add(Account $account) : bool
{
if (!isset($this->accounts[$account->id])) {
$this->accounts[$account->id] = $account;
return true;
}
return false;
}
/**
* Remove account.
*
* @param int $id Account id
*
* @return bool Returns true if the account could be removed otherwise false
*
* @since 1.0.0
*/
public function remove(int $id) : bool
{
if (isset($this->accounts[$id])) {
unset($this->accounts[$id]);
return true;
}
return false;
}
/**
* Get accounts count.
*
* @return int Returns the amount of accounts in the manager (>= 0)
*
* @since 1.0.0
*/
public function count() : int
{
return \count($this->accounts);
}
}