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

AccountCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 57
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 4 1
A account() 0 7 2
A get() 0 7 2
A getDefault() 0 7 2
A addAccount() 0 9 2
A getIterator() 0 4 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