Completed
Push — 8.x-3.x ( 3995b9...ed8472 )
by Sebastian
01:54
created

ImageDerivative::resolveValues()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 3
nop 4
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\Fields\Image;
4
5
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6
use Drupal\Core\Image\ImageFactory;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\file\Entity\File;
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;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use Drupal\image\Entity\ImageStyle;
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'])) {
0 ignored issues
show
Bug introduced by
The class Drupal\image\Plugin\Field\FieldType\ImageItem does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
      assert($style instanceof ImageStyle);
0 ignored issues
show
Bug introduced by
The class Drupal\image\Entity\ImageStyle does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
74
      $file = $value->entity;
75
      assert($file instanceof File);
0 ignored issues
show
Bug introduced by
The class Drupal\file\Entity\File does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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->url();
89
      }
90
91
      yield new CacheableValue([
92
        'url' => $url,
93
        'width' => $dimensions['width'],
94
        'height' => $dimensions['height'],
95
      ], [$style]);
96
    }
97
  }
98
99
}
100