ViewSortByDeriver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B getDerivativeDefinitions() 0 36 6
1
<?php
2
3
namespace Drupal\graphql_views\Plugin\Deriver\Enums;
4
5
use Drupal\graphql\Utility\StringHelper;
6
use Drupal\graphql_views\Plugin\Deriver\ViewDeriverBase;
7
use Drupal\views\Views;
8
9
class ViewSortByDeriver extends ViewDeriverBase {
10
11
  /**
12
   * {@inheritdoc}
13
   */
14
  public function getDerivativeDefinitions($basePluginDefinition) {
15
    if ($this->entityTypeManager->hasDefinition('view')) {
16
      $viewStorage = $this->entityTypeManager->getStorage('view');
17
18
      foreach (Views::getApplicableViews('graphql_display') as list($viewId, $displayId)) {
19
        /** @var \Drupal\views\ViewEntityInterface $view */
20
        $view = $viewStorage->load($viewId);
21
        if (!$type = $this->getRowResolveType($view, $displayId)) {
22
          continue;
23
        }
24
25
        /** @var \Drupal\graphql_views\Plugin\views\display\GraphQL $display */
26
        $display = $this->getViewDisplay($view, $displayId);
27
        $sorts = array_filter($display->getOption('sorts') ?: [], function ($sort) {
28
          return $sort['exposed'];
29
        });
30
        $sorts = array_reduce($sorts, function ($carry, $sort) {
31
          $carry[strtoupper($sort['id'])] = [
32
            'value' => $sort['id'],
33
            'description' => $sort['expose']['label'],
34
          ];
35
          return $carry;
36
        }, []);
37
38
        if (!empty($sorts)) {
39
          $id = implode('-', [$viewId, $displayId, 'view']);
40
          $this->derivatives["$viewId-$displayId"] = [
41
            'name' => StringHelper::camelCase($id, 'sort', 'by'),
42
            'values' => $sorts,
43
          ] + $basePluginDefinition;
44
        }
45
      }
46
    }
47
48
    return parent::getDerivativeDefinitions($basePluginDefinition);
49
  }
50
51
}
52