Completed
Push — master ( f09455...266dce )
by Song
02:34
created

CanExportGrid::export()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Concerns;
4
5
use Encore\Admin\Grid;
6
use Encore\Admin\Grid\Column;
7
use Encore\Admin\Grid\Exporter;
8
use Encore\Admin\Grid\Exporters\AbstractExporter;
9
10
trait CanExportGrid
11
{
12
    /**
13
     * Export driver.
14
     *
15
     * @var string
16
     */
17
    protected $exporter;
18
19
    /**
20
     * Handle export request.
21
     *
22
     * @param bool $forceExport
23
     */
24
    protected function handleExportRequest($forceExport = false)
25
    {
26
        if (!$scope = request(Exporter::$queryName)) {
27
            return;
28
        }
29
30
        // clear output buffer.
31
        if (ob_get_length()) {
32
            ob_end_clean();
33
        }
34
35
        $this->disablePagination();
0 ignored issues
show
Bug introduced by
It seems like disablePagination() 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...
36
37
        if ($forceExport) {
38
            $this->getExporter($scope)->export();
39
        }
40
    }
41
42
    /**
43
     * @param string $scope
44
     *
45
     * @return AbstractExporter
46
     */
47
    protected function getExporter($scope)
48
    {
49
        return (new Exporter($this))->resolve($this->exporter)->withScope($scope);
50
    }
51
52
    /**
53
     * Set exporter driver for Grid to export.
54
     *
55
     * @param $exporter
56
     *
57
     * @return $this
58
     */
59
    public function exporter($exporter)
60
    {
61
        $this->exporter = $exporter;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Get the export url.
68
     *
69
     * @param int $scope
70
     * @param null $args
71
     *
72
     * @return string
73
     */
74
    public function getExportUrl($scope = 1, $args = null)
75
    {
76
        $input = array_merge(request()->all(), Exporter::formatExportQuery($scope, $args));
77
78
        if ($constraints = $this->model()->getConstraints()) {
0 ignored issues
show
Bug introduced by
It seems like model() 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...
79
            $input = array_merge($input, $constraints);
80
        }
81
82
        return $this->resource() . '?' . http_build_query($input);
0 ignored issues
show
Bug introduced by
It seems like resource() 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...
83
    }
84
85
    /**
86
     * If grid show export btn.
87
     *
88
     * @return bool
89
     */
90
    public function showExportBtn()
91
    {
92
        return $this->option('show_exporter');
0 ignored issues
show
Bug introduced by
It seems like option() 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...
93
    }
94
95
    /**
96
     * Disable export.
97
     *
98
     * @return $this
99
     */
100
    public function disableExport(bool $disable = true)
101
    {
102
        return $this->option('show_exporter', !$disable);
0 ignored issues
show
Bug introduced by
It seems like option() 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...
103
    }
104
105
    /**
106
     * Render export button.
107
     *
108
     * @return string
109
     */
110
    public function renderExportButton()
111
    {
112
        return (new Grid\Tools\ExportButton($this))->render();
113
    }
114
115
    /**
116
     * @param \Closure $callback
117
     */
118
    public function export(\Closure $callback)
119
    {
120
        if (!$scope = request(Exporter::$queryName)) {
121
            return;
122
        }
123
124
        $this->getExporter($scope)->setCallback($callback);
125
126
        return $this;
127
    }
128
}
129