SchemaController   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 3 1
A __construct() 0 2 1
A get() 0 8 1
1
<?php
2
3
namespace Drupal\paragraphs_editor\Controller;
4
5
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6
use Drupal\Core\Field\FieldConfigInterface;
7
use Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
11
/**
12
 * Provides an endpoint for serving paragraphs editor schema definitions.
13
 */
14
class SchemaController implements ContainerInjectionInterface {
15
16
  /**
17
   * Context factory containing the bundle lookup information.
18
   *
19
   * @var \Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface
20
   */
21
  protected $contextFactory;
22
23
  /**
24
   * Creates a SchemaController object.
25
   *
26
   * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface $context_factory
27
   *   The context factory to lookup schemas with.
28
   *
29
   * @constructor
30
   */
31
  public function __construct(CommandContextFactoryInterface $context_factory) {
32
    $this->contextFactory = $context_factory;
33
  }
34
35
  /**
36
   * {@inheritdoc}
37
   */
38
  public static function create(ContainerInterface $container) {
39
    return new static(
40
      $container->get('paragraphs_editor.command.context_factory')
41
    );
42
  }
43
44
  /**
45
   * Endpoint for retrieving paragraphs editor schema information.
46
   *
47
   * @param \Drupal\Core\Field\FieldConfigInterface $field_config
48
   *   The field configuration to lookup the schema for.
49
   *
50
   * @return \Symfony\Component\HttpFoundation\JsonResponse
51
   *   A JSON response containing a widget binder schema model.
52
   */
53
  public function get(FieldConfigInterface $field_config) {
54
    $bundle_filter = $this->contextFactory->createBundleFilter($field_config);
55
    return new JsonResponse([
56
      [
57
        'type' => 'schema',
58
        'id' => $field_config->id(),
59
        'attributes' => [
60
          'allowed' => $bundle_filter->getAllowedBundles(),
61
        ],
62
      ],
63
    ]);
64
  }
65
66
}
67