Completed
Pull Request — master (#86)
by
unknown
02:38
created

ImageHandler::expand()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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