@@ -7,90 +7,90 @@ |
||
7 | 7 | use Symfony\Bridge\Doctrine\RegistryInterface; |
8 | 8 | |
9 | 9 | class ProtectedReferencesChecker { |
10 | - /** |
|
11 | - * @var RegistryInterface |
|
12 | - */ |
|
13 | - private $doctrine; |
|
14 | - /** |
|
15 | - * @var DefinitionRegistry |
|
16 | - */ |
|
17 | - private $registry; |
|
10 | + /** |
|
11 | + * @var RegistryInterface |
|
12 | + */ |
|
13 | + private $doctrine; |
|
14 | + /** |
|
15 | + * @var DefinitionRegistry |
|
16 | + */ |
|
17 | + private $registry; |
|
18 | 18 | |
19 | - public function __construct(RegistryInterface $doctrine, DefinitionRegistry $registry) { |
|
20 | - $this->doctrine = $doctrine; |
|
21 | - $this->registry = $registry; |
|
22 | - } |
|
19 | + public function __construct(RegistryInterface $doctrine, DefinitionRegistry $registry) { |
|
20 | + $this->doctrine = $doctrine; |
|
21 | + $this->registry = $registry; |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * @param string $class |
|
26 | - * @param array|null $ids |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public function findProtectedIdsByClass(string $class, array $ids = null) : array { |
|
30 | - $keys = []; |
|
31 | - foreach ($this->registry->keys() as $key) { |
|
32 | - $definition = $this->registry->get($key); |
|
33 | - if ($definition instanceof EntityDefinition and is_a($class, $definition->getClass(), true)) { |
|
34 | - $keys[] = $definition->getKey(); |
|
35 | - } |
|
36 | - } |
|
24 | + /** |
|
25 | + * @param string $class |
|
26 | + * @param array|null $ids |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public function findProtectedIdsByClass(string $class, array $ids = null) : array { |
|
30 | + $keys = []; |
|
31 | + foreach ($this->registry->keys() as $key) { |
|
32 | + $definition = $this->registry->get($key); |
|
33 | + if ($definition instanceof EntityDefinition and is_a($class, $definition->getClass(), true)) { |
|
34 | + $keys[] = $definition->getKey(); |
|
35 | + } |
|
36 | + } |
|
37 | 37 | |
38 | - if (empty($keys)) { |
|
39 | - return []; |
|
40 | - } |
|
38 | + if (empty($keys)) { |
|
39 | + return []; |
|
40 | + } |
|
41 | 41 | |
42 | - $qb = $this->doctrine->getEntityManagerForClass(ConfigurationValue::class)->createQueryBuilder(); |
|
43 | - $qb |
|
44 | - ->select('DISTINCT (v.value)') |
|
45 | - ->from(ConfigurationValue::class, 'v') |
|
46 | - ->where($qb->expr()->in('v.key', ':keys')) |
|
47 | - ->setParameter('keys', $keys); |
|
42 | + $qb = $this->doctrine->getEntityManagerForClass(ConfigurationValue::class)->createQueryBuilder(); |
|
43 | + $qb |
|
44 | + ->select('DISTINCT (v.value)') |
|
45 | + ->from(ConfigurationValue::class, 'v') |
|
46 | + ->where($qb->expr()->in('v.key', ':keys')) |
|
47 | + ->setParameter('keys', $keys); |
|
48 | 48 | |
49 | - if ($ids !== null) { |
|
50 | - $qb |
|
51 | - ->andWhere($qb->expr()->in('v.value', ':ids')) |
|
52 | - ->setParameter('ids', $ids); |
|
53 | - } |
|
49 | + if ($ids !== null) { |
|
50 | + $qb |
|
51 | + ->andWhere($qb->expr()->in('v.value', ':ids')) |
|
52 | + ->setParameter('ids', $ids); |
|
53 | + } |
|
54 | 54 | |
55 | - $protectedIds = $qb->getQuery()->getScalarResult(); |
|
56 | - return empty($protectedIds) ? [] : array_merge(...$protectedIds); |
|
57 | - } |
|
55 | + $protectedIds = $qb->getQuery()->getScalarResult(); |
|
56 | + return empty($protectedIds) ? [] : array_merge(...$protectedIds); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @param $object |
|
61 | - * @throws ProtectedReferenceException |
|
62 | - * @throws \Doctrine\ORM\NoResultException |
|
63 | - * @throws \Doctrine\ORM\NonUniqueResultException |
|
64 | - */ |
|
65 | - public function checkReferenceForProtection($object) { |
|
66 | - $id = $this->doctrine->getEntityManagerForClass(get_class($object))->getUnitOfWork()->getSingleIdentifierValue($object); |
|
59 | + /** |
|
60 | + * @param $object |
|
61 | + * @throws ProtectedReferenceException |
|
62 | + * @throws \Doctrine\ORM\NoResultException |
|
63 | + * @throws \Doctrine\ORM\NonUniqueResultException |
|
64 | + */ |
|
65 | + public function checkReferenceForProtection($object) { |
|
66 | + $id = $this->doctrine->getEntityManagerForClass(get_class($object))->getUnitOfWork()->getSingleIdentifierValue($object); |
|
67 | 67 | |
68 | - $keys = []; |
|
69 | - foreach ($this->registry->keys() as $key) { |
|
70 | - $definition = $this->registry->get($key); |
|
71 | - if ($definition instanceof EntityDefinition and is_a($object, $definition->getClass())) { |
|
72 | - $keys[] = $definition->getKey(); |
|
73 | - } |
|
74 | - } |
|
68 | + $keys = []; |
|
69 | + foreach ($this->registry->keys() as $key) { |
|
70 | + $definition = $this->registry->get($key); |
|
71 | + if ($definition instanceof EntityDefinition and is_a($object, $definition->getClass())) { |
|
72 | + $keys[] = $definition->getKey(); |
|
73 | + } |
|
74 | + } |
|
75 | 75 | |
76 | - if (empty($keys)) { |
|
77 | - return; |
|
78 | - } |
|
76 | + if (empty($keys)) { |
|
77 | + return; |
|
78 | + } |
|
79 | 79 | |
80 | - $repo = $this->doctrine->getEntityManagerForClass(ConfigurationValue::class); |
|
81 | - $qb = $repo->createQueryBuilder(); |
|
82 | - $countProtected = $qb |
|
83 | - ->select('count(v)') |
|
84 | - ->from(ConfigurationValue::class, 'v') |
|
85 | - ->where($qb->expr()->in('v.key', ':keys')) |
|
86 | - ->andWhere($qb->expr()->eq('v.value', ':id')) |
|
87 | - ->getQuery() |
|
88 | - ->setParameter('keys', $keys) |
|
89 | - ->setParameter('id', $id) |
|
90 | - ->getSingleScalarResult(); |
|
80 | + $repo = $this->doctrine->getEntityManagerForClass(ConfigurationValue::class); |
|
81 | + $qb = $repo->createQueryBuilder(); |
|
82 | + $countProtected = $qb |
|
83 | + ->select('count(v)') |
|
84 | + ->from(ConfigurationValue::class, 'v') |
|
85 | + ->where($qb->expr()->in('v.key', ':keys')) |
|
86 | + ->andWhere($qb->expr()->eq('v.value', ':id')) |
|
87 | + ->getQuery() |
|
88 | + ->setParameter('keys', $keys) |
|
89 | + ->setParameter('id', $id) |
|
90 | + ->getSingleScalarResult(); |
|
91 | 91 | |
92 | - if ($countProtected > 0) { |
|
93 | - throw new ProtectedReferenceException("Can't delete protected reference."); |
|
94 | - } |
|
95 | - } |
|
92 | + if ($countProtected > 0) { |
|
93 | + throw new ProtectedReferenceException("Can't delete protected reference."); |
|
94 | + } |
|
95 | + } |
|
96 | 96 | } |
@@ -3,20 +3,20 @@ |
||
3 | 3 | namespace OneGuard\Bundle\DynamicConfigurationBundle; |
4 | 4 | |
5 | 5 | abstract class Definition { |
6 | - private $key; |
|
6 | + private $key; |
|
7 | 7 | |
8 | - /** |
|
9 | - * Definition constructor. |
|
10 | - * @param $key |
|
11 | - */ |
|
12 | - public function __construct($key) { |
|
13 | - $this->key = $key; |
|
14 | - } |
|
8 | + /** |
|
9 | + * Definition constructor. |
|
10 | + * @param $key |
|
11 | + */ |
|
12 | + public function __construct($key) { |
|
13 | + $this->key = $key; |
|
14 | + } |
|
15 | 15 | |
16 | - /** |
|
17 | - * @return mixed |
|
18 | - */ |
|
19 | - public function getKey() { |
|
20 | - return $this->key; |
|
21 | - } |
|
16 | + /** |
|
17 | + * @return mixed |
|
18 | + */ |
|
19 | + public function getKey() { |
|
20 | + return $this->key; |
|
21 | + } |
|
22 | 22 | } |
@@ -18,39 +18,39 @@ |
||
18 | 18 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
19 | 19 | |
20 | 20 | class OneGuardDynamicConfigurationExtension extends Extension { |
21 | - /** |
|
22 | - * @param array $configs |
|
23 | - * @param ContainerBuilder $container |
|
24 | - * @throws \Exception |
|
25 | - */ |
|
26 | - public function load(array $configs, ContainerBuilder $container) { |
|
27 | - $loader = new YamlFileLoader( |
|
28 | - $container, |
|
29 | - new FileLocator(__DIR__.'/../Resources/config') |
|
30 | - ); |
|
31 | - $loader->load('services.yml'); |
|
32 | - |
|
33 | - $configuration = new Configuration(); |
|
34 | - $config = $this->processConfiguration($configuration, $configs); |
|
35 | - |
|
36 | - $container->setParameter('one_guard.dynamic_configuration.translation_domain', $config['translation_domain']); |
|
37 | - $container->setParameter('one_guard.dynamic_configuration.translation_prefix', $config['translation_prefix']); |
|
38 | - |
|
39 | - $register = $container->getDefinition(DefinitionRegistry::class); |
|
40 | - |
|
41 | - foreach ($config['definitions'] as $key => $definitionConfig) { |
|
42 | - switch ($definitionConfig['type']) { |
|
43 | - case 'entity': |
|
44 | - $register->addMethodCall('registerEntity', [ |
|
45 | - $key, |
|
46 | - $definitionConfig['options']['class'], |
|
47 | - $definitionConfig['options']['choice_label'] |
|
48 | - ]); |
|
49 | - break; |
|
50 | - case 'string': |
|
51 | - $register->addMethodCall('registerString', [$key]); |
|
52 | - break; |
|
53 | - } |
|
54 | - } |
|
55 | - } |
|
21 | + /** |
|
22 | + * @param array $configs |
|
23 | + * @param ContainerBuilder $container |
|
24 | + * @throws \Exception |
|
25 | + */ |
|
26 | + public function load(array $configs, ContainerBuilder $container) { |
|
27 | + $loader = new YamlFileLoader( |
|
28 | + $container, |
|
29 | + new FileLocator(__DIR__.'/../Resources/config') |
|
30 | + ); |
|
31 | + $loader->load('services.yml'); |
|
32 | + |
|
33 | + $configuration = new Configuration(); |
|
34 | + $config = $this->processConfiguration($configuration, $configs); |
|
35 | + |
|
36 | + $container->setParameter('one_guard.dynamic_configuration.translation_domain', $config['translation_domain']); |
|
37 | + $container->setParameter('one_guard.dynamic_configuration.translation_prefix', $config['translation_prefix']); |
|
38 | + |
|
39 | + $register = $container->getDefinition(DefinitionRegistry::class); |
|
40 | + |
|
41 | + foreach ($config['definitions'] as $key => $definitionConfig) { |
|
42 | + switch ($definitionConfig['type']) { |
|
43 | + case 'entity': |
|
44 | + $register->addMethodCall('registerEntity', [ |
|
45 | + $key, |
|
46 | + $definitionConfig['options']['class'], |
|
47 | + $definitionConfig['options']['choice_label'] |
|
48 | + ]); |
|
49 | + break; |
|
50 | + case 'string': |
|
51 | + $register->addMethodCall('registerString', [$key]); |
|
52 | + break; |
|
53 | + } |
|
54 | + } |
|
55 | + } |
|
56 | 56 | } |
@@ -18,50 +18,50 @@ |
||
18 | 18 | * @ORM\Entity() |
19 | 19 | */ |
20 | 20 | class ConfigurationValue { |
21 | - /** |
|
22 | - * @var string |
|
23 | - * |
|
24 | - * @ORM\Id() |
|
25 | - * @ORM\Column(name="`key`", nullable=false, type="string", length=64) |
|
26 | - * |
|
27 | - * @Assert\NotNull() |
|
28 | - * @Assert\NotBlank() |
|
29 | - * @Assert\Length(max="64") |
|
30 | - */ |
|
31 | - private $key; |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + * |
|
24 | + * @ORM\Id() |
|
25 | + * @ORM\Column(name="`key`", nullable=false, type="string", length=64) |
|
26 | + * |
|
27 | + * @Assert\NotNull() |
|
28 | + * @Assert\NotBlank() |
|
29 | + * @Assert\Length(max="64") |
|
30 | + */ |
|
31 | + private $key; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @var string |
|
35 | - * |
|
36 | - * @ORM\Column(name="`value`", nullable=false, type="text") |
|
37 | - */ |
|
38 | - private $value; |
|
33 | + /** |
|
34 | + * @var string |
|
35 | + * |
|
36 | + * @ORM\Column(name="`value`", nullable=false, type="text") |
|
37 | + */ |
|
38 | + private $value; |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return string|null |
|
42 | - */ |
|
43 | - public function getKey() { |
|
44 | - return $this->key; |
|
45 | - } |
|
40 | + /** |
|
41 | + * @return string|null |
|
42 | + */ |
|
43 | + public function getKey() { |
|
44 | + return $this->key; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * @param string|null $key |
|
49 | - */ |
|
50 | - public function setKey(string $key = null) { |
|
51 | - $this->key = $key; |
|
52 | - } |
|
47 | + /** |
|
48 | + * @param string|null $key |
|
49 | + */ |
|
50 | + public function setKey(string $key = null) { |
|
51 | + $this->key = $key; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * @return string|null |
|
56 | - */ |
|
57 | - public function getValue() { |
|
58 | - return $this->value; |
|
59 | - } |
|
54 | + /** |
|
55 | + * @return string|null |
|
56 | + */ |
|
57 | + public function getValue() { |
|
58 | + return $this->value; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * @param string|null $value |
|
63 | - */ |
|
64 | - public function setValue(string $value = null) { |
|
65 | - $this->value = $value; |
|
66 | - } |
|
61 | + /** |
|
62 | + * @param string|null $value |
|
63 | + */ |
|
64 | + public function setValue(string $value = null) { |
|
65 | + $this->value = $value; |
|
66 | + } |
|
67 | 67 | } |
@@ -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.'); |
|
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.'); |
|
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.'); |
|
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.'); |
|
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 | } |