Passed
Pull Request — 8.x-1.x (#40)
by
unknown
02:38
created

Views::isPaged()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Drupal\graphql_views\Plugin\GraphQL\DataProducer;
4
5
use Drupal\Core\Entity\EntityTypeManagerInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Entity\EntityTypeManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Plugin\ContainerFactoryPluginInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
0 ignored issues
show
Bug introduced by
The type Drupal\graphql\Plugin\Gr...\DataProducerPluginBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Drupal\views\Plugin\views\display\DisplayPluginInterface;
0 ignored issues
show
Bug introduced by
The type Drupal\views\Plugin\view...\DisplayPluginInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\DependencyInjection\ContainerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...tion\ContainerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
/**
12
 * GraphQL data producer for views.
13
 *
14
 * @DataProducer(
15
 *   id = "views",
16
 *   name = @Translation("Views"),
17
 *   description = @Translation("Views."),
18
 *   produces = @ContextDefinition("entity",
19
 *     label = @Translation("Entity")
20
 *   ),
21
 *   consumes = {
22
 *     "view_id" = @ContextDefinition("string",
23
 *       label = @Translation("View ID")
24
 *     ),
25
 *     "display_id" = @ContextDefinition("string",
26
 *       label = @Translation("Display ID")
27
 *     ),
28
 *     "offset" = @ContextDefinition("integer",
29
 *       label = @Translation("Offset"),
30
 *       required = FALSE
31
 *     ),
32
 *     "page_size" = @ContextDefinition("integer",
33
 *       label = @Translation("Page size"),
34
 *       required = FALSE
35
 *     ),
36
 *     "page" = @ContextDefinition("integer",
37
 *       label = @Translation("Current page"),
38
 *       required = FALSE
39
 *     )
40
 *   }
41
 * )
42
 */
43
class Views extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
44
45
  /**
46
   * The entity type manager.
47
   *
48
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
49
   */
50
  protected $entityTypeManager;
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
56
    return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager'));
57
  }
58
59
  /**
60
   * {@inheritdoc}
61
   */
62
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager) {
63
    parent::__construct($configuration, $plugin_id, $plugin_definition);
64
    $this->entityTypeManager = $entityTypeManager;
65
  }
66
67
  /**
68
   * Resolve the data.
69
   *
70
   * @param string $view_id
71
   *   The view ID.
72
   * @param string $display_id
73
   *   The display ID.
74
   * @param int|null $offset
75
   *   Offset of the query.
76
   * @param int|null $page_size
77
   *   Number of items on page.
78
   * @param int|null $page
79
   *   Number of page.
80
   *
81
   * @return array|null
82
   *   List of entities.
83
   *
84
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
85
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
86
   */
87
  public function resolve(string $view_id, string $display_id, int $offset = NULL, int $page_size = NULL, int $page = NULL) {
88
89
    /** @var \Drupal\views\Entity\View $view */
90
    $view = \Drupal::entityTypeManager()->getStorage('view')->load($view_id);
91
92
    $executable = $view->getExecutable();
93
    $executable->setDisplay($display_id);
94
95
    // Set paging parameters.
96
    if ($this->isPaged($executable->getDisplay()) && $page_size && $page) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $page of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $page_size of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
97
      $executable->setItemsPerPage($page_size);
98
      $executable->setCurrentPage($page);
99
    }
100
101
    if ($offset) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $offset of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
102
      $executable->setOffset($offset);
103
    }
104
105
    $executable->preExecute();
106
    $executable->execute();
107
    return $executable->render($display_id);
108
  }
109
110
  /**
111
   * Check if a pager is configured.
112
   *
113
   * @param \Drupal\views\Plugin\views\display\DisplayPluginInterface $display
114
   *   The display configuration.
115
   *
116
   * @return bool
117
   *   Flag indicating if the view is configured with a pager.
118
   */
119
  protected function isPaged(DisplayPluginInterface $display) {
120
    $pagerOptions = $display->getOption('pager');
121
    return isset($pagerOptions['type']) && in_array($pagerOptions['type'], [
122
      'full',
123
      'mini',
124
    ]);
125
  }
126
127
}
128