Completed
Pull Request — master (#1)
by
unknown
06:08
created

ApiPaginationComponent::implementedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 3
     * @return void
38
     */
39 3
    public function beforeRender(Event $event)
40
    {
41
        if (!$this->isPaginatedApiRequest()) {
42
            return;
43
        }
44
45
        $subject = $event->subject();
46
        $this->pagingInfo = $this->request->params['paging'][$subject->name];
47
        $config = $this->config();
48
49 18
        if (!empty($config['aliases'])) {
50
            $this->setAliases();
51 18
        }
52 3
53
        if (!empty($config['visible'])) {
54
            $this->setVisibility();
55 15
        }
56 15
57 15
        $subject->set($config['key'], $this->pagingInfo);
58
        $subject->viewVars['_serialize'][] = $config['key'];
59 15
    }
60 6
61 6
    /**
62
     * Aliases the default pagination keys to the new keys that the user defines
63 15
     * in the config.
64 6
     *
65 6
     * @return void
66
     */
67 15
    protected function setAliases()
68 15
    {
69 15
        foreach ($this->config('aliases') as $key => $value) {
70
            $this->pagingInfo[$value] = $this->pagingInfo[$key];
71
            unset($this->pagingInfo[$key]);
72
        }
73
    }
74
75
    /**
76
     * Removes any pagination keys that haven't been defined as visible in the
77 6
     * config.
78
     *
79 6
     * @return void
80 6
     */
81 6
    protected function setVisibility()
82 6
    {
83 6
        $visible = $this->config('visible');
84
        foreach ($this->pagingInfo as $key => $value) {
85
            if (!in_array($key, $visible)) {
86
                unset($this->pagingInfo[$key]);
87
            }
88
        }
89
    }
90
91 6
    /**
92
     * Checks whether the current request is a JSON or XML request with
93 6
     * pagination.
94 6
     *
95 6
     * @return bool True if JSON or XML with paging, otherwise false.
96 6
     */
97 6
    protected function isPaginatedApiRequest()
98 6
    {
99 6
        if (isset($this->request->params['paging']) &&
100
            $this->request->is(['json', 'xml'])
101
        ) {
102
            return true;
103
        }
104
105
        return false;
106
    }
107
}
108