ApiPaginationComponent::isPaginatedApiRequest()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 3
eloc 5
nc 2
nop 0
crap 3
1
<?php
2
namespace BryanCrowe\ApiPagination\Controller\Component;
3
4
use Cake\Controller\Component;
5
use Cake\Event\Event;
6
7
/**
8
 * This is a simple component that injects pagination info into responses when
9
 * using CakePHP's PaginatorComponent alongside of CakePHP's JsonView or XmlView
10
 * classes.
11
 */
12
class ApiPaginationComponent extends Component
13
{
14
    /**
15
     * Default config.
16
     *
17
     * @var array
18
     */
19
    protected $_defaultConfig = [
20
        'key' => 'pagination',
21
        'aliases' => [],
22
        'visible' => []
23
    ];
24
25
    /**
26
     * Holds the paging information array from the request.
27
     *
28
     * @var array
29
     */
30
    protected $pagingInfo = [];
31
32
    /**
33
     * Injects the pagination info into the response if the current request is a
34
     * JSON or XML request with pagination.
35
     *
36
     * @param \Cake\Event\Event $event The Controller.beforeRender event.
37
     * @return void
38
     */
39 18
    public function beforeRender(Event $event)
40
    {
41 18
        if (!$this->isPaginatedApiRequest()) {
42 3
            return;
43
        }
44
45 15
        $subject = $event->getSubject();
46 15
        $this->pagingInfo = $this->request->getParam('paging')[$subject->getName()];
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated with message: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead.

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

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

Loading history...
47 15
        $config = $this->getConfig();
48
49 15
        if (!empty($config['aliases'])) {
50 6
            $this->setAliases();
51 6
        }
52
53 15
        if (!empty($config['visible'])) {
54 6
            $this->setVisibility();
55 6
        }
56
57 15
        $subject->set($config['key'], $this->pagingInfo);
58 15
        $subject->viewVars['_serialize'][] = $config['key'];
59 15
    }
60
61
    /**
62
     * Aliases the default pagination keys to the new keys that the user defines
63
     * in the config.
64
     *
65
     * @return void
66
     */
67 6
    protected function setAliases()
68
    {
69 6
        foreach ($this->getConfig('aliases') as $key => $value) {
70 6
            $this->pagingInfo[$value] = $this->pagingInfo[$key];
71 6
            unset($this->pagingInfo[$key]);
72 6
        }
73 6
    }
74
75
    /**
76
     * Removes any pagination keys that haven't been defined as visible in the
77
     * config.
78
     *
79
     * @return void
80
     */
81 6
    protected function setVisibility()
82
    {
83 6
        $visible = $this->getConfig('visible');
84 6
        foreach ($this->pagingInfo as $key => $value) {
85 6
            if (!in_array($key, $visible)) {
86 6
                unset($this->pagingInfo[$key]);
87 6
            }
88 6
        }
89 6
    }
90
91
    /**
92
     * Checks whether the current request is a JSON or XML request with
93
     * pagination.
94
     *
95
     * @return bool True if JSON or XML with paging, otherwise false.
96
     */
97 18
    protected function isPaginatedApiRequest()
98
    {
99 18
        if ($this->request->getParam('paging') &&
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated with message: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead.

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

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

Loading history...
100 15
            $this->request->is(['json', 'xml'])
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Controller\Component::$request has been deprecated with message: 3.4.0 Storing references to the request is deprecated. Use Component::getController() or callback $event->getSubject() to access the controller & request instead.

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

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

Loading history...
101 18
        ) {
102 15
            return true;
103
        }
104
105 3
        return false;
106
    }
107
}
108