Completed
Push — master ( 589fec...539028 )
by Milan
07:58
created

AccountCollection::account()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace h4kuna\Fio\Account;
4
5
use h4kuna\Fio\Exceptions;
6
7
/**
8
 * @author Tomáš Jacík
9
 */
10
class AccountCollection implements \Countable, \IteratorAggregate
11
{
12
13
	/** @var FioAccount[] */
14
	private $accounts = [];
15
16
17
	public function account(string $alias = ''): FioAccount
18
	{
19
		if ($alias === '') {
20
			return $this->getDefault();
21
		}
22
		return $this->get($alias);
23
	}
24
25
26
	private function get(string $alias): FioAccount
27
	{
28
		if (isset($this->accounts[$alias])) {
29
			return $this->accounts[$alias];
30
		}
31
		throw new Exceptions\InvalidArgument('This account alias does not exists: ' . $alias);
32
	}
33
34
35
	private function getDefault(): FioAccount
36
	{
37
		if ($this->accounts === []) {
38
			throw new Exceptions\InvalidState('Missing account, let\'s fill in configuration.');
39
		}
40
		return reset($this->accounts);
41
	}
42
43
44
	public function addAccount(string $alias, FioAccount $account): AccountCollection
45
	{
46
		if (isset($this->accounts[$alias])) {
47
			throw new Exceptions\InvalidArgument('This alias already exists: ' . $alias);
48
		}
49
50
		$this->accounts[$alias] = $account;
51
		return $this;
52
	}
53
54
55
	public function count(): int
56
	{
57
		return count($this->accounts);
58
	}
59
60
61
	public function getIterator(): \ArrayIterator
62
	{
63
		return new \ArrayIterator($this->accounts);
64
	}
65
66
}
67