PaginateExtension   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 104
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A implementedEvents() 0 7 1
A findEntities() 0 12 2
A _page() 0 10 3
A _limit() 0 16 5
A afterFind() 0 15 1
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action\Extension;
13
14
use CakeDC\Api\Service\Action\Action;
15
use Cake\Event\Event;
16
use Cake\Event\EventListenerInterface;
17
use Cake\ORM\Entity;
18
19
/**
20
 * Class PaginateExtension
21
 *
22
 * @package CakeDC\Api\Service\Action\Extension
23
 */
24
class PaginateExtension extends Extension implements EventListenerInterface
25
{
26
27
    protected $_defaultConfig = [
28
        'defaultLimit' => 20,
29
        'limitField' => 'limit',
30
        'pageField' => 'page',
31
    ];
32
33
    /**
34
     * Returns a list of events this object is implementing. When the class is registered
35
     * in an event manager, each individual method will be associated with the respective event.
36
     *
37
     * @return array
38
     */
39 27
    public function implementedEvents()
40
    {
41
        return [
42 27
            'Action.Crud.onFindEntities' => 'findEntities',
43 27
            'Action.Crud.afterFindEntities' => 'afterFind',
44 27
        ];
45
    }
46
47
    /**
48
     * Find entities
49
     *
50
     * @param Event $event An Event instance
51
     * @return Entity
52
     */
53 20
    public function findEntities(Event $event)
54
    {
55 20
        $action = $event->getSubject();
56 20
        $query = $event->getData('query');
57 20
        if ($event->result) {
58 13
            $query = $event->result;
59 13
        }
60 20
        $query->limit($this->_limit($action));
0 ignored issues
show
Documentation introduced by
$action is of type object|null, but the function expects a object<CakeDC\Api\Service\Action\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
61 20
        $query->page($this->_page($action));
0 ignored issues
show
Documentation introduced by
$action is of type object|null, but the function expects a object<CakeDC\Api\Service\Action\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
62
63 20
        return $query;
64
    }
65
66
    /**
67
     * Returns current page.
68
     *
69
     * @param Action $action An Action instance
70
     * @return int
71
     */
72 20
    protected function _page(Action $action)
73
    {
74 20
        $data = $action->getData();
75 20
        $pageField = $this->getConfig('pageField');
76 20
        if (!empty($data[$pageField]) && is_numeric($data[$pageField])) {
77 1
            return (int)$data[$pageField];
78
        } else {
79 19
            return 1;
80
        }
81
    }
82
83
    /**
84
     * Returns current limit
85
     *
86
     * @param Action $action An Action instance
87
     * @return mixed
88
     */
89 20
    protected function _limit(Action $action)
90
    {
91 20
        $data = $action->getData();
92 20
        $limitField = $this->getConfig('limitField');
93 20
        $maxLimit = $action->getConfig($limitField);
94 20
        if (empty($maxLimit)) {
95 20
            $maxLimit = $this->getConfig('defaultLimit');
96 20
        }
97 20
        if (!empty($limitField) && !empty($data[$limitField]) && is_numeric($data[$limitField])) {
98 16
            $limit = min((int)$data[$limitField], $maxLimit);
99
100 16
            return $limit;
101
        } else {
102 4
            return $maxLimit;
103
        }
104
    }
105
106
    /**
107
     * after find entities
108
     *
109
     * @param Event $event An Event instance
110
     * @return void
111
     */
112 20
    public function afterFind(Event $event)
113
    {
114 20
        $action = $event->getSubject();
115 20
        $query = $event->getData('query');
116 20
        $result = $action->getService()->getResult();
117 20
        $count = $query->count();
118 20
        $limit = $this->_limit($action);
0 ignored issues
show
Documentation introduced by
$action is of type object|null, but the function expects a object<CakeDC\Api\Service\Action\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
119
        $pagination = [
120 20
            'page' => $this->_page($action),
0 ignored issues
show
Documentation introduced by
$action is of type object|null, but the function expects a object<CakeDC\Api\Service\Action\Action>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121 20
            'limit' => $limit,
122 20
            'pages' => ceil($count / $limit),
123
            'count' => $count
124 20
        ];
125 20
        $result->appendPayload('pagination', $pagination);
126 20
    }
127
}
128