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

FioFactory::transactionClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace h4kuna\Fio\Utils;
4
5
use h4kuna\Fio;
6
use h4kuna\Fio\Account;
7
use h4kuna\Fio\Response\Read\Transaction;
8
9
class FioFactory
10
{
11
12
	/** @var Account\AccountCollection */
13
	private $accountCollection;
14
15
	/** @var Fio\Request\IQueue */
16
	private $queue;
17
18
	/** @var string */
19
	private $transactionClass;
20
21
	/** @var string */
22
	protected $temp;
23
24
25
	public function __construct(array $accounts, string $transactionClass = Transaction::class, string $temp = '')
26
	{
27
		$this->setTemp($temp);
28
		$this->accountCollection = $this->createAccountCollection($accounts);
29
		$this->queue = $this->createQueue();
30
		$this->transactionClass = $transactionClass;
31
	}
32
33
34
	private function setTemp(string $temp): void
35
	{
36
		if ($temp === '') {
37
			$temp = sys_get_temp_dir();
38
		}
39
40
		$this->temp = $temp;
41
	}
42
43
44
	public function createFioRead(string $name = ''): Fio\FioRead
45
	{
46
		return new Fio\FioRead($this->queue(), $this->accountCollection()->account($name), $this->createReader());
47
	}
48
49
50
	/**
51
	 * @param string $name Configured account name from AccountCollection
52
	 * @return Fio\FioPay
53
	 */
54
	public function createFioPay(string $name = ''): Fio\FioPay
55
	{
56
		return new Fio\FioPay(
57
			$this->queue(), $this->accountCollection()->account($name), $this->createXmlFile()
58
		);
59
	}
60
61
62
	/**
63
	 * COMMON ******************************************************************
64
	 * *************************************************************************
65
	 */
66
	protected function createQueue(): Fio\Request\IQueue
67
	{
68
		return new Fio\Request\Queue($this->temp);
69
	}
70
71
72
	protected function createAccountCollection(array $accounts): Account\AccountCollection
73
	{
74
		return Account\AccountCollectionFactory::create($accounts);
75
	}
76
77
78
	final protected function accountCollection(): Account\AccountCollection
79
	{
80
		return $this->accountCollection;
81
	}
82
83
84
	final protected function queue(): Fio\Request\IQueue
85
	{
86
		return $this->queue;
87
	}
88
89
90
	final protected function transactionClass(): string
91
	{
92
		return $this->transactionClass;
93
	}
94
95
96
	/**
97
	 * READ ********************************************************************
98
	 * *************************************************************************
99
	 */
100
	protected function createTransactionListFactory(): Fio\Response\Read\JsonTransactionFactory
101
	{
102
		return new Fio\Response\Read\JsonTransactionFactory($this->transactionClass());
103
	}
104
105
106
	protected function createReader(): Fio\Request\Read\Files\Json
107
	{
108
		return new Fio\Request\Read\Files\Json($this->createTransactionListFactory());
109
	}
110
111
112
	/**
113
	 * PAY *********************************************************************
114
	 * *************************************************************************
115
	 */
116
	protected function createXmlFile(): Fio\Request\Pay\XMLFile
117
	{
118
		return new Fio\Request\Pay\XMLFile($this->temp);
119
	}
120
121
}
122