Passed
Push — master ( 964e1c...49cc4f )
by Florian
01:49 queued 12s
created

ServicePaginatorTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 93
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setPaginator() 0 5 1
A getPaginator() 0 8 2
A paginate() 0 28 3
A addPagingParamToRequest() 0 3 1
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\CakeServiceLayer\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
     * @var \Cake\Datasource\PaginatorInterface
34
     */
35
    protected $_paginator;
36
37
    /**
38
     * Default paginator class
39
     *
40
     * @var string
41
     */
42
    protected $_defaultPaginatorClass = Paginator::class;
43
44
    /**
45
     * Set paginator instance.
46
     *
47
     * @param \Cake\Datasource\PaginatorInterface $paginator Paginator instance.
48
     * @return static
49
     */
50 2
    public function setPaginator(PaginatorInterface $paginator)
51
    {
52 2
        $this->_paginator = $paginator;
53
54 2
        return $this;
55
    }
56
57
    /**
58
     * Get paginator instance.
59
     *
60
     * @return \Cake\Datasource\PaginatorInterface
61
     */
62 2
    public function getPaginator()
63
    {
64 2
        if (empty($this->_paginator)) {
65 2
            $class = $this->_defaultPaginatorClass;
66 2
            $this->setPaginator(new $class());
67
        }
68
69 2
        return $this->_paginator;
70
    }
71
72
    /**
73
     * Paginate
74
     *
75
     * @param \Cake\Datasource\RepositoryInterface|\Cake\Datasource\QueryInterface $object The table or query to paginate.
76
     * @param array $params Request params
77
     * @param array $settings The settings/configuration used for pagination.
78
     * @return \Cake\Datasource\ResultSetInterface Query results
79
     */
80 2
    public function paginate($object, array $params = [], array $settings = [])
81
    {
82 2
        $event = $this->dispatchEvent('Service.beforePaginate', compact(
83 2
            'object',
84 2
            'params',
85 2
            'settings'
86
        ));
87
88 2
        if ($event->isStopped()) {
89
            return $event->getResult();
90
        }
91
92 2
        $result = $this->getPaginator()->paginate($object, $params, $settings);
93 2
        $pagingParams = $this->getPaginator()->getPagingParams();
94
95 2
        $event = $this->dispatchEvent('Service.afterPaginate', compact(
96 2
            'object',
97 2
            'params',
98 2
            'settings',
99 2
            'result',
100 2
            'pagingParams'
101
        ));
102
103 2
        if ($event->getResult() !== null) {
104
            return $event->getResult();
105
        }
106
107 2
        return $result;
108
    }
109
110
    /**
111
     * Adds the paginator params to the request objects params
112
     *
113
     * @param \Cake\Http\ServerRequest $request Request object
114
     * @return \Cake\Http\ServerRequest
115
     */
116 2
    public function addPagingParamToRequest(ServerRequest $request): ServerRequest
117
    {
118 2
        return $request->withParam('paging', $this->getPaginator()->getPagingParams() + (array)$request->getParam('paging'));
119
    }
120
}
121