EntityConfigurationValueType   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 20
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A buildForm() 0 9 3
1
<?php
2
3
/*
4
 * This file is part of the OneGuard DynamicConfigurationBundle.
5
 *
6
 * (c) OneGuard <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace OneGuard\Bundle\DynamicConfigurationBundle\Form;
13
14
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
15
use Symfony\Bridge\Doctrine\RegistryInterface;
16
use Symfony\Component\Form\CallbackTransformer;
17
use Symfony\Component\Form\FormBuilderInterface;
18
19
class EntityConfigurationValueType extends EntityType {
20
	/**
21
	 * @var RegistryInterface
22
	 */
23
	private $doctrine;
24
25
	public function __construct(RegistryInterface $doctrine) {
26
		parent::__construct($doctrine);
27
		$this->doctrine = $doctrine;
28
	}
29
30
	public function buildForm(FormBuilderInterface $builder, array $options) {
31
		parent::buildForm($builder, $options);
32
		$builder->addModelTransformer(new CallbackTransformer(
33
			function ($id) use ($options) {
34
				return $id === null ? null : $this->doctrine->getRepository($options['class'])->find($id);
35
			},
36
			function ($entity) use ($options) {
37
				return $entity === null ?
38
					null : $this->doctrine->getEntityManagerForClass($options['class'])->getUnitOfWork()->getSingleIdentifierValue($entity);
39
			}
40
		));
41
	}
42
}
43