MediaEntity   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A create() 0 8 1
A uri() 0 18 3
1
<?php
2
3
namespace Drupal\crop_media_entity\Plugin\Crop\EntityProvider;
4
5
use Drupal\Core\Entity\EntityInterface;
6
use Drupal\Core\Entity\EntityTypeManagerInterface;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\crop\EntityProviderBase;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Media entity crop integration.
13
 *
14
 * @CropEntityProvider(
15
 *   entity_type = "media",
16
 *   label = @Translation("Media"),
17
 *   description = @Translation("Provides crop integration for media entity.")
18
 * )
19
 */
20
class MediaEntity extends EntityProviderBase implements ContainerFactoryPluginInterface {
21
22
  /**
23
   * Entity type manager service.
24
   *
25
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26
   */
27
  protected $entityTypeManager;
28
29
  /**
30
   * Constructs media entity integration plugin.
31
   *
32
   * @param array $configuration
33
   *   A configuration array containing information about the plugin instance.
34
   * @param string $plugin_id
35
   *   The plugin_id for the plugin instance.
36
   * @param mixed $plugin_definition
37
   *   The plugin implementation definition.
38
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
39
   *   Entity type manager service.
40
   */
41
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
42
    parent::__construct($configuration, $plugin_id, $plugin_definition);
43
    $this->entityTypeManager = $entity_type_manager;
44
  }
45
46
  /**
47
   * {@inheritdoc}
48
   */
49
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
50
    return new static(
51
      $configuration,
52
      $plugin_id,
53
      $plugin_definition,
54
      $container->get('entity_type.manager')
55
    );
56
  }
57
58
  /**
59
   * {@inheritdoc}
60
   */
61
  public function uri(EntityInterface $entity) {
62
    /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
63
    $bundle = $this->entityTypeManager->getStorage('media_bundle')->load($entity->bundle());
64
    $image_field = $bundle->getThirdPartySetting('crop', 'image_field');
65
66
    if ($entity->{$image_field}->first()->isEmpty()) {
67
      return FALSE;
68
    }
69
70
    /** @var \Drupal\file\FileInterface $image */
71
    $image = $this->entityTypeManager->getStorage('file')->load($entity->{$image_field}->target_id);
72
73
    if (strpos($image->getMimeType(), 'image') !== 0) {
74
      return FALSE;
75
    }
76
77
    return $image->getFileUri();
78
  }
79
80
}
81