CakeOrmPaginator::paginate()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 35
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.105

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 12
nop 2
dl 0
loc 35
ccs 18
cts 21
cp 0.8571
crap 6.105
rs 8.9297
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright (c) Phauthentic (https://github.com/Phauthentic)
4
 *
5
 * Licensed under The MIT License
6
 * For full copyright and license information, please see the LICENSE.txt
7
 * Redistributions of files must retain the above copyright notice.
8
 *
9
 * @copyright     Copyright (c) Phauthentic (https://github.com/Phauthentic)
10
 * @link          https://github.com/Phauthentic
11
 * @license       https://opensource.org/licenses/mit-license.php MIT License
12
 */
13
declare(strict_types = 1);
14
15
namespace Phauthentic\Pagination\Paginator;
16
17
use Cake\Datasource\QueryInterface;
18
use InvalidArgumentException;
19
use Phauthentic\Pagination\PaginationParamsInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
22
/**
23
 * Pagination To Cake Orm Mapper
24
 */
25
class CakeOrmPaginator implements PaginatorInterface
26
{
27
    /**
28
     * Maps the params to the repository
29
     *
30
     * @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params
31
     * @param mixed $repository
32
     * @return mixed
33
     */
34 1
    public function paginate($repository, PaginationParamsInterface $paginationParams)
35
    {
36
        /** @var \Cake\Database\Query $query */
37 1
        $query = null;
38 1
        if ($repository instanceof QueryInterface) {
39
            $query = $repository;
40
            $object = $query->getRepository();
41
        } else {
42 1
            $query = $repository->find();
43 1
            $object = $repository;
44
        }
45
46 1
        if (!$query instanceof QueryInterface) {
47
            throw new InvalidArgumentException();
48
        }
49
50 1
        $count = $query->count();
51 1
        $paginationParams->setCount($count);
52
53 1
        $sortBy = $paginationParams->getSortBy();
54 1
        if ($sortBy !== null) {
55 1
            if (strpos($sortBy, '.') === false) {
56 1
                $sortBy = $object->aliasField($sortBy);
57
            }
58 1
            if ($paginationParams->getDirection() === 'desc') {
59 1
                $query->orderDesc($sortBy);
0 ignored issues
show
Bug introduced by
The method orderDesc() does not exist on Cake\Datasource\QueryInterface. Did you maybe mean order()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
                $query->/** @scrutinizer ignore-call */ 
60
                        orderDesc($sortBy);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
            } else {
61 1
                $query->orderAsc($sortBy);
0 ignored issues
show
Bug introduced by
The method orderAsc() does not exist on Cake\Datasource\QueryInterface. Did you maybe mean order()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
                $query->/** @scrutinizer ignore-call */ 
62
                        orderAsc($sortBy);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
            }
63
        }
64
65
        return $query
66 1
            ->limit($paginationParams->getLimit())
67 1
            ->offSet($paginationParams->getOffSet())
68 1
            ->all();
69
    }
70
}
71