Completed
Pull Request — master (#201)
by
unknown
02:10
created

ExcelDownload::getBaseName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Nayjest\Grids\Components;
4
5
use Event;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Pagination\Paginator;
8
use Illuminate\Foundation\Application;
9
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet;
10
use Maatwebsite\Excel\Excel;
11
use Maatwebsite\Excel\Writers\LaravelExcelWriter;
12
use Nayjest\Grids\Components\Base\RenderableComponent;
13
use Nayjest\Grids\Components\Base\RenderableRegistry;
14
use Nayjest\Grids\DataProvider;
15
use Nayjest\Grids\DataRow;
16
use Nayjest\Grids\FieldConfig;
17
use Nayjest\Grids\Grid;
18
19
class ExcelDownload extends RenderableComponent
20
{
21
    private $baseName;
22
    private $date;
23
24
    const NAME = 'excel_download';
25
    const INPUT_PARAM = 'dld';
26
27
    protected $template = '*.components.excel_download';
28
    protected $name = ExcelDownload::NAME;
29
    protected $render_section = RenderableRegistry::SECTION_END;
30
    protected $extension = 'dld';
31
32
    /**
33
     * @return mixed
34
     */
35
    public function getBaseName()
36
    {
37
        return $this->baseName;
38
    }
39
40
    /**
41
     * @param mixed $baseName
42
     */
43
    public function setBaseName($baseName)
44
    {
45
        $this->baseName = $baseName;
46
        return $this;
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52
    public function getDate()
53
    {
54
        return $this->date;
55
    }
56
57
    /**
58
     * @param mixed $date
59
     */
60
    public function setDate($date)
61
    {
62
        $this->date = $date;
63
        return $this;
64
    }
65
    
66
67
    public function initialize(Grid $grid)
68
    {
69
        parent::initialize($grid);
70
        Event::listen(Grid::EVENT_PREPARE, function (Grid $grid) {
71
            if ($this->grid !== $grid) {
72
                return;
73
            }
74
            if ($grid->getInputProcessor()->getValue(static::INPUT_PARAM, false)) {
75
                foreach (glob(Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix() . 'public/excels/*.xlsx', GLOB_BRACE) as $fileName) {
0 ignored issues
show
Bug introduced by
The method getDriver() does not seem to exist on object<Illuminate\Contra...\Filesystem\Filesystem>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
                    if (strpos($fileName, $this->getBaseName())) {
77
                        $this->responseWithProtectedFile($fileName);
78
                    }
79
                }
80
            }
81
        });
82
    }
83
    public function responseWithProtectedFile($path)
84
    {
85
        $fileContents = Storage::disk('public')->get('/excels/supporters_2019-02-06-165444.xlsx');
86
        $fileMimeType = Storage::getMimeType($path);
87
        return response($fileContents, 200)->header('Content-Type', $fileMimeType);
88
    }
89
}