| Conditions | 18 |
| Paths | 140 |
| Total Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 19 | public function process(ContainerBuilder $container) |
||
| 20 | { |
||
| 21 | $resolvers = $container->findTaggedServiceIds('doctrine.orm.entity_listener'); |
||
| 22 | |||
| 23 | $lazyServiceReferencesByResolver = []; |
||
| 24 | |||
| 25 | foreach ($resolvers as $id => $tagAttributes) { |
||
| 26 | foreach ($tagAttributes as $attributes) { |
||
| 27 | $name = isset($attributes['entity_manager']) ? $attributes['entity_manager'] : $container->getParameter('doctrine.default_entity_manager'); |
||
| 28 | $entityManager = sprintf('doctrine.orm.%s_entity_manager', $name); |
||
| 29 | |||
| 30 | if (! $container->hasDefinition($entityManager)) { |
||
| 31 | continue; |
||
| 32 | } |
||
| 33 | |||
| 34 | $resolverId = sprintf('doctrine.orm.%s_entity_listener_resolver', $name); |
||
| 35 | |||
| 36 | if (! $container->has($resolverId)) { |
||
| 37 | continue; |
||
| 38 | } |
||
| 39 | |||
| 40 | $resolver = $container->findDefinition($resolverId); |
||
| 41 | $resolver->setPublic(true); |
||
| 42 | |||
| 43 | if (isset($attributes['entity']) && isset($attributes['event'])) { |
||
| 44 | $this->attachToListener($container, $name, $id, $attributes); |
||
| 45 | } |
||
| 46 | |||
| 47 | $interface = 'Doctrine\\Bundle\\DoctrineBundle\\Mapping\\EntityListenerServiceResolver'; |
||
| 48 | $class = $resolver->getClass(); |
||
| 49 | |||
| 50 | if (substr($class, 0, 1) === '%') { |
||
| 51 | // resolve container parameter first |
||
| 52 | $class = $container->getParameterBag()->resolveValue($resolver->getClass()); |
||
| 53 | } |
||
| 54 | $resolverSupportsLazyListeners = is_a($class, $interface, true); |
||
| 55 | |||
| 56 | $lazyByAttribute = isset($attributes['lazy']) && $attributes['lazy']; |
||
| 57 | if ($lazyByAttribute && ! $resolverSupportsLazyListeners) { |
||
| 58 | throw new InvalidArgumentException( |
||
| 59 | sprintf('Lazy-loaded entity listeners can only be resolved by a resolver implementing %s.', $interface) |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (! isset($attributes['lazy']) && $resolverSupportsLazyListeners || $lazyByAttribute) { |
||
| 64 | $listener = $container->findDefinition($id); |
||
| 65 | |||
| 66 | if ($listener->isAbstract()) { |
||
| 67 | throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as this entity listener is lazy-loaded.', $id)); |
||
| 68 | } |
||
| 69 | |||
| 70 | $resolver->addMethodCall('registerService', [$listener->getClass(), $id]); |
||
| 71 | |||
| 72 | if (! isset($lazyServiceReferencesByResolver[$resolverId])) { |
||
| 73 | $lazyServiceReferencesByResolver[$resolverId] = []; |
||
| 74 | } |
||
| 75 | $lazyServiceReferencesByResolver[$resolverId][$id] = new Reference($id); |
||
| 76 | } else { |
||
| 77 | $resolver->addMethodCall('register', [new Reference($id)]); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | foreach ($lazyServiceReferencesByResolver as $resolverId => $listenerReferences) { |
||
| 83 | $container->findDefinition($resolverId)->replaceArgument(0, ServiceLocatorTagPass::register($container, $listenerReferences)); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 110 |