Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| AccountManager | |
100.00% |
16 / 16 |
|
100.00% |
5 / 5 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| add | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| remove | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Account |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Account; |
| 16 | |
| 17 | use phpOMS\Auth\Auth; |
| 18 | use phpOMS\DataStorage\Session\SessionInterface; |
| 19 | |
| 20 | /** |
| 21 | * Account manager class. |
| 22 | * |
| 23 | * The account manager is used to manage accounts. |
| 24 | * |
| 25 | * @package phpOMS\Account |
| 26 | * @license OMS License 2.0 |
| 27 | * @link https://jingga.app |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | final class AccountManager implements \Countable |
| 31 | { |
| 32 | /** |
| 33 | * Accounts. |
| 34 | * |
| 35 | * @var Account[] |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | private array $accounts = []; |
| 39 | |
| 40 | /** |
| 41 | * Session. |
| 42 | * |
| 43 | * @var SessionInterface |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | private SessionInterface $session; |
| 47 | |
| 48 | /** |
| 49 | * Constructor. |
| 50 | * |
| 51 | * @param SessionInterface $session Session |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public function __construct(SessionInterface $session) |
| 56 | { |
| 57 | $this->session = $session; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get account. |
| 62 | * |
| 63 | * @param int $id Account id |
| 64 | * |
| 65 | * @return Account |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | public function get(int $id = 0) : Account |
| 70 | { |
| 71 | if ($id === 0) { |
| 72 | $account = new Account(Auth::authenticate($this->session)); |
| 73 | |
| 74 | if (!isset($this->accounts[$account->getId()])) { |
| 75 | $this->accounts[$account->getId()] = $account; |
| 76 | } |
| 77 | |
| 78 | return $account; |
| 79 | } |
| 80 | |
| 81 | return $this->accounts[$id] ?? new NullAccount(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Add account. |
| 86 | * |
| 87 | * @param Account $account Account |
| 88 | * |
| 89 | * @return bool Returns true if the account could be added otherwise false is returned |
| 90 | * |
| 91 | * @since 1.0.0 |
| 92 | */ |
| 93 | public function add(Account $account) : bool |
| 94 | { |
| 95 | if (!isset($this->accounts[$account->getId()])) { |
| 96 | $this->accounts[$account->getId()] = $account; |
| 97 | |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Remove account. |
| 106 | * |
| 107 | * @param int $id Account id |
| 108 | * |
| 109 | * @return bool Returns true if the account could be removed otherwise false |
| 110 | * |
| 111 | * @since 1.0.0 |
| 112 | */ |
| 113 | public function remove(int $id) : bool |
| 114 | { |
| 115 | if (isset($this->accounts[$id])) { |
| 116 | unset($this->accounts[$id]); |
| 117 | |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get accounts count. |
| 126 | * |
| 127 | * @return int Returns the amount of accounts in the manager (>= 0) |
| 128 | * |
| 129 | * @since 1.0.0 |
| 130 | */ |
| 131 | public function count() : int |
| 132 | { |
| 133 | return \count($this->accounts); |
| 134 | } |
| 135 | } |