FormFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 44
ccs 9
cts 11
cp 0.8182
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A create() 0 16 3
1
<?php
2
/**
3
 * FormFactory.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Forms!
9
 * @subpackage     common
10
 * @since          5.0
11
 *
12
 * @date           10.06.14
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Forms;
18
19
use Nette\Application;
20
use Nette\DI;
21
use Nette\Localization;
22
23
/**
24
 * Form factory
25
 *
26
 * @package        iPublikuj:Forms!
27
 * @subpackage     common
28
 *
29
 * @author         Adam Kadlec <[email protected]>
30
 */
31 1
final class FormFactory implements IFormFactory
32
{
33
	/**
34
	 * @var Localization\ITranslator|NULL
35
	 */
36
	private $translator;
37
38
	/**
39
	 * @var DI\Container
40
	 */
41
	private $container;
42
43
	/**
44
	 * @param Localization\ITranslator|NULL $translator
45
	 * @param DI\Container $container
46
	 */
47
	public function __construct(
48
		Localization\ITranslator $translator = NULL,
49
		DI\Container $container
50
	) {
51 1
		$this->translator = $translator;
52 1
		$this->container = $container;
53 1
	}
54
55
	/**
56
	 * {@inheritdoc}
57
	 */
58
	public function create(string $formClassName, ...$args) : Application\UI\Form
59
	{
60 1
		if (!class_exists($formClassName)) {
61
			throw new Exceptions\InvalidArgumentException('Factory form class isn\'t defined.');
62
		}
63
64
		/** @var Application\UI\Form $form */
65 1
		$form = $this->container->createInstance($formClassName, $args);
66 1
		$this->container->callInjects($form);
67
68 1
		if ($this->translator instanceof Localization\ITranslator) {
69
			$form->setTranslator($this->translator);
70
		}
71
72 1
		return $form;
73
	}
74
}
75