Completed
Pull Request — master (#157)
by
unknown
01:46
created

ImageDrupal8::processValue()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 15

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
dl 26
loc 26
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
1
<?php
2
namespace Drupal\Driver\Plugin\DriverField;
3
4
use Drupal\Driver\Plugin\DriverFieldPluginDrupal8Base;
5
6
/**
7
 * A driver field plugin for image fields.
8
 *
9
 * @DriverField(
10
 *   id = "image",
11
 *   version = 8,
12
 *   fieldTypes = {
13
 *     "image",
14
 *   },
15
 *   weight = -100,
16
 * )
17
 */
18 View Code Duplication
class ImageDrupal8 extends DriverFieldPluginDrupal8Base
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...
19
{
20
21
  /**
22
   * {@inheritdoc}
23
   */
24
    protected function processValue($value)
25
    {
26
        $data = file_get_contents($value['target_id']);
27
        if (false === $data) {
28
            throw new \Exception("Error reading file");
29
        }
30
31
        /* @var \Drupal\file\FileInterface $file */
32
        $file = file_save_data(
33
            $data,
34
            'public://' . uniqid() . '.jpg'
35
        );
36
37
        if (false === $file) {
38
            throw new \Exception("Error saving file");
39
        }
40
41
        $file->save();
42
43
        $return = array(
44
        'target_id' => $file->id(),
45
        'alt' => 'Behat test image',
46
        'title' => 'Behat test image',
47
        );
48
        return $return;
49
    }
50
}
51