Completed
Push — master ( d17503...204a77 )
by Jonathan
02:15
created

FileHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B expand() 0 31 3
1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\Driver\Fields\Drupal7\FileHandler.
6
 */
7
8
namespace Drupal\Driver\Fields\Drupal7;
9
10
/**
11
 * File field handler for Drupal 7.
12
 */
13
class FileHandler extends AbstractHandler {
14
15
  /**
16
   * {@inheritdoc}
17
   *
18
   * Specify files in file fields by their filename.
19
   */
20
  public function expand($values) {
21
    $return = array();
22
23
    foreach ($values as $value) {
24
      $query = new \EntityFieldQuery();
25
26
      $query->entityCondition('entity_type', 'file')
27
        ->propertyCondition('filename', $value)
28
        ->propertyOrderBy('timestamp', 'DESC')
29
        ->range(0, 1);
30
31
      $result = $query->execute();
32
33
      if (!empty($result['file'])) {
34
        $files = entity_load('file', array_keys($result['file']));
35
        $file = current($files);
36
37
        $return[$this->language][] = array(
38
          'filename' => $file->filename,
39
          'uri' => $file->uri,
40
          'fid' => $file->fid,
41
          'display' => 1,
42
        );
43
      }
44
      else {
45
        throw new \Exception(sprintf('File with filename "%s" not found.', $value));
46
      }
47
    }
48
49
    return $return;
50
  }
51
52
}
53