CommandContextConverter::convert()   C
last analyzed

Complexity

Conditions 9
Paths 48

Size

Total Lines 52
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 25
nc 48
nop 4
dl 0
loc 52
ccs 0
cts 22
cp 0
crap 90
rs 6.5703
c 0
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Drupal\paragraphs_editor\ParamConverter;
4
5
use Drupal\Core\ParamConverter\ParamConverterInterface;
6
use Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
use Symfony\Component\Routing\Route;
9
10
/**
11
 * Route parameter converter for paragraphs ckeditor command contexts.
12
 *
13
 * This is mostly a convenience wrapper so we can just pass a string that
14
 * uniquely identifies the editor instance, and re-assemble everything about
15
 * the state of the editor before the command controller gets ahold of it.
16
 */
17
class CommandContextConverter implements ParamConverterInterface {
18
19
  /**
20
   * The context factory for creating command contexts.
21
   *
22
   * @var \Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface
23
   */
24
  protected $contextFactory;
25
26
  /**
27
   * The current page request to pull widget field settings from.
28
   *
29
   * @var \Symfony\Component\HttpFoundation\Request
30
   */
31
  protected $request;
32
33
  /**
34
   * Creates a paragraphs ckeditor command context route parameter converter.
35
   *
36
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
37
   *   The symfony request stack service that is managing page requests.
38
   * @param \Drupal\paragraphs_editor\EditorCommand\CommandContextFactoryInterface $context_factory
39
   *   The context factory to use for creating command contexts.
40
   */
41
  public function __construct(RequestStack $request_stack, CommandContextFactoryInterface $context_factory) {
42
    $this->contextFactory = $context_factory;
43
    $this->request = $request_stack->getCurrentRequest();
44
  }
45
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public function convert($value, $definition, $name, array $defaults) {
50
    // Since a context string is just an ordered listing of information about
51
    // where the editor instance came from, we can separate out the ids here to
52
    // load the relevant plugins  and entities.
53
    list($field_config_id, $widget_build_id, $entity_id) = $this->contextFactory->parseContextString($value);
54
55
    // The settings array for the field widget has to be passed through the
56
    // request, either by POST or GET. Otherwise it's extremely difficult to get
57
    // back the settings from just the context.
58
    $settings = $this->request->get('settings');
59
    if (!is_array($settings)) {
60
      $settings = [];
61
    }
62
63
    $context = $this->contextFactory->create($field_config_id, $entity_id, $settings, $widget_build_id);
64
65
    $request_whitelist = [
66
      'editorContext',
67
      'editableContexts',
68
      'edits',
69
      'module',
70
    ];
71
72
    foreach ($request_whitelist as $name) {
73
      $value = $this->request->get($name);
74
      if (isset($value)) {
75
        $context->addAdditionalContext($name, $value);
76
      }
77
    }
78
79
    $editor_context = $context->getAdditionalContext('editorContext');
80
    if ($editor_context) {
81
      $context->getEditBuffer()->tagParentBuffer($editor_context);
82
    }
83
84
    $additional_context = $this->request->get('additional_context');
85
    if ($additional_context) {
86
      foreach (unserialize($additional_context) as $key => $value) {
87
        $context->addAdditionalContext($key, $value);
88
      }
89
    }
90
91
    // If the parameter definition gave any additional context about the command
92
    // that is being executed, we add that here so that delivery or bundle
93
    // selector plugins have access to it.
94
    if (is_array($definition)) {
95
      foreach ($definition as $key => $value) {
96
        $context->addAdditionalContext($key, $value);
97
      }
98
    }
99
100
    return $context;
101
  }
102
103
  /**
104
   * {@inheritdoc}
105
   */
106
  public function applies($definition, $name, Route $route) {
107
    return (!empty($definition['type']) && $definition['type'] == 'paragraphs_editor_command_context');
108
  }
109
110
}
111