Completed
Push — master ( 4f2cea...0c21fc )
by Milan
02:45
created

FioFactory::setTemp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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