Completed
Pull Request — master (#123)
by
unknown
02:21
created

FileHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 1
dl 0
loc 34
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B expand() 0 27 4
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal8;
4
5
/**
6
 * File field handler for Drupal 8.
7
 */
8
class FileHandler 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
    $ext = pathinfo($values[0], PATHINFO_EXTENSION);
20
    $ext = !empty($ext) ? '.' . $ext : '';
21
22
    /* @var \Drupal\file\FileInterface $file */
23
    $file = file_save_data(
24
        $data,
25
        'public://' . uniqid() . $ext);
26
27
    if (FALSE === $file) {
28
      throw new \Exception("Error saving file");
29
    }
30
31
    $file->save();
32
33
    $return = array(
34
      'target_id' => $file->id(),
35
      'display' => '1',
36
      'description' => 'Behat test file',
37
    );
38
    return $return;
39
  }
40
41
}
42