@@ -12,54 +12,54 @@ |
||
| 12 | 12 | namespace OneGuard\Bundle\DynamicConfigurationBundle; |
| 13 | 13 | |
| 14 | 14 | class DefinitionRegistry { |
| 15 | - /** |
|
| 16 | - * @var Definition[] |
|
| 17 | - */ |
|
| 18 | - private $definitions = []; |
|
| 15 | + /** |
|
| 16 | + * @var Definition[] |
|
| 17 | + */ |
|
| 18 | + private $definitions = []; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * @param Definition $definition |
|
| 22 | - * @throws \InvalidArgumentException |
|
| 23 | - */ |
|
| 24 | - public function register(Definition $definition) { |
|
| 25 | - if (array_key_exists($definition->getKey(), $this->definitions)) { |
|
| 26 | - throw new \InvalidArgumentException('Definition already exists (Key: ' . $definition->getKey() . ').'); |
|
| 27 | - } |
|
| 28 | - $this->definitions[$definition->getKey()] = $definition; |
|
| 29 | - } |
|
| 20 | + /** |
|
| 21 | + * @param Definition $definition |
|
| 22 | + * @throws \InvalidArgumentException |
|
| 23 | + */ |
|
| 24 | + public function register(Definition $definition) { |
|
| 25 | + if (array_key_exists($definition->getKey(), $this->definitions)) { |
|
| 26 | + throw new \InvalidArgumentException('Definition already exists (Key: ' . $definition->getKey() . ').'); |
|
| 27 | + } |
|
| 28 | + $this->definitions[$definition->getKey()] = $definition; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param string $key |
|
| 33 | - * @return Definition |
|
| 34 | - * @throws \InvalidArgumentException |
|
| 35 | - */ |
|
| 36 | - public function get(string $key) : Definition { |
|
| 37 | - if (!array_key_exists($key, $this->definitions)) { |
|
| 38 | - throw new \InvalidArgumentException('Definition not found (Key: ' . $key . ').'); |
|
| 39 | - } |
|
| 40 | - return $this->definitions[$key]; |
|
| 41 | - } |
|
| 31 | + /** |
|
| 32 | + * @param string $key |
|
| 33 | + * @return Definition |
|
| 34 | + * @throws \InvalidArgumentException |
|
| 35 | + */ |
|
| 36 | + public function get(string $key) : Definition { |
|
| 37 | + if (!array_key_exists($key, $this->definitions)) { |
|
| 38 | + throw new \InvalidArgumentException('Definition not found (Key: ' . $key . ').'); |
|
| 39 | + } |
|
| 40 | + return $this->definitions[$key]; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @param string $key |
|
| 45 | - * @return bool |
|
| 46 | - */ |
|
| 47 | - public function has(string $key) : bool { |
|
| 48 | - return array_key_exists($key, $this->definitions); |
|
| 49 | - } |
|
| 43 | + /** |
|
| 44 | + * @param string $key |
|
| 45 | + * @return bool |
|
| 46 | + */ |
|
| 47 | + public function has(string $key) : bool { |
|
| 48 | + return array_key_exists($key, $this->definitions); |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @return string[] |
|
| 53 | - */ |
|
| 54 | - public function keys() : array { |
|
| 55 | - return array_keys($this->definitions); |
|
| 56 | - } |
|
| 51 | + /** |
|
| 52 | + * @return string[] |
|
| 53 | + */ |
|
| 54 | + public function keys() : array { |
|
| 55 | + return array_keys($this->definitions); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function registerEntity($key, $class, $choiceLabel) { |
|
| 59 | - $this->register(new EntityDefinition($key, $class, $choiceLabel)); |
|
| 60 | - } |
|
| 58 | + public function registerEntity($key, $class, $choiceLabel) { |
|
| 59 | + $this->register(new EntityDefinition($key, $class, $choiceLabel)); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - public function registerString($key) { |
|
| 63 | - $this->register(new StringDefinition($key)); |
|
| 64 | - } |
|
| 62 | + public function registerString($key) { |
|
| 63 | + $this->register(new StringDefinition($key)); |
|
| 64 | + } |
|
| 65 | 65 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | */ |
| 24 | 24 | public function register(Definition $definition) { |
| 25 | 25 | if (array_key_exists($definition->getKey(), $this->definitions)) { |
| 26 | - throw new \InvalidArgumentException('Definition already exists (Key: ' . $definition->getKey() . ').'); |
|
| 26 | + throw new \InvalidArgumentException('Definition already exists (Key: '.$definition->getKey().').'); |
|
| 27 | 27 | } |
| 28 | 28 | $this->definitions[$definition->getKey()] = $definition; |
| 29 | 29 | } |
@@ -35,7 +35,7 @@ discard block |
||
| 35 | 35 | */ |
| 36 | 36 | public function get(string $key) : Definition { |
| 37 | 37 | if (!array_key_exists($key, $this->definitions)) { |
| 38 | - throw new \InvalidArgumentException('Definition not found (Key: ' . $key . ').'); |
|
| 38 | + throw new \InvalidArgumentException('Definition not found (Key: '.$key.').'); |
|
| 39 | 39 | } |
| 40 | 40 | return $this->definitions[$key]; |
| 41 | 41 | } |
@@ -26,94 +26,94 @@ |
||
| 26 | 26 | * @Route(path="/configuration-value") |
| 27 | 27 | */ |
| 28 | 28 | class ConfigurationValueController extends Controller { |
| 29 | - /** |
|
| 30 | - * @var \Doctrine\ORM\EntityManagerInterface |
|
| 31 | - */ |
|
| 32 | - private $entityManager; |
|
| 29 | + /** |
|
| 30 | + * @var \Doctrine\ORM\EntityManagerInterface |
|
| 31 | + */ |
|
| 32 | + private $entityManager; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var DefinitionRegistry |
|
| 36 | - */ |
|
| 37 | - private $registry; |
|
| 34 | + /** |
|
| 35 | + * @var DefinitionRegistry |
|
| 36 | + */ |
|
| 37 | + private $registry; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @var ConfigurationResolverFactory |
|
| 41 | - */ |
|
| 42 | - private $factory; |
|
| 39 | + /** |
|
| 40 | + * @var ConfigurationResolverFactory |
|
| 41 | + */ |
|
| 42 | + private $factory; |
|
| 43 | 43 | |
| 44 | - public function __construct( |
|
| 45 | - RegistryInterface $doctrine, |
|
| 46 | - DefinitionRegistry $registry, |
|
| 47 | - ConfigurationResolverFactory $factory |
|
| 48 | - ) { |
|
| 49 | - $this->entityManager = $doctrine->getEntityManagerForClass(ConfigurationValue::class); |
|
| 50 | - $this->registry = $registry; |
|
| 51 | - $this->factory = $factory; |
|
| 52 | - } |
|
| 44 | + public function __construct( |
|
| 45 | + RegistryInterface $doctrine, |
|
| 46 | + DefinitionRegistry $registry, |
|
| 47 | + ConfigurationResolverFactory $factory |
|
| 48 | + ) { |
|
| 49 | + $this->entityManager = $doctrine->getEntityManagerForClass(ConfigurationValue::class); |
|
| 50 | + $this->registry = $registry; |
|
| 51 | + $this->factory = $factory; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @Route(path="/assign", name="configurationValue.assign") |
|
| 56 | - * @throws \Doctrine\ORM\OptimisticLockException |
|
| 57 | - * @throws \Doctrine\ORM\ORMException |
|
| 58 | - */ |
|
| 59 | - public function assignAction(Request $request) { |
|
| 60 | - $configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
|
| 61 | - $existingValueKeys = array_map(function (ConfigurationValue $value) { |
|
| 62 | - return $value->getKey(); |
|
| 63 | - }, $configurationValues); |
|
| 64 | - $missingValueKeys = array_diff($this->registry->keys(), $existingValueKeys); |
|
| 65 | - $configurationValues = array_merge( |
|
| 66 | - $configurationValues, |
|
| 67 | - array_map(function (string $key) { |
|
| 68 | - $value = new ConfigurationValue(); |
|
| 69 | - $value->setKey($key); |
|
| 70 | - return $value; |
|
| 71 | - }, $missingValueKeys) |
|
| 72 | - ); |
|
| 54 | + /** |
|
| 55 | + * @Route(path="/assign", name="configurationValue.assign") |
|
| 56 | + * @throws \Doctrine\ORM\OptimisticLockException |
|
| 57 | + * @throws \Doctrine\ORM\ORMException |
|
| 58 | + */ |
|
| 59 | + public function assignAction(Request $request) { |
|
| 60 | + $configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
|
| 61 | + $existingValueKeys = array_map(function (ConfigurationValue $value) { |
|
| 62 | + return $value->getKey(); |
|
| 63 | + }, $configurationValues); |
|
| 64 | + $missingValueKeys = array_diff($this->registry->keys(), $existingValueKeys); |
|
| 65 | + $configurationValues = array_merge( |
|
| 66 | + $configurationValues, |
|
| 67 | + array_map(function (string $key) { |
|
| 68 | + $value = new ConfigurationValue(); |
|
| 69 | + $value->setKey($key); |
|
| 70 | + return $value; |
|
| 71 | + }, $missingValueKeys) |
|
| 72 | + ); |
|
| 73 | 73 | |
| 74 | - $form = $this->createForm(ConfigurationValuesType::class, ['configurationValues' => $configurationValues]); |
|
| 75 | - $form->handleRequest($request); |
|
| 76 | - if ($form->isSubmitted() and $form->isValid()) { |
|
| 77 | - /* @var $configurationValue ConfigurationValue */ |
|
| 78 | - foreach ($form->get('configurationValues')->getData() as $configurationValue) { |
|
| 79 | - $isPersisted = \Doctrine\ORM\UnitOfWork::STATE_MANAGED === |
|
| 80 | - $this->entityManager->getUnitOfWork()->getEntityState($configurationValue); |
|
| 74 | + $form = $this->createForm(ConfigurationValuesType::class, ['configurationValues' => $configurationValues]); |
|
| 75 | + $form->handleRequest($request); |
|
| 76 | + if ($form->isSubmitted() and $form->isValid()) { |
|
| 77 | + /* @var $configurationValue ConfigurationValue */ |
|
| 78 | + foreach ($form->get('configurationValues')->getData() as $configurationValue) { |
|
| 79 | + $isPersisted = \Doctrine\ORM\UnitOfWork::STATE_MANAGED === |
|
| 80 | + $this->entityManager->getUnitOfWork()->getEntityState($configurationValue); |
|
| 81 | 81 | |
| 82 | - if ($configurationValue->getValue() === null and $isPersisted) { |
|
| 83 | - $this->entityManager->remove($configurationValue); |
|
| 84 | - } else if ($configurationValue->getValue() !== null and !$isPersisted) { |
|
| 85 | - $this->entityManager->persist($configurationValue); |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - $this->entityManager->flush(); |
|
| 82 | + if ($configurationValue->getValue() === null and $isPersisted) { |
|
| 83 | + $this->entityManager->remove($configurationValue); |
|
| 84 | + } else if ($configurationValue->getValue() !== null and !$isPersisted) { |
|
| 85 | + $this->entityManager->persist($configurationValue); |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + $this->entityManager->flush(); |
|
| 89 | 89 | |
| 90 | - return $this->redirectToRoute('configurationValue.view'); |
|
| 91 | - } |
|
| 90 | + return $this->redirectToRoute('configurationValue.view'); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - return $this->render('@OneGuardDynamicConfiguration/ConfigurationValue/assign.html.twig', ['form' => $form->createView()]); |
|
| 94 | - } |
|
| 93 | + return $this->render('@OneGuardDynamicConfiguration/ConfigurationValue/assign.html.twig', ['form' => $form->createView()]); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * @Route(path="/view", name="configurationValue.view") |
|
| 98 | - */ |
|
| 99 | - public function viewAction() { |
|
| 100 | - /* @var $configurationValues ConfigurationValue[] */ |
|
| 101 | - $configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
|
| 102 | - $labels = []; |
|
| 103 | - $propertyAccessor = new PropertyAccessor(); |
|
| 104 | - foreach ($configurationValues as $configurationValue) { |
|
| 105 | - $definition = $this->registry->get($configurationValue->getKey()); |
|
| 106 | - if ($definition instanceof EntityDefinition) { |
|
| 107 | - $object = $this->factory->create($definition->getKey())->resolve(); |
|
| 108 | - $labels[$definition->getKey()] = $propertyAccessor->getValue($object, $definition->getChoiceLabel()); |
|
| 109 | - } |
|
| 110 | - } |
|
| 96 | + /** |
|
| 97 | + * @Route(path="/view", name="configurationValue.view") |
|
| 98 | + */ |
|
| 99 | + public function viewAction() { |
|
| 100 | + /* @var $configurationValues ConfigurationValue[] */ |
|
| 101 | + $configurationValues = $this->entityManager->getRepository(ConfigurationValue::class)->findAll(); |
|
| 102 | + $labels = []; |
|
| 103 | + $propertyAccessor = new PropertyAccessor(); |
|
| 104 | + foreach ($configurationValues as $configurationValue) { |
|
| 105 | + $definition = $this->registry->get($configurationValue->getKey()); |
|
| 106 | + if ($definition instanceof EntityDefinition) { |
|
| 107 | + $object = $this->factory->create($definition->getKey())->resolve(); |
|
| 108 | + $labels[$definition->getKey()] = $propertyAccessor->getValue($object, $definition->getChoiceLabel()); |
|
| 109 | + } |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - return $this->render('@OneGuardDynamicConfiguration/ConfigurationValue/view.html.twig', [ |
|
| 113 | - 'configurationValues' => $configurationValues, |
|
| 114 | - 'translationDomain' => $this->getParameter('one_guard.dynamic_configuration.translation_domain'), |
|
| 115 | - 'translationPrefix' => $this->getParameter('one_guard.dynamic_configuration.translation_prefix'), |
|
| 116 | - 'labels' => $labels |
|
| 117 | - ]); |
|
| 118 | - } |
|
| 112 | + return $this->render('@OneGuardDynamicConfiguration/ConfigurationValue/view.html.twig', [ |
|
| 113 | + 'configurationValues' => $configurationValues, |
|
| 114 | + 'translationDomain' => $this->getParameter('one_guard.dynamic_configuration.translation_domain'), |
|
| 115 | + 'translationPrefix' => $this->getParameter('one_guard.dynamic_configuration.translation_prefix'), |
|
| 116 | + 'labels' => $labels |
|
| 117 | + ]); |
|
| 118 | + } |
|
| 119 | 119 | } |