Completed
Pull Request — master (#123)
by
unknown
08:19
created

FileHandler::expand()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 24
rs 8.9713
cc 3
eloc 15
nc 3
nop 1
1
<?php
2
3
namespace Drupal\Driver\Fields\Drupal8;
4
5
/**
6
 * File field handler for Drupal 8.
7
 */
8 View Code Duplication
class FileHandler extends AbstractHandler {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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());
23
24
        if (FALSE === $file) {
25
            throw new \Exception("Error saving file");
26
        }
27
28
        $file->save();
29
30
        $return = array(
31
            'target_id' => $file->id(),
32
            'display' => '1',
33
            'description' => 'Behat test file',
34
        );
35
        return $return;
36
    }
37
38
}
39