XlsHandler   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.92%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 10
c 1
b 1
f 0
lcom 1
cbo 5
dl 0
loc 168
ccs 47
cts 48
cp 0.9792
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setProject() 0 6 1
B handle() 0 43 3
A globalStyle() 0 6 1
A sheetTitle() 0 7 1
A sheetRow() 0 20 4
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Export\Project\Issue;
13
14
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet;
15
use Maatwebsite\Excel\Writers\CellWriter;
16
use Tinyissue\Model\Project;
17
use Tinyissue\Services\Exporter;
18
19
/**
20
 * XlsHandler is an export class for exporting a project issues into a Xls or Xlsx file.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 */
24
class XlsHandler
25
{
26
    /**
27
     * Instance of the project.
28
     *
29
     * @var Project
30
     */
31
    protected $project;
32
33
    /**
34
     * Collection of project issues.
35
     *
36
     * @var Project\Issue[]
37
     */
38
    protected $issues;
39
40
    /**
41
     * Columns to export from the issues table.
42
     *
43
     * @var array
44
     */
45
    protected $columns = [
46
        'id'         => '',
47
        'title'      => '',
48
        'time_quote' => '',
49
        'created_at' => '',
50
        'updated_at' => '',
51
        'closed_at'  => '',
52
        'status'     => '',
53
    ];
54
55
    /**
56
     * Columns in the output file.
57
     *
58
     * @var array
59
     */
60
    protected $header = [
61
        'tinyissue.id',
62
        'tinyissue.title',
63
        'tinyissue.time_quote',
64
        'tinyissue.label_created',
65
        'tinyissue.updated',
66
        'tinyissue.label_closed',
67
        'tinyissue.status',
68
    ];
69
70
    /**
71
     * Set an instance of a project model.
72
     *
73
     * @param Project $project
74
     *
75
     * @return $this
76
     */
77 1
    public function setProject(Project $project)
78
    {
79 1
        $this->project = $project;
80
81 1
        return $this;
82
    }
83
84
    /**
85
     * @param Exporter $exporter
86
     *
87
     * @return void
88
     */
89 1
    public function handle(Exporter $exporter)
90
    {
91 1
        $params  = $exporter->getParams();
92 1
        $project = $exporter->getParams('route.project');
93 1
        if (!$project instanceof Project) {
94
            throw new \LogicException('Unable to find project instance in the current request.');
95
        }
96 1
        $this->setProject($project);
97
98 1
        $query = $this->project->issues()->select(array_keys($this->columns));
99
100
        // Filter issues
101 1
        $this->project->filterTitleOrBody($query, $params['keyword']);
102 1
        $this->project->filterAssignTo($query, $params['keyword']);
103 1
        $this->project->filterTitleOrBody($query, $params['keyword']);
104
105
        // Fetch issues
106 1
        $this->issues = $query->get();
107
108
        // Create CSV file
109
        $exporter->sheet($this->project->name, function (LaravelExcelWorksheet $sheet) {
110
111
            // Global sheet styles
112 1
            $this->globalStyle($sheet);
113
114
            // Title
115 1
            $sheet->mergeCells('A1:G1');
116 1
            $sheet->setHeight(1, 50);
117
            $sheet->cell('A1', function (CellWriter $cell) {
118 1
                $this->sheetTitle($cell);
119 1
            });
120
121
            // Header
122 1
            $sheet->row(2, array_map('trans', $this->header));
123
124
            // Rows
125 1
            $index = 3;
126 1
            foreach ($this->issues as $issue) {
127 1
                $this->sheetRow($sheet, $index, $issue);
128 1
                ++$index;
129
            }
130 1
        });
131 1
    }
132
133
    /**
134
     * Setup sheet global styles.
135
     *
136
     * @param LaravelExcelWorksheet $sheet
137
     *
138
     * @return void
139
     */
140 1
    protected function globalStyle(LaravelExcelWorksheet $sheet)
141
    {
142
        // Font size & family
143 1
        $sheet->setFontSize(15);
144 1
        $sheet->setFontFamily('Calibri');
145 1
    }
146
147
    /**
148
     * Sheet title in the first row.
149
     *
150
     * @param CellWriter $cell
151
     *
152
     * @return void
153
     */
154 1
    public function sheetTitle(CellWriter $cell)
155
    {
156 1
        $cell->setFontWeight('bold');
157 1
        $cell->setBorder('none', 'none', 'none', 'thin');
158 1
        $title = trans('tinyissue.export_xls_title', ['name' => $this->project->name, 'count' => count($this->issues)]);
159 1
        $cell->setValue($title);
160 1
    }
161
162
    /**
163
     * Sheet data row.
164
     *
165
     * @param LaravelExcelWorksheet $sheet
166
     * @param int                   $index
167
     * @param Project\Issue         $issue
168
     *
169
     * @return void
170
     */
171 1
    protected function sheetRow(LaravelExcelWorksheet $sheet, $index, Project\Issue $issue)
172
    {
173
        // Setup row data
174
        array_walk($this->columns, function (&$column, $key, Project\Issue $issue) {
175 1
            $column = (string) $issue->$key;
176 1
            if ($key === 'status') {
177 1
                $column = (int) $issue->status === Project\Issue::STATUS_OPEN ? 'open' : 'closed';
178 1
                $column = trans('tinyissue.' . $column);
179
            }
180 1
        }, $issue);
181
182
        // Write row
183 1
        $sheet->row($index, $this->columns);
184
185
        // Format last cell
186 1
        $sheet->cell('G' . $index, function (CellWriter $cell) use ($issue) {
187 1
            $color = (int) $issue->status === Project\Issue::STATUS_CLOSED ? '#FF0000' : '#00FF00';
188 1
            $cell->setBackground($color);
189 1
        });
190 1
    }
191
}
192