ExcelBuilder   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 3
dl 0
loc 96
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getYamlKey() 0 4 1
A getFileName() 0 6 2
A getFileType() 0 11 3
A getDateTimeFormat() 0 6 2
A getExport() 0 9 2
B fillExport() 0 20 6
A setUserExcelColumnConfiguration() 0 11 5
A getExportCredentials() 0 4 1
1
<?php
2
3
namespace Admingenerator\GeneratorBundle\Builder\Admin;
4
5
use Admingenerator\GeneratorBundle\Generator\Column;
6
7
/**
8
 * This builder generates php for list actions
9
 *
10
 * @author cedric Lombardot
11
 * @author Piotr Gołębiewski <[email protected]>
12
 * @author Bob van de Vijver
13
 */
14
class ExcelBuilder extends ListBuilder
15
{
16
  /**
17
   * @var array
18
   */
19
  protected $export = null;
20
21
  /**
22
   * (non-PHPdoc)
23
   * @see Admingenerator\GeneratorBundle\Builder.BaseBuilder::getYamlKey()
24
   */
25
  public function getYamlKey()
26
  {
27
    return 'excel';
28
  }
29
30
  public function getFileName($key = null){
31
    if(null === ($filename = $this->getVariable('filename'))){
32
      $filename = 'admin_export_'. str_replace(' ', '_', strtolower($this->getGenerator()->getFromYaml('builders.list.params.title'))). '.xlsx';
33
    }
34
    return $this->getExportParamsForKey($key, 'filename', $filename);
35
  }
36
37
  public function getFileType($key = null){
38
    if(null === ($filetype = $this->getVariable('filetype'))){
39
      if(class_exists('\PhpOffice\PhpSpreadsheet\Spreadsheet'))
40
      {
41
        $filetype = 'Xlsx';
42
      } else {
43
        $filetype = 'Excel2007';
44
      }
45
    }
46
    return $this->getExportParamsForKey($key, 'filetype', $filetype);
47
  }
48
49
  public function getDateTimeFormat($key = null){
50
    if(null === ($dateTimeFormat = $this->getVariable('datetime_format'))){
51
      $dateTimeFormat = 'Y-m-d H:i:s';
52
    }
53
    return $this->getExportParamsForKey($key, 'datetime_format', $dateTimeFormat);
54
  }
55
56
  /**
57
   * Return a list of columns from excel.export
58
   * 
59
   * @return array
60
   */
61
  public function getExport()
62
  {
63
      if (null === $this->export) {
64
          $this->export = array();
65
          $this->fillExport();
66
      }
67
68
      return $this->export;
69
  }
70
71
  protected function fillExport()
72
  {
73
      $export = $this->getVariable('export',[]);
74
      if (!count($export)) return [];
75
76
      foreach ($export as $keyName => $columns ) {
77
          $params = [];
78
          $this->export[$keyName] = []; 
79
          if (isset($columns['display'])) {
80
              $params = isset($columns['fields']) ? $columns['fields'] : [];
81
              $columns = $columns['display'];
82
          } 
83
          foreach ($columns as $columnName) {
84
              $column = $this->createColumn($columnName, false);
85
              $this->setUserColumnConfiguration($column);              
86
              $this->setUserExcelColumnConfiguration($column, $params);              
87
              $this->export[$keyName][$columnName] = $column;
88
          }
89
      }
90
  }
91
92
  protected function setUserExcelColumnConfiguration(Column $column, array $optionsFields)
93
  {
94
      if (!count($optionsFields)) return;
95
96
      $options = is_array($optionsFields) && array_key_exists($column->getName(), $optionsFields) ?
97
          $optionsFields[$column->getName()] : array();
98
99
      foreach ($options as $option => $value) {
100
          $column->setProperty($option, $value);
101
      }
102
  }
103
104
  public function getExportCredentials($key = null)
105
  {
106
    return $this->getExportParamsForKey($key, 'credentials', null);
107
  }
108
109
}