ImageHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 31
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A expand() 0 24 3
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal8;
4
5
/**
6
 * Image field handler for Drupal 8.
7
 */
8
class ImageHandler extends AbstractHandler {
9
10
  /**
11
   * {@inheritdoc}
12
   */
13
  public function expand($values) {
14
    $data = file_get_contents($values[0]);
15
    if (FALSE === $data) {
16
      throw new \Exception("Error reading file");
17
    }
18
19
    /** @var \Drupal\file\FileInterface $file */
20
    $file = file_save_data(
21
      $data,
22
      'public://' . uniqid() . '.jpg');
23
24
    if (FALSE === $file) {
25
      throw new \Exception("Error saving file");
26
    }
27
28
    $file->save();
29
30
    $return = [
31
      'target_id' => $file->id(),
32
      'alt' => 'Behat test image',
33
      'title' => 'Behat test image',
34
    ];
35
    return $return;
36
  }
37
38
}
39