Completed
Push — master ( 3f628c...0e6b45 )
by Bryan
02:53
created

ApiPaginationComponent::beforeRender()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.2
cc 4
eloc 11
nc 5
nop 1
crap 4
1
<?php
2
namespace BryanCrowe\ApiPagination\Controller\Component;
3
4
use Cake\Controller\Component;
5
use Cake\Controller\Controller;
6
use Cake\Event\Event;
7
8
/**
9
 * This is a simple component that injects pagination info into responses when
10
 * using CakePHP's PaginatorComponent alongside of CakePHP's JsonView or XmlView
11
 * classes.
12
 */
13
class ApiPaginationComponent extends Component
14
{
15
    /**
16
     * Default config.
17
     *
18
     * @var array
19
     */
20
    protected $_defaultConfig = [
21
        'key' => 'pagination',
22
        'aliases' => [],
23
        'visible' => []
24
    ];
25
26
    /**
27
     * Holds the paging information.
28
     *
29
     * @var array
30
     */
31
    protected $pagingInfo = [];
32
33
    /**
34
     * {@inheritDoc}
35
     *
36
     * @return array
37
     */
38 3
    public function implementedEvents()
39
    {
40 3
        return [];
41
    }
42
43
    /**
44
     * Injects the pagination info into the response if the current request is a
45
     * JSON or XML request with pagination.
46
     *
47
     * @param Event $event The Controller.beforeRender event.
48
     * @return void
49
     */
50 18
    public function beforeRender(Event $event)
51
    {
52 18
        if (!$this->isPaginatedApiRequest()) {
53 3
            return;
54
        }
55
56 15
        $this->pagingInfo = $this->request->params['paging'][$event->subject()->name];
57 15
        $config = $this->config();
58
59 15
        if (!empty($config['aliases'])) {
60 6
            $this->setAliases();
61 6
        }
62
63 15
        if (!empty($config['visible'])) {
64 6
            $this->setVisibility();
65 6
        }
66
67 15
        $event->subject()->set($config['key'], $this->pagingInfo);
68 15
        $event->subject()->viewVars['_serialize'][] = $config['key'];
69 15
    }
70
71
    /**
72
     * Aliases the default pagination keys to the new keys that the user defines
73
     * in the config.
74
     *
75
     * @return void
76
     */
77 6
    protected function setAliases()
78
    {
79 6
        foreach ($this->config('aliases') as $key => $value) {
80 6
            $this->pagingInfo[$value] = $this->pagingInfo[$key];
81 6
            unset($this->pagingInfo[$key]);
82 6
        }
83 6
    }
84
85
    /**
86
     * Removes any pagination keys that haven't been defined as visible in the
87
     * config.
88
     *
89
     * @return void
90
     */
91 6
    protected function setVisibility()
92
    {
93 6
        $visible = $this->config('visible');
94 6
        foreach ($this->pagingInfo as $key => $value) {
95 6
            if (!in_array($key, $visible)) {
96 6
                unset($this->pagingInfo[$key]);
97 6
            }
98 6
        }
99 6
    }
100
101
    /**
102
     * Checks whether the current request is a JSON or XML request with
103
     * pagination.
104
     *
105
     * @return bool True if JSON or XML with paging, otherwise false.
106
     */
107 18
    protected function isPaginatedApiRequest()
108
    {
109 18
        if (isset($this->request->params['paging']) &&
110 15
            $this->request->is(['json', 'xml'])
111 18
        ) {
112 15
            return true;
113
        }
114
115 3
        return false;
116
    }
117
}
118