Completed
Pull Request — 8.x-1.x (#14)
by
unknown
02:04
created

CropEffect   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 171
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 2

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 10 1
A transformDimensions() 0 8 2
B applyEffect() 0 25 5
A getSummary() 0 9 1
A defaultConfiguration() 0 5 1
A buildConfigurationForm() 0 16 2
A submitConfigurationForm() 0 4 1
A getCrop() 0 3 1
A getCropFromUri() 0 10 3
1
<?php
2
3
namespace Drupal\crop\Plugin\ImageEffect;
4
5
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6
use Drupal\Core\Form\FormStateInterface;
7
use Drupal\Core\Image\ImageInterface;
8
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9
use Drupal\crop\CropStorageInterface;
10
use Drupal\crop\Entity\Crop;
11
use Drupal\image\ConfigurableImageEffectBase;
12
use Psr\Log\LoggerInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Crops an image resource.
17
 *
18
 * @ImageEffect(
19
 *   id = "crop_crop",
20
 *   label = @Translation("Manual crop"),
21
 *   description = @Translation("Applies manually provided crop to the image.")
22
 * )
23
 */
24
class CropEffect extends ConfigurableImageEffectBase implements ContainerFactoryPluginInterface {
25
26
  /**
27
   * Crop entity storage.
28
   *
29
   * @var \Drupal\crop\CropStorageInterface
30
   */
31
  protected $storage;
32
33
  /**
34
   * Crop type entity storage.
35
   *
36
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
37
   */
38
  protected $typeStorage;
39
40
  /**
41
   * Crop entity.
42
   *
43
   * @var \Drupal\crop\CropInterface
44
   */
45
  protected $crop;
46
47
  /**
48
   * {@inheritdoc}
49
   */
50
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, CropStorageInterface $crop_storage, ConfigEntityStorageInterface $crop_type_storage) {
51
    parent::__construct($configuration, $plugin_id, $plugin_definition, $logger);
52
    $this->storage = $crop_storage;
53
    $this->typeStorage = $crop_type_storage;
54
  }
55
56
  /**
57
   * {@inheritdoc}
58
   */
59
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
60
    return new static(
61
      $configuration,
62
      $plugin_id,
63
      $plugin_definition,
64
      $container->get('logger.factory')->get('image'),
65
      $container->get('entity.manager')->getStorage('crop'),
66
      $container->get('entity.manager')->getStorage('crop_type')
67
    );
68
  }
69
70
  /**
71
   * {@inheritdoc}
72
   */
73
  public function transformDimensions(array &$dimensions, $uri) {
74
    if ($crop = $this->getCropFromUri($uri)) {
75
      $size = $crop->size();
76
77
      $dimensions['width'] = $size['width'];
78
      $dimensions['height'] = $size['height'];
79
    }
80
  }
81
82
  /**
83
   * {@inheritdoc}
84
   */
85
  public function applyEffect(ImageInterface $image) {
86
    if (empty($this->configuration['crop_type']) || !$this->typeStorage->load($this->configuration['crop_type'])) {
87
      $this->logger->error('Manual image crop failed due to misconfigured crop type on %path.', ['%path' => $image->getSource()]);
88
      return FALSE;
89
    }
90
91
    if ($crop = $this->getCrop($image)) {
92
      $anchor = $crop->anchor();
93
      $size = $crop->size();
94
95
      if (!$image->crop($anchor['x'], $anchor['y'], $size['width'], $size['height'])) {
96
        $this->logger->error('Manual image crop failed using the %toolkit toolkit on %path (%mimetype, %width x %height)', [
97
            '%toolkit' => $image->getToolkitId(),
98
            '%path' => $image->getSource(),
99
            '%mimetype' => $image->getMimeType(),
100
            '%width' => $image->getWidth(),
101
            '%height' => $image->getHeight(),
102
          ]
103
        );
104
        return FALSE;
105
      }
106
    }
107
108
    return TRUE;
109
  }
110
111
  /**
112
   * {@inheritdoc}
113
   */
114
  public function getSummary() {
115
    $summary = [
116
      '#theme' => 'crop_crop_summary',
117
      '#data' => $this->configuration,
118
    ];
119
    $summary += parent::getSummary();
120
121
    return $summary;
122
  }
123
124
  /**
125
   * {@inheritdoc}
126
   */
127
  public function defaultConfiguration() {
128
    return parent::defaultConfiguration() + [
129
      'crop_type' => NULL,
130
    ];
131
  }
132
133
  /**
134
   * {@inheritdoc}
135
   */
136
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
137
    $options = [];
138
    foreach ($this->typeStorage->loadMultiple() as $type) {
139
      $options[$type->id()] = $type->label();
140
    }
141
142
    $form['crop_type'] = [
143
      '#type' => 'select',
144
      '#title' => t('Crop type'),
145
      '#default_value' => $this->configuration['crop_type'],
146
      '#options' => $options,
147
      '#description' => t('Crop type to be used for the image style.'),
148
    ];
149
150
    return $form;
151
  }
152
153
  /**
154
   * {@inheritdoc}
155
   */
156
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
157
    parent::submitConfigurationForm($form, $form_state);
158
    $this->configuration['crop_type'] = $form_state->getValue('crop_type');
159
  }
160
161
  /**
162
   * Gets crop entity for the image.
163
   *
164
   * @param ImageInterface $image
165
   *   Image object.
166
   *
167
   * @return \Drupal\Core\Entity\EntityInterface|\Drupal\crop\CropInterface|FALSE
168
   *   Crop entity or FALSE if crop doesn't exist.
169
   */
170
  protected function getCrop(ImageInterface $image) {
171
    return $this->getCropFromUri($image->getSource());
172
  }
173
174
  /**
175
   * Gets crop entity for the given URI.
176
   *
177
   * @param $uri
178
   *    URI of the image
179
   *
180
   * @return bool|\Drupal\Core\Entity\EntityInterface|\Drupal\crop\CropInterface|FALSE
181
   *    Crop entity or FALSE if crop doesn't exist
182
   */
183
  protected function getCropFromUri($uri) {
184
    if (!isset($this->crop)) {
185
      $this->crop = FALSE;
0 ignored issues
show
Documentation Bug introduced by
It seems like FALSE of type false is incompatible with the declared type object<Drupal\crop\CropInterface> of property $crop.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
186
      if ($crop = Crop::findCrop($uri, $this->configuration['crop_type'])) {
187
        $this->crop = $crop;
188
      }
189
    }
190
191
    return $this->crop;
192
  }
193
194
}
195