Completed
Pull Request — master (#118)
by
unknown
05:41
created

CollectionDataProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Nayjest\Grids;
3
4
use Illuminate\Database\Eloquent\Builder;
5
use Event;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\Collection;
8
9
class CollectionDataProvider extends DataProvider
10
{
11
    protected $collection;
12
13
    protected $paginator;
14
15
    /** @var  $iterator \ArrayIterator */
16
    protected $iterator;
17
18
    /**
19
     * Constructor.
20
     *
21
     * @param Builder $src
22
     */
23
    public function __construct(Collection $src)
24
    {
25
        parent::__construct($src);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function reset()
32
    {
33
        $this->getIterator()->rewind();
34
        return $this;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getCollection()
41
    {
42
        return $this->src;
43
    }
44
45
    public function getPaginator()
46
    {
47
        if (!$this->paginator) {
48
            $this->paginator = new \Illuminate\Pagination\Paginator($this->src, $this->src->count());
49
        }
50
        return $this->paginator;
51
    }
52
53
    /**
54
     * @return \Illuminate\Pagination\Factory
55
     */
56
    public function getPaginationFactory()
57
    {
58
        return $this->getPaginator();//$this->src->getQuery()->getConnection()->getPaginator();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getPaginator(); (Illuminate\Pagination\Paginator) is incompatible with the return type declared by the abstract method Nayjest\Grids\DataProvider::getPaginationFactory of type Illuminate\Pagination\Factory.

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...
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59
    }
60
61
    protected function getIterator()
62
    {
63
        if (!$this->iterator) {
64
            $this->iterator = $this->getCollection()->getIterator();
65
        }
66
        return $this->iterator;
67
    }
68
69
    /**
70
     * @return Builder
71
     */
72
    public function getBuilder()
73
    {
74
        return $this->src;
75
    }
76
77 View Code Duplication
    public function getRow()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        if (!$this->iterator) {
80
            $this->getIterator();
81
        }
82
83
        if ($this->index < $this->count()) {
84
            $this->index++;
85
            $item = $this->iterator->current();
86
            $this->iterator->next();
87
            $row = new EloquentDataRow($item, $this->getRowId());
0 ignored issues
show
Deprecated Code introduced by
The class Nayjest\Grids\EloquentDataRow has been deprecated with message: Class EloquentDataRow

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
88
            Event::fire(self::EVENT_FETCH_ROW, [$row, $this]);
89
            return $row;
90
        } else {
91
            return null;
92
        }
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function count()
99
    {
100
        return $this->getCollection()->count();
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function orderBy($fieldName, $direction)
107
    {
108
        if ($direction === Grid::SORT_ASC) {
109
            $this->src->sortBy($fieldName);
110
        } else {
111
            $this->src->sortByDesc($fieldName);
112
        }
113
        
114
        return $this;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function filter($fieldName, $operator, $value)
121
    {
122
        /*
123
            Currently no filter support on CollectionDataProvider
124
        */
125
        return $this;
126
    }
127
}
128