Passed
Push — master ( df8e4b...6a1492 )
by Florian
02:33
created

ServicePaginatorTrait::addPagingParamToRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
nc 1
nop 1
dl 0
loc 5
c 0
b 0
f 0
cc 1
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1
<?php
2
/**
3
 * Copyright (c) Florian Krämer
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) Florian Krämer
10
 * @link          https://github.com/burzum/cakephp-service-layer
11
 * @since         1.0.0
12
 * @license       https://opensource.org/licenses/mit-license.php MIT License
13
 */
14
declare(strict_types = 1);
15
16
namespace Burzum\Cake\Service;
17
18
use Cake\Datasource\Paginator;
19
use Cake\Datasource\PaginatorInterface;
20
use Cake\Event\EventDispatcherTrait;
21
use Cake\Http\ServerRequest;
22
23
/**
24
 * ServicePaginatorTrait
25
 */
26
trait ServicePaginatorTrait
27
{
28
    use EventDispatcherTrait;
29
30
    /**
31
     * Paginator instance
32
     */
33
    protected $_paginator;
34
35
    /**
36
     * Default paginator class
37
     *
38
     * @var string
39
     */
40
    protected $_defaultPaginatorClass = Paginator::class;
41
42
    /**
43
     * Set paginator instance.
44
     *
45
     * @param \Cake\Datasource\PaginatorInterface $paginator Paginator instance.
46
     * @return self
47
     */
48 2
    public function setPaginator(PaginatorInterface $paginator)
49
    {
50 2
        $this->_paginator = $paginator;
51
52 2
        return $this;
53
    }
54
55
    /**
56
     * Get paginator instance.
57
     *
58
     * @return \Cake\Datasource\Paginator
59
     */
60 2
    public function getPaginator()
61
    {
62 2
        if (empty($this->_paginator)) {
63 2
            $class = $this->_defaultPaginatorClass;
64 2
            $this->setPaginator(new $class());
65
        }
66
67 2
        return $this->_paginator;
68
    }
69
70
    /**
71
     * Paginate
72
     *
73
     * @param \Cake\Datasource\RepositoryInterface|\Cake\Datasource\QueryInterface $object The table or query to paginate.
74
     * @param array $params Request params
75
     * @param array $settings The settings/configuration used for pagination.
76
     * @return \Cake\Datasource\ResultSetInterface Query results
77
     */
78 2
    public function paginate($object, array $params = [], array $settings = [])
79
    {
80 2
        $event = $this->dispatchEvent('Service.beforePaginate', compact(
81 2
            'object',
82 2
            'params',
83 2
            'settings'
84
        ));
85
86 2
        if ($event->isStopped()) {
87
            return $event->getResult();
88
        }
89
90 2
        $result = $this->getPaginator()->paginate($object, $params, $settings);
91 2
        $pagingParams = $this->getPaginator()->getPagingParams();
92
93 2
        $event = $this->dispatchEvent('Service.afterPaginate', compact(
94 2
            'object',
95 2
            'params',
96 2
            'settings',
97 2
            'result',
98 2
            'pagingParams'
99
        ));
100
101 2
        if ($event->getResult() !== null) {
102
            return $event->getResult();
103
        }
104
105 2
        return $result;
106
    }
107
108
    /**
109
     * Adds the paginator params to the request objects params
110
     *
111
     * @param \Cake\Http\ServerRequest $request Request object
112
     * @return void
113
     */
114 2
    public function addPagingParamToRequest(ServerRequest &$request)
115
    {
116 2
        $request->addParams([
0 ignored issues
show
Deprecated Code introduced by
The function Cake\Http\ServerRequest::addParams() has been deprecated: 3.6.0 ServerRequest::addParams() is deprecated. Use `withParam()` or `withAttribute('params')` instead. ( Ignorable by Annotation )

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

116
        /** @scrutinizer ignore-deprecated */ $request->addParams([

This function has been deprecated. The supplier of the function has supplied an explanatory message.

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

Loading history...
117 2
            'paging' => $this->getPaginator()->getPagingParams()
118 2
                + (array)$request->getParam('paging')
119
        ]);
120 2
    }
121
}
122