Passed
Push — master ( a5762d...03970c )
by Iman
04:00
created

PhpColConfig   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B makeColumnPhpCode() 0 25 3
B addProperties() 0 16 5
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator;
4
5
class PhpColConfig
6
{
7
    /**
8
     * @return array
9
     */
10
    public static function makeColumnPhpCode()
11
    {
12
        $labels = request('column');
13
        $name = request('name');
14
        $isImage = request('is_image');
15
        $isDownload = request('is_download');
16
        $callback = request('callback');
17
        $width = request('width');
18
19
        $indent = str_repeat(' ', 8);
20
21
        $columnScript = [];
22
        $columnScript[] = $indent.'$this->col[] = [];';
23
        foreach ($labels as $i => $label) {
24
25
            if (! $name[$i]) {
26
                continue;
27
            }
28
29
            $colProperties = ["'label' => '$label'", "'name' => '{$name[$i]}'"];
30
            $colProperties = self::addProperties($colProperties, $isImage[$i], $isDownload[$i], $callback[$i], $width[$i]);
31
32
            $columnScript[] = $indent.'$this->col[] = ['.implode(", ", $colProperties).'];';
33
        }
34
        return implode("\n", $columnScript);
0 ignored issues
show
Bug Best Practice introduced by
The expression return implode(' ', $columnScript) returns the type string which is incompatible with the documented return type array.
Loading history...
35
    }
36
37
    /**
38
     * @param $colProperties
39
     * @param $isImage
40
     * @param $isDownload
41
     * @param $callback
42
     * @param $width
43
     * @return array
44
     */
45
    private static function addProperties($colProperties, $isImage, $isDownload, $callback, $width)
46
    {
47
        if ($isImage) {
48
            $colProperties[] = '"image" => true ';
49
        }
50
        if ($isDownload) {
51
            $colProperties[] = '"download" => true';
52
        }
53
        if ($callback) {
54
            $colProperties[] = '"callback" => function($row) {'.$callback.'}';
55
        }
56
        if ($width) {
57
            $colProperties[] = "'width' => '$width'";
58
        }
59
60
        return $colProperties;
61
    }
62
}