Completed
Push — develop ( 12ceca...974b72 )
by Mohamed
04:39
created

XlsHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 9
c 5
b 2
f 0
lcom 1
cbo 5
dl 0
loc 165
ccs 0
cts 53
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sheetRow() 0 20 4
A setProject() 0 6 1
B handle() 0 40 2
A globalStyle() 0 6 1
A sheetTitle() 0 7 1
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
    public function setProject(Project $project)
78
    {
79
        $this->project = $project;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param Exporter $exporter
86
     *
87
     * @return void
88
     */
89
    public function handle(Exporter $exporter)
90
    {
91
        $params = $exporter->getParams();
92
93
        $this->setProject($exporter->getParams('route.project'));
0 ignored issues
show
Documentation introduced by
$exporter->getParams('route.project') is of type string|object<Illuminate...se\Eloquent\Model>|null, but the function expects a object<Tinyissue\Model\Project>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94
95
        $query = $this->project->issues()->select(array_keys($this->columns));
0 ignored issues
show
Bug introduced by
It seems like select() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
97
        // Filter issues
98
        $this->project->filterTitleOrBody($query, $params['keyword']);
99
        $this->project->filterAssignTo($query, $params['keyword']);
100
        $this->project->filterTitleOrBody($query, $params['keyword']);
101
102
        // Fetch issues
103
        $this->issues = $query->get();
104
105
        // Create CSV file
106
        $exporter->sheet($this->project->name, function (LaravelExcelWorksheet $sheet) {
107
108
            // Global sheet styles
109
            $this->globalStyle($sheet);
110
111
            // Title
112
            $sheet->mergeCells('A1:G1');
113
            $sheet->setHeight(1, 50);
1 ignored issue
show
Documentation introduced by
50 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
            $sheet->cell('A1', function (CellWriter $cell) {
115
                $this->sheetTitle($cell);
116
            });
117
118
            // Header
119
            $sheet->row(2, array_map('trans', $this->header));
120
121
            // Rows
122
            $index = 3;
123
            foreach ($this->issues as $issue) {
124
                $this->sheetRow($sheet, $index, $issue);
125
                ++$index;
126
            }
127
        });
128
    }
129
130
    /**
131
     * Setup sheet global styles.
132
     *
133
     * @param LaravelExcelWorksheet $sheet
134
     *
135
     * @return void
136
     */
137
    protected function globalStyle(LaravelExcelWorksheet $sheet)
138
    {
139
        // Font size & family
140
        $sheet->setFontSize(15);
141
        $sheet->setFontFamily('Calibri');
142
    }
143
144
    /**
145
     * Sheet title in the first row.
146
     *
147
     * @param CellWriter $cell
148
     *
149
     * @return void
150
     */
151
    public function sheetTitle(CellWriter $cell)
152
    {
153
        $cell->setFontWeight('bold');
154
        $cell->setBorder('none', 'none', 'none', 'thin');
155
        $title = trans('tinyissue.export_xls_title', ['name' => $this->project->name, 'count' => count($this->issues)]);
156
        $cell->setValue($title);
157
    }
158
159
    /**
160
     * Sheet data row.
161
     *
162
     * @param LaravelExcelWorksheet $sheet
163
     * @param int                   $index
164
     * @param Project\Issue         $issue
165
     *
166
     * @return void
167
     */
168
    protected function sheetRow(LaravelExcelWorksheet $sheet, $index, Project\Issue $issue)
169
    {
170
        // Setup row data
171
        array_walk($this->columns, function (&$column, $key, Project\Issue $issue) {
172
            $column = (string) $issue->$key;
173
            if ($key === 'status') {
174
                $column = (int) $issue->status === Project\Issue::STATUS_OPEN ? 'open' : 'closed';
175
                $column = trans('tinyissue.' . $column);
176
            }
177
        }, $issue);
178
179
        // Write row
180
        $sheet->row($index, $this->columns);
181
182
        // Format last cell
183
        $sheet->cell('G' . $index, function (CellWriter $cell) use ($issue) {
184
            $color = (int) $issue->status === Project\Issue::STATUS_CLOSED ? '#FF0000' : '#00FF00';
185
            $cell->setBackground($color);
186
        });
187
    }
188
}
189