Passed
Push — 8.x-2.x ( b65c24...82a225 )
by Frédéric G.
03:06
created

MongodbWatchdogServiceProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A alter() 0 26 4
1
<?php
2
3
namespace Drupal\mongodb_watchdog;
4
5
use Drupal\Core\DependencyInjection\ContainerBuilder;
6
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
7
use Symfony\Component\DependencyInjection\Reference;
8
9
/**
10
 * MongodbWatchdogServiceProvider add $formState support to forms.
11
 */
12
class MongodbWatchdogServiceProvider implements ServiceModifierInterface {
13
14
  const KERNEL_RESOLVER = 'http_kernel.controller.argument_resolver';
15
16
  // Individual resolvers.
17
  const RESOLVER_DEFAULT = 'argument_resolver.default';
18
  const RESOLVER_FORM_STATE = 'argument_resolver.mongodb_watchdog.form_state';
19
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function alter(ContainerBuilder $container) {
24
    $kernelResolverDefinition = $container
25
      ->getDefinition(static::KERNEL_RESOLVER);
26
    $resolvers = $kernelResolverDefinition->getArgument(1);
27
    $defaultIndex = $count = count($resolvers);
28
    /** @var \Symfony\Component\DependencyInjection\Reference $reference */
29
    foreach ($resolvers as $index => $reference) {
30
      if ("$reference" === static::RESOLVER_DEFAULT) {
31
        $defaultIndex = $index;
32
        break;
33
      }
34
    }
35
36
    $formStateResolverReference = new Reference(static::RESOLVER_FORM_STATE);
37
38
    // If the default resolved is present, insert just before it.
39
    if ($defaultIndex != $count) {
40
      array_splice($resolvers, $defaultIndex, 0, [$formStateResolverReference]);
41
    }
42
    // Else add the formState resolver at the end of the list.
43
    else {
44
      array_push($resolvers, [$formStateResolverReference]);
45
    }
46
47
    $kernelResolverDefinition->setArgument(1, $resolvers);
48
  }
49
50
}
51