Completed
Pull Request — master (#1281)
by
unknown
02:32
created

AbstractExporter::chunk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Exporters;
4
5
use Encore\Admin\Grid;
6
7
abstract class AbstractExporter implements ExporterInterface
8
{
9
    /**
10
     * @var \Encore\Admin\Grid
11
     */
12
    protected $grid;
13
14
    /**
15
     * Create a new exporter instance.
16
     *
17
     * @param $grid
18
     */
19
    public function __construct(Grid $grid = null)
20
    {
21
        if ($grid) {
22
            $this->setGrid($grid);
23
        }
24
    }
25
26
    /**
27
     * Set grid for exporter.
28
     *
29
     * @param Grid $grid
30
     *
31
     * @return $this
32
     */
33
    public function setGrid(Grid $grid)
34
    {
35
        $this->grid = $grid;
36
37
        return $this;
38
    }
39
40
    /**
41
     * Get table of grid.
42
     *
43
     * @return string
44
     */
45
    public function getTable()
46
    {
47
        return $this->grid->model()->eloquent()->getTable();
48
    }
49
50
    /**
51
     * Get data with export query.
52
     *
53
     * @return array
54
     */
55
    public function getData()
56
    {
57
        return $this->grid->getFilter()->execute();
58
    }
59
60
    /**
61
     * @param callable $callback
62
     * @param int      $count
63
     *
64
     * @return bool
65
     */
66
    public function chunk(callable $callback, $count = 100)
67
    {
68
        return $this->grid->getFilter()->chunk($callback, $count);
69
    }
70
71
    /**
72
     * Export data with scope.
73
     *
74
     * @param string $scope
75
     *
76
     * @return $this
77
     */
78
    public function withScope($scope)
79
    {
80
        if ($scope == Grid\Exporter::SCOPE_ALL) {
81
            return $this;
82
        }
83
84
        list($scope, $args) = explode(':', $scope);
85
86
        if ($scope == Grid\Exporter::SCOPE_CURRENT_PAGE) {
87
            $this->grid->model()->usePaginate(true);
88
        }
89
90
        if ($scope == Grid\Exporter::SCOPE_SELECTED_ROWS) {
91
            $selected = explode(',', $args);
92
            $this->grid->model()->whereIn($this->grid->getKeyName(), $selected);
0 ignored issues
show
Documentation Bug introduced by
The method whereIn does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
93
        }
94
95
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    abstract public function export();
102
}
103