Test Setup Failed
Pull Request — master (#1)
by Florian
10:46
created

CakeOrmPaginator::paginate()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 10
nop 2
dl 0
loc 28
rs 9.3554
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 Phauthentic\Pagination\PaginationParamsInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
21
/**
22
 * Pagination To Cake Orm Mapper
23
 */
24
class CakeOrmPaginator implements PaginatorInterface
25
{
26
    /**
27
     * Maps the params to the repository
28
     *
29
     * @param \Phauthentic\Pagination\PaginationParamsInterface $paginationParams Pagination params
30
     * @param mixed $repository
31
     * @return mixed
32
     */
33
    public function paginate($object, PaginationParamsInterface $paginationParams)
34
    {
35
        /** @var \Cake\Database\Query $query */
36
        $query = null;
37
        if ($object instanceof QueryInterface) {
38
            $query = $object;
39
            $object = $query->getRepository();
40
        }
41
42
        $count = $query->count();
0 ignored issues
show
introduced by
The method count() does not exist on Cake\Database\Query. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

42
        /** @scrutinizer ignore-call */ 
43
        $count = $query->count();
Loading history...
43
        $paginationParams->setCount($count);
44
45
        $sortBy = $paginationParams->getSortBy();
46
        if ($sortBy !== null) {
47
            if (strpos($sortBy, '.') === false) {
48
                $sortBy = $object->aliasField($sortBy);
49
            }
50
            if ($paginationParams->getDirection() === 'desc') {
51
                $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

51
                $query->/** @scrutinizer ignore-call */ 
52
                        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...
52
            } else {
53
                $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

53
                $query->/** @scrutinizer ignore-call */ 
54
                        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...
54
            }
55
        }
56
57
        return $query
58
            ->limit($paginationParams->getLimit())
59
            ->offSet($paginationParams->getOffSet())
60
            ->all();
0 ignored issues
show
introduced by
The method all() does not exist on Cake\Database\Query. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

60
            ->/** @scrutinizer ignore-call */ all();
Loading history...
61
    }
62
}
63