Loadmore::loadmore()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
namespace Mohamedsabil83\LaravelLoadmore;
4
5
use Illuminate\Pagination\Paginator;
6
use Illuminate\Pagination\LengthAwarePaginator;
7
8
trait Loadmore
9
{
10
    /**
11
     * Prepare a loadmore pagination.
12
     *
13
     * @param integer $initial
14
     * @param integer $loadMore
15
     * @return \Illuminate\Pagination\LengthAwarePaginator
16
     */
17
    public function loadmore($initial = 4, $loadMore = 16)
18
    {
19
        $page = (int) Paginator::resolveCurrentPage();
20
        $perPage = ($page == 1) ? $initial : $loadMore;
21
        $skip = ($page == 1) ? 0 : ($initial + ($loadMore * ($page - 2)));
22
        $items = $this->skip($skip)->take($perPage)->get();
0 ignored issues
show
Bug introduced by
It seems like skip() 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...
23
        $total = $this->count() + $loadMore - $initial;
0 ignored issues
show
Bug introduced by
It seems like count() 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...
24
25
        return new LengthAwarePaginator(
26
            $items,
27
            $total,
28
            $loadMore,
29
            Paginator::resolveCurrentPage(),
30
            ['path' => Paginator::resolveCurrentPath()]
31
        );
32
    }
33
34
    /**
35
     * Scope a query to match loadmore style.
36
     *
37
     * @param \Illuminate\Database\Eloquent\Builder $query
38
     * @param integer $initial
39
     * @param integer $loadMore
40
     * @return \Illuminate\Database\Eloquent\Builder
41
     */
42
    public function scopeLoadmore($query, $initial = 4, $loadMore = 16)
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
    {
44
        return $this->loadmore($initial, $loadMore);
45
    }
46
}
47