CollectionSource::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Boduch\Grid\Source;
4
5
use Boduch\Grid\Column;
6
use Boduch\Grid\Filters\FilterOperator;
7
use Boduch\Grid\Order;
8
use Illuminate\Support\Collection;
9
10
class CollectionSource implements SourceInterface
11
{
12
    /**
13
     * @var Collection
14
     */
15
    protected $collection;
16
17
    /**
18
     * @param Collection $source
19
     */
20
    public function __construct($source)
21
    {
22
        $this->collection = $source;
23
    }
24
25
    /**
26
     * @param Column[] $columns
27
     */
28
    public function applyFilters($columns)
29
    {
30
        foreach ($columns as $column) {
31
            /** @var \Boduch\Grid\Column $column */
32
            if ($column->isFilterable() && !$column->getFilter()->isEmpty()) {
33
                $this->filterValue(
34
                    $column->getFilter()->getName(),
35
                    $column->getFilter()->getInput(),
0 ignored issues
show
Bug introduced by
It seems like $column->getFilter()->getInput() targeting Boduch\Grid\Filters\FilterInterface::getInput() can also be of type array; however, Boduch\Grid\Source\CollectionSource::filterValue() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
36
                    $column->getFilter()->getOperator()
37
                );
38
            }
39
        }
40
    }
41
42
    /**
43
     * @param int $perPage
44
     * @param int $currentPage
45
     * @param Order $order
46
     * @return Collection
47
     */
48
    public function execute($perPage, $currentPage, Order $order)
49
    {
50
        if ($order->getDirection()) {
51
            $direction = $order->getDirection() == 'desc' ? 'sortByDesc' : 'sortBy';
52
            $this->collection = $this->collection->$direction($order->getColumn());
53
        }
54
55
        return $this
56
            ->collection
57
            ->forPage($currentPage, $perPage);
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function total()
64
    {
65
        return $this->collection->count();
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param string $value
71
     * @param string $operator
72
     */
73
    protected function filterValue($key, $value, $operator)
74
    {
75
        if ($operator == FilterOperator::OPERATOR_LIKE || $operator == FilterOperator::OPERATOR_ILIKE) {
76
            $this->collection = $this->collection->where($key, $value);
77
        } else {
78
            $this->collection = $this->collection->whereStrict($key, $value);
79
        }
80
    }
81
}
82