ImageDerivative   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 1
b 0
f 0
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 2 1
A __construct() 0 3 1
A resolveValues() 0 25 6
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\Fields\Image;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\DependencyIn...dencySerializationTrait 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\Image\ImageFactory;
0 ignored issues
show
Bug introduced by
The type Drupal\Core\Image\ImageFactory 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\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...
8
use Drupal\file\Entity\File;
0 ignored issues
show
Bug introduced by
The type Drupal\file\Entity\File 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 Drupal\graphql\GraphQL\Cache\CacheableValue;
10
use Drupal\graphql\GraphQL\Execution\ResolveContext;
11
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
12
use Drupal\image\Plugin\Field\FieldType\ImageItem;
0 ignored issues
show
Bug introduced by
The type Drupal\image\Plugin\Field\FieldType\ImageItem 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...
13
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...
14
use GraphQL\Type\Definition\ResolveInfo;
15
use Drupal\image\Entity\ImageStyle;
0 ignored issues
show
Bug introduced by
The type Drupal\image\Entity\ImageStyle 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...
16
17
/**
18
 * Retrieve the image field derivative (image style).
19
 *
20
 * @GraphQLField(
21
 *   id = "image_derivative",
22
 *   secure = true,
23
 *   name = "derivative",
24
 *   type = "ImageResource",
25
 *   arguments = {
26
 *     "style" = "ImageStyleId!"
27
 *   },
28
 *   field_types = {"image"},
29
 *   provider = "image",
30
 *   deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
31
 * )
32
 */
33
class ImageDerivative extends FieldPluginBase implements ContainerFactoryPluginInterface {
34
35
  use DependencySerializationTrait;
36
37
  /**
38
   * The image factory.
39
   *
40
   * @var \Drupal\Core\Image\ImageFactory
41
   */
42
  protected $imageFactory;
43
44
  /**
45
   * {@inheritdoc}
46
   */
47
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
48
    return new static($configuration, $pluginId, $pluginDefinition, $container->get('image.factory'));
49
  }
50
51
  /**
52
   * ImageDerivative constructor.
53
   *
54
   * @param array $configuration
55
   *   The plugin configuration array.
56
   * @param string $pluginId
57
   *   The plugin id.
58
   * @param mixed $pluginDefinition
59
   *   The plugin definition.
60
   * @param \Drupal\Core\Image\ImageFactory $imageFactory
61
   *   The image factory service.
62
   */
63
  public function __construct(array $configuration, $pluginId, $pluginDefinition, ImageFactory $imageFactory) {
64
    parent::__construct($configuration, $pluginId, $pluginDefinition);
65
    $this->imageFactory = $imageFactory;
66
  }
67
68
  /**
69
   * {@inheritdoc}
70
   */
71
  protected function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
72
    if ($value instanceof ImageItem && $value->entity && $value->entity->access('view') && $style = ImageStyle::load($args['style'])) {
73
      assert($style instanceof ImageStyle);
74
      $file = $value->entity;
75
      assert($file instanceof File);
76
77
      // Determine the dimensions of the styled image.
78
      $dimensions = [
79
        'width' => $value->width,
80
        'height' => $value->height,
81
      ];
82
83
      if ($style->supportsUri($file->getFileUri())) {
84
        $style->transformDimensions($dimensions, $file->getFileUri());
85
        $url = $style->buildUrl($file->getFileUri());
86
      }
87
      else {
88
        $url = $file->getFileUri();
89
      }
90
91
      yield new CacheableValue([
92
        'url' => $url,
93
        'width' => $dimensions['width'],
94
        'height' => $dimensions['height'],
95
      ], [$style]);
96
    }
97
  }
98
99
}
100