Test Failed
Pull Request — master (#19)
by Florian
02:37
created

ServicePaginatorTrait::addPagingParamToRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 \Cake\Http\ServerRequest
113
     */
114 2
    public function addPagingParamToRequest(ServerRequest $request): ServerRequest
115
    {
116 2
        return $request->withParam('paging', $this->getPaginator()->getPagingParams() + (array)$request->getParam('paging'));
117
    }
118
}
119