Completed
Branch master (fe6485)
by Nil
04:03
created

EloquentPageRepository::findAll()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 43
rs 8.439
cc 6
eloc 28
nc 17
nop 1
1
<?php
2
3
namespace NilPortugues\Foundation\Infrastructure\Model\Repository\EloquentMongoDB;
4
5
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Page;
6
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Pageable;
7
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\PageRepository;
8
use NilPortugues\Foundation\Domain\Model\Repository\Page as ResultPage;
9
10
class EloquentPageRepository extends BaseEloquentRepository implements PageRepository
11
{
12
    /**
13
     * Returns a Page of entities meeting the paging restriction provided in the Pageable object.
14
     *
15
     * @param Pageable $pageable
16
     *
17
     * @return Page
18
     */
19
    public function findAll(Pageable $pageable = null)
20
    {
21
        $model = self::$instance;
22
        $query = $model->query();
23
24
        if ($pageable) {
25
            $fields = $pageable->fields();
26
            $columns = (!$fields->isNull()) ? $fields->get() : ['*'];
27
28
            $filter = $pageable->filters();
29
            if (!$filter->isNull()) {
30
                EloquentFilter::filter($query, $filter);
0 ignored issues
show
Compatibility introduced by
$query of type object<Illuminate\Database\Eloquent\Builder> is not a sub-type of object<Jenssegers\Mongodb\Eloquent\Builder>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Builder to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
31
            }
32
33
            $sort = $pageable->sortings();
34
            if (!$sort->isNull()) {
35
                EloquentSorter::sort($query, $sort);
0 ignored issues
show
Compatibility introduced by
$query of type object<Illuminate\Database\Eloquent\Builder> is not a sub-type of object<Jenssegers\Mongodb\Eloquent\Builder>. It seems like you assume a child class of the class Illuminate\Database\Eloquent\Builder to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
36
            }
37
38
            $model = $model
39
                ->take($pageable->pageSize())
40
                ->offset($pageable->pageSize() * ($pageable->pageNumber() - 1));
41
42
            if (count($distinctFields = $pageable->distinctFields()->get()) > 0) {
43
                $model = $model->distinct();
44
                $columns = $distinctFields;
45
            }
46
47
            return new ResultPage(
48
                $model->get($columns)->toArray(),
49
                $model->count(),
50
                $pageable->pageNumber(),
51
                ceil($query->paginate()->total() / $pageable->pageSize())
52
            );
53
        }
54
55
        return new ResultPage(
56
            $query->paginate($query->paginate()->total(), ['*'], 'page', 1)->items(),
57
            $query->paginate()->total(),
58
            1,
59
            1
60
        );
61
    }
62
}
63