Completed
Push — master ( a4261d...fc5e52 )
by John
14s
created

GroupAnalysisExport::collection()   F

Complexity

Conditions 21
Paths 204

Size

Total Lines 74
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 21
eloc 55
nc 204
nop 0
dl 0
loc 74
rs 3.2833
c 1
b 0
f 0

How to fix   Long Method    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
3
namespace App\Exports;
4
5
use Maatwebsite\Excel\Concerns\FromCollection;
6
use Maatwebsite\Excel\Concerns\WithEvents;
7
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
8
use Maatwebsite\Excel\Events\AfterSheet;
9
class GroupAnalysisExport implements FromCollection, WithEvents, WithStrictNullComparison
10
{
11
    private $contest_data;
12
13
    private $tag_problems;
14
15
    private $member_data;
16
    private $config;
17
18
    public function __construct($data, $config = [])
19
    {
20
        if($config['mode'] == 'contest'){
21
            $this->contest_data = $data['contest_data'];
22
            $this->member_data = $data['member_data'];
23
        }else{
24
            $this->member_data = $data['member_data'];
25
            $this->tag_problems = $data['tag_problems'];
26
        }
27
        $this->config = $config;
28
    }
29
30
    /**
31
    * @return array
32
    */
33
    public function registerEvents(): array
34
    {
35
        return [
36
            AfterSheet::class => function(AfterSheet $event) {
37
                if($this->config['mode'] === 'contest'){
38
                    $mergeCell = ['A1:A2','B1:E1'];
39
                    foreach ($this->contest_data as $c) {
40
                        array_push($mergeCell,$this->mergeCellColumnNext());
41
                    }
42
                    $event->sheet->getDelegate()->setMergeCells($mergeCell);
43
                }
44
            },
45
        ];
46
    }
47
48
    /**
49
    * @return \Illuminate\Support\Collection
50
    */
51
    public function collection()
52
    {
53
        $maxium = $this->config['maxium'] ?? false;
54
        $percent = $this->config['percent'] ?? false;
55
56
        if($this->config['mode'] == 'contest'){
57
            $row_1 = ['Member','Total','','',''];
58
            $row_2 = ['','Elo','Rank','Solved','Penalty'];
59
            foreach ($this->contest_data as $contest) {
60
                array_push($row_1,$contest['name'],'');
61
                array_push($row_2,'Solved','Penalty');
62
            }
63
            $data = [$row_1,$row_2];
64
            foreach ($this->member_data as $member) {
65
                $display_name = $member['name'];
66
                if(!empty($member['nick_name'])){
67
                    $display_name .= ' ('.$member['nick_name'].')';
68
                }
69
                $row = [
70
                    $display_name,
71
                    $member['elo'],
72
                    !empty($member['rank_ave']) ? round($member['rank_ave'],1) : '-',
73
                    $percent === 'true'  ? ($member['problem_all'] != 0 ? round( $member['solved_all'] / $member['problem_all'] * 100 , 1) : '-'). ' %'
74
                            : ($maxium === 'true'  ? $member['solved_all'] . ' / ' . $member['problem_all'] : $member['solved_all']),
75
                    round($member['penalty']),
76
                ];
77
                foreach ($this->contest_data as $contest) {
78
                    if(in_array($contest['cid'] , array_keys($member['contest_detial']))){
79
                        $contest_detial = $member['contest_detial'][$contest['cid']];
80
                        array_push(
81
                            $row,
82
                            $percent === 'true' ? (round($contest_detial['solved'] / $contest_detial['problems'] * 100,1) . ' %')
83
                                    : ($maxium === 'true' ? $contest_detial['solved'].' / '.$contest_detial['problems'] : $contest_detial['solved']),
84
                            round($contest_detial['penalty'])
85
                        );
86
                    }else{
87
                        array_push(
88
                            $row,
89
                            $percent === 'true' ? '- %'
90
                                    : ($maxium === 'true' ? '- / -' : ' - '),
91
                            0
92
                        );
93
                    }
94
                }
95
                array_push($data,$row);
96
            }
97
        }else{
98
            $row_1 = ['Member'];
99
            $row_2 = [''];
100
            foreach ($this->tag_problems as $tag => $tag_problem_set) {
101
                array_push($row_1,$tag);
102
                array_push($row_2,'Solved');
103
            }
104
            $data = [$row_1,$row_2];
105
            foreach ($this->member_data as $member) {
106
                $display_name = $member['name'];
107
                if(!empty($member['nick_name'])){
108
                    $display_name .= ' ('.$member['nick_name'].')';
109
                }
110
                $row = [
111
                    $display_name,
112
                ];
113
                foreach($member['completion'] as $tag => $tag_completion){
114
                    $count = count($tag_completion);
115
                    $solved = eval('return '.join('+',array_values($tag_completion)).';');
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
116
                    array_push(
117
                        $row,
118
                        $percent === 'true' ? (round($solved / $count * 100,1) . ' %') : ($maxium  === 'true' ? $solved .' / '.$count : $solved)
119
                    );
120
                }
121
                array_push($data,$row);
122
            }
123
        }
124
        return collect($data);
125
    }
126
127
    private function mergeCellColumnNext(){
128
        static $columns = [
129
            [3],[4]
130
        ];
131
        $columns_str = ['',''];
132
        $chars = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
133
        foreach($columns as $key => &$column){
134
            $column[0] += 2;
135
            if($column[0] > 25){
136
                if(isset($column[1])){
137
                    $column[1] += 1;
138
                    if($column[1] > 25){
139
                        if(isset($column[2])){
140
                            $column[2] += 1;
141
                        }else{
142
                            $column[2] = 0;
143
                        }
144
                    }
145
                }else{
146
                    $column[1] = 0;
147
                }
148
            }
149
            $columns_str[$key] = '';
150
            $reverse_column = array_reverse($column);
151
            foreach ($reverse_column as $ord) {
152
                $columns_str[$key] .= $chars[$ord];
153
            }
154
            $columns_str[$key] .= '1';
155
        }
156
        return $columns_str[0].':'.$columns_str[1];
157
    }
158
}
159