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

FormStateValueResolver::supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\mongodb_watchdog\Controller\ArgumentResolver;
4
5
use Drupal\Core\Form\FormStateInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
8
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
9
10
/**
11
 * Yields a form_state argument for FormStateInterface $formState arguments.
12
 *
13
 * This resolver supports form methods with a FormStateInterface argument
14
 * regardless of its name.
15
 */
16
class FormStateValueResolver implements ArgumentValueResolverInterface {
17
18
  const NAME_LEGACY = 'form_state';
19
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function supports(Request $request, ArgumentMetadata $argument) {
24
    $argumentInterfaceMatches = $argument->getType() === FormStateInterface::class;
25
    $requestAttributeExists = $request->attributes->has(static::NAME_LEGACY);
26
    return $argumentInterfaceMatches || $requestAttributeExists;
27
  }
28
29
  /**
30
   * {@inheritdoc}
31
   */
32
  public function resolve(Request $request, ArgumentMetadata $argument) {
33
    $formState = $request->attributes->has(static::NAME_LEGACY)
34
      ? $request->attributes->get(static::NAME_LEGACY)
35
      : NULL;
36
    yield $formState;
37
  }
38
39
}
40