Completed
Push — develop ( b779dc...323e79 )
by Mohamed
06:59
created

Exporter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.74%

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 6
c 6
b 3
f 1
lcom 1
cbo 2
dl 0
loc 101
ccs 18
cts 19
cp 0.9474
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilename() 0 4 1
A getParams() 0 8 2
A getHandlerClassName() 0 11 2
A exportFile() 0 16 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\Services;
13
14
use Maatwebsite\Excel\Exceptions\LaravelExcelException;
15
use Maatwebsite\Excel\Files\NewExcelFile;
16
17
/**
18
 * Exporter is class for initialising the exporter, process data, and export the generated file.
19
 *
20
 * @author Mohamed Alsharaf <[email protected]>
21
 *
22
 * @method $this sheet($sheetID, $callback = null)
23
 * @method $this store($ext = 'xls', $path = false, $returnInfo = false)
24
 * @method $this setFileName($fileName)
25
 */
26
class Exporter extends NewExcelFile
27
{
28
    /** Current supported files type */
29
    const TYPE_CSV  = 'csv';
30
    const TYPE_XLS  = 'xls';
31
    const TYPE_XLSX = 'xlsx';
32
33
    /**
34
     * Parameters.
35
     *
36
     * @var array
37
     */
38
    protected $params = [];
39
40
    /**
41
     * Export file format.
42
     *
43
     * @var string
44
     */
45
    protected $format = self::TYPE_CSV;
46
47
    /**
48
     * Type of data to export.
49
     *
50
     * @var string
51
     */
52
    protected $type = '';
53
54
    /**
55
     * Returns the parameters.
56
     *
57
     * @param string $key
58
     *
59
     * @return string|\Illuminate\Database\Eloquent\Model|null
60
     */
61 4
    public function getParams($key = null)
62
    {
63 4
        if (null === $key) {
64 4
            return $this->params;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->params; (array) is incompatible with the return type documented by Tinyissue\Services\Exporter::getParams of type string|Illuminate\Database\Eloquent\Model|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
        }
66
67 1
        return array_get($this->params, $key);
68
    }
69
70
    /**
71
     * Start importing.
72
     *
73
     * @param string $className
74
     * @param string $format
75
     * @param array  $params
76
     *
77
     * @return array['full', 'path', 'file', 'title', 'ext']
0 ignored issues
show
Documentation introduced by
The doc-type array['full', could not be parsed: Expected "]" at position 2, but found "'full'". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
78
     */
79 4
    public function exportFile($className, $format = self::TYPE_CSV, array $params = [])
80
    {
81 4
        $params['route'] = $this->app->request->route()->parameters();
82 4
        $this->format    = $format;
83 4
        $this->params    = $params;
84 4
        $this->type      = $className;
85
86
        // Update file name
87 4
        $this->setFileName($this->getFilename());
88
89
        // Start exporting
90 4
        $this->handle($className);
91
92
        // Store file and return info
93 4
        return $this->store($format, false, true);
94
    }
95
96
    /**
97
     * Returns export file name.
98
     *
99
     * @return string
100
     */
101 4
    public function getFilename()
102
    {
103 4
        return 'export_' . str_replace('\\', '_', strtolower($this->type)) . '_' . time();
104
    }
105
106
    /**
107
     * Construct the export class full name.
108
     *
109
     * @param string $type
110
     *
111
     * @return string
112
     *
113
     * @throws LaravelExcelException
114
     */
115 4
    protected function getHandlerClassName($type)
116
    {
117 4
        $handler = '\\Tinyissue\Export\\' . $type . '\\' . ucfirst(substr($this->format, 0, 3)) . 'Handler';
118
119
        // Check if the handler exists
120 4
        if (!class_exists($handler)) {
121
            throw new LaravelExcelException("$type handler [$handler] does not exist.");
122
        }
123
124 4
        return $handler;
125
    }
126
}
127