Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

CsvHandler::handle()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 38
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 38
ccs 18
cts 18
cp 1
rs 8.8571
cc 2
eloc 17
nc 1
nop 1
crap 2
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 Tinyissue\Model\Project;
16
use Tinyissue\Services\Exporter;
17
18
/**
19
 * CsvHandler is an export class for exporting a project issues into a csv file.
20
 *
21
 * @author Mohamed Alsharaf <[email protected]>
22
 */
23
class CsvHandler
24
{
25
    /**
26
     * CSV columns.
27
     *
28
     * @var array
29
     */
30
    protected $columns = [
31
        'tinyissue.id'            => 'id',
32
        'tinyissue.project'       => 'project',
33
        'tinyissue.title'         => 'title',
34
        'tinyissue.time_quote'    => 'time_quote',
35
        'tinyissue.label_created' => 'created_at',
36
        'tinyissue.updated'       => 'updated_at',
37
        'tinyissue.label_closed'  => 'closed_at',
38
        'tinyissue.status'        => 'status',
39
    ];
40
41
    /**
42
     * @param Exporter $exporter
43
     *
44
     * @return void
45
     */
46 3
    public function handle(Exporter $exporter)
47
    {
48
        // Translate export columns
49 3
        $this->columns = array_combine(array_map('trans', array_keys($this->columns)), $this->columns);
50
51
        // Export parameters
52 3
        $params = $exporter->getParams();
53
54
        /** @var Project $project */
55 3
        $project = $params['route']['project'];
56
57
        // Query issues select specific columns
58
        $query = $project->issues()->select(array_filter($this->columns, function ($column) {
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...
59 3
            return $column !== 'project';
60 3
        }));
61
62
        // Filter issues
63 3
        $project->filterTags($query, $params['tags']);
64 3
        $project->filterAssignTo($query, $params['assignto']);
65 3
        $project->filterTitleOrBody($query, $params['keyword']);
66
67
        // Fetch issues
68
        $issues = $query->get()->map(function (Project\Issue $issue) use ($project) {
69
            return array_map(function ($column) use ($issue, $project) {
70
71 3
                if ($column == 'project') {
72 3
                    return $project->name;
73
                }
74
75 3
                return (string) $issue->$column;
76 3
            }, $this->columns);
77 3
        });
78
79
        // Create CSV file
80 3
        $exporter->sheet('issues', function (LaravelExcelWorksheet $sheet) use ($issues) {
81 3
            $sheet->fromArray($issues);
82 3
        });
83 3
    }
84
}
85