Completed
Push — master ( 0e6b45...c8e36f )
by Bryan
02:19
created

ApiPaginationComponent::beforeRender()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

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