ViewDerivative::setOverridenViewDefaults()   B
last analyzed

Complexity

Conditions 10
Paths 24

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 7.6666
c 0
b 0
f 0
cc 10
nc 24
nop 2

How to fix   Complexity   

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\graphql_views\Plugin\GraphQL\Fields\Entity\Fields\View;
4
5
use Drupal\graphql\GraphQL\Execution\ResolveContext;
6
use Drupal\graphql_views\Plugin\GraphQL\Fields\View;
7
use Drupal\graphql_views\ViewDeriverHelperTrait;
8
use Drupal\views\Entity\View as EntityView;
9
use Drupal\views\Plugin\views\display\DisplayPluginInterface;
10
use GraphQL\Type\Definition\ResolveInfo;
11
12
/**
13
 * Retrieve the views field derivative.
14
 *
15
 * @GraphQLField(
16
 *   id = "view_derivative",
17
 *   secure = true,
18
 *   name = "viewDerivative",
19
 *   type = "ViewResult",
20
 *   field_types = {"viewsreference"},
21
 *   provider = "viewsreference",
22
 *   arguments={
23
 *     "filter" = {
24
 *       "optional" = true,
25
 *       "type" = "ViewsFilterInput"
26
 *     },
27
 *     "offset" = {
28
 *       "optional" = true,
29
 *       "type" = "Int"
30
 *     },
31
 *     "page" = {
32
 *       "optional" = true,
33
 *       "type" = "Int"
34
 *     },
35
 *     "pageSize" = {
36
 *       "optional" = true,
37
 *       "type" = "Int"
38
 *     },
39
 *     "sortBy" = {
40
 *       "optional" = true,
41
 *       "type" = "ViewsSortByInput"
42
 *     },
43
 *     "sortDirection" = {
44
 *       "optional" = true,
45
 *       "type" = "ViewSortDirection",
46
 *       "default" = "asc"
47
 *     },
48
 *     "contextualFilter" = {
49
 *       "optional" = true,
50
 *       "type" = "ViewsContextualFilterInput"
51
 *     }
52
 *   },
53
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
54
 * )
55
 */
56
class ViewDerivative extends View {
57
  use ViewDeriverHelperTrait;
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
63
    $values = $value->getValue();
64
    $this->pluginDefinition['view'] = $values['target_id'];
65
    $this->pluginDefinition['display'] = $values['display_id'];
66
    $view = EntityView::load($values['target_id']);
67
    $display = $this->getViewDisplay($view, $values['display_id']);
68
    $this->pluginDefinition['paged'] = $this->isPaged($display);
69
    $this->pluginDefinition['arguments_info'] = $this->getArgumentsInfo($display->getOption('arguments') ?: []);
70
    $this->pluginDefinition = array_merge($this->pluginDefinition, $this->getCacheMetadataDefinition($view, $display));
71
    $this->setOverridenViewDefaults($value, $args);
72
    $this->setViewDefaultValues($display, $args);
73
    return parent::resolveValues($value, $args, $context, $info);
74
  }
75
76
  /**
77
   * Get configuration values from views reference field.
78
   *
79
   * @param mixed $value
80
   *   The current object value.
81
   *
82
   * @return array|mixed
83
   *   Return unserialized data.
84
   */
85
  protected function getViewReferenceConfiguration($value) {
86
    $values = $value->getValue();
87
    return isset($values['data']) ? unserialize($values['data']) : [];
88
  }
89
90
  /**
91
   * Set default display settings.
92
   *
93
   * @param mixed $value
94
   *   The current object value.
95
   * @param array $args
96
   *   Arguments where the default view settings needs to be added.
97
   */
98
  protected function setOverridenViewDefaults($value, array &$args) {
99
    $viewReferenceConfiguration = $this->getViewReferenceConfiguration($value);
100
    if (!empty($viewReferenceConfiguration['pager'])) {
101
      $this->pluginDefinition['paged'] = in_array($viewReferenceConfiguration['pager'], [
102
        'full',
103
        'mini',
104
      ]);
105
    }
106
107
    if (!isset($args['pageSize']) && !empty($viewReferenceConfiguration['limit'])) {
108
      $args['pageSize'] = $viewReferenceConfiguration['limit'];
109
    }
110
111
    if (!isset($args['offset']) && !empty($viewReferenceConfiguration['offset'])) {
112
      $args['offset'] = $viewReferenceConfiguration['offset'];
113
    }
114
115
    /* Expected format: {"contextualFilter": {"key": "value","keyN": "valueN"}} */
116
    if (!isset($args['contextualFilter']) && !empty($viewReferenceConfiguration['argument'])) {
117
      $argument = json_decode($viewReferenceConfiguration['argument'], TRUE);
118
      if (isset($argument['contextualFilter']) && !empty($argument['contextualFilter'])) {
119
        $args['contextualFilter'] = $argument['contextualFilter'];
120
      }
121
    }
122
  }
123
124
  /**
125
   * Set default display settings.
126
   *
127
   * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
128
   *   The display configuration.
129
   * @param array $args
130
   *   Arguments where the default view settings needs to be added.
131
   */
132
  protected function setViewDefaultValues(DisplayPluginInterface $display, array &$args) {
133
    if (!isset($args['pageSize']) && $this->pluginDefinition['paged']) {
134
      $args['pageSize'] = $this->getPagerLimit($display);
135
    }
136
    if (!isset($args['page']) && $this->pluginDefinition['paged']) {
137
      $args['page'] = $this->getPagerOffset($display);
138
    }
139
  }
140
141
}
142