generateExportFileData()   B
last analyzed

Complexity

Conditions 11
Paths 24

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 7.3166
c 0
b 0
f 0
cc 11
nc 24
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
if (!class_exists('GridFieldExportAllAutomatedLinksButton')) {
3
    class GridFieldExportAllAutomatedLinksButton extends GridFieldExportButton {
4
        public function generateExportFileData($gridField) {
5
            $separator = $this->csvSeparator;
6
            $csvColumns =$gridField->getColumns();
7
            $fileData = '';
8
            if($this->csvHasHeader) {
9
                $headers = array();
10
                foreach($csvColumns as $columnSource => $columnHeader) {
11
                    $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader;
12
                }
13
                $fileData .= "\"" . implode("\"{$separator}\"", array_values($headers)) . "\"";
14
                $fileData .= "\n";
15
            }
16
            $items = $gridField->getList();
17
18
            foreach($gridField->getConfig()->getComponents() as $component){
19
                if($component instanceof GridFieldFilterHeader || $component instanceof GridFieldSortableHeader) {
20
                    $items = $component->getManipulatedData($gridField, $items);
21
                }
22
            }
23
            foreach($items->limit(null) as $item) {
24
                $columnData = array();
25
                foreach($csvColumns as $columnSource => $columnHeader) {
26
                    $value = ( $item->hasMethod( $columnHeader ) ) ? $item->$columnHeader() : $item->$columnHeader;
27
                    $value = str_replace(array("\r", "\n"), "\n", $value);
28
                    $columnData[] = '"' . str_replace('"', '\"', $value) . '"';
29
                }
30
                $fileData .= implode($separator, $columnData);
31
                $fileData .= "\n";
32
                $item->destroy();
33
            }
34
            return $fileData;
35
        }
36
    }
37
}
38