Completed
Pull Request — 8.x-3.x (#399)
by Philipp
02:23
created

ViewResultCountDeriver::getDerivativeDefinitions()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 19
nc 2
nop 1
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\Deriver\Fields;
4
5
use Drupal\graphql\Utility\StringHelper;
6
use Drupal\graphql_core\Plugin\Deriver\ViewDeriverBase;
7
use Drupal\views\Views;
8
9
/**
10
 * Derive fields from configured views.
11
 */
12
class ViewResultCountDeriver extends ViewDeriverBase {
13
14
  /**
15
   * {@inheritdoc}
16
   */
17
  public function getDerivativeDefinitions($basePluginDefinition) {
18
    if ($this->entityTypeManager->hasDefinition('view')) {
19
      $viewStorage = $this->entityTypeManager->getStorage('view');
20
21
      foreach (Views::getApplicableViews('graphql_display') as list($viewId, $displayId)) {
22
        /** @var \Drupal\views\ViewEntityInterface $view */
23
        $view = $viewStorage->load($viewId);
24
        /** @var \Drupal\graphql\Plugin\views\display\GraphQL $display */
25
        $display = $this->getViewDisplay($view, $displayId);
26
        if (!$this->isPaged($display)) {
27
          continue;
28
        }
29
30
        if (!$this->getRowResolveType($view, $displayId)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getRowResolveType($view, $displayId) of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
31
          continue;
32
        }
33
34
        $id = implode('-', [$viewId, $displayId, 'result', 'count']);
35
        $this->derivatives[$id] = [
36
            'id' => $id,
37
            'type' => 'Int',
38
            'parents' => [$display->getGraphQLResultName()],
39
            'view' => $viewId,
40
            'display' => $displayId,
41
          ] + $this->getCacheMetadataDefinition($view) + $basePluginDefinition;
42
      }
43
    }
44
45
    return parent::getDerivativeDefinitions($basePluginDefinition);
46
  }
47
48
}
49