Skip to content

Commit 218f545

Browse files
committed
Separate Http Base Action and REST Classes
1 parent 4a0edc0 commit 218f545

File tree

6 files changed

+104
-60
lines changed

6 files changed

+104
-60
lines changed

src/Http/Action/BaseHttpAction.php

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,27 @@ abstract class BaseHttpAction implements RequestHandlerInterface
7070

7171
/**
7272
* The pagination limit
73-
* @var int|null
73+
* @var int
7474
*/
75-
protected ?int $limit = null;
75+
protected int $limit;
7676

7777
/**
78-
* The pagination current page
79-
* @var int|null
78+
* The pagination max limit to fetch "all" record
79+
* @var int
80+
*/
81+
protected int $maxLimit = 0;
82+
83+
/**
84+
* The pagination max per page
85+
* @var int
8086
*/
81-
protected ?int $page = null;
87+
protected int $maxPerPage = 0;
8288

8389
/**
84-
* Whether to query all list without pagination
85-
* @var bool
90+
* The pagination current page
91+
* @var int
8692
*/
87-
protected bool $all = false;
93+
protected int $page = 1;
8894

8995
/**
9096
* The pagination instance
@@ -269,37 +275,35 @@ protected function setFilters(): void
269275
protected function setPagination(): void
270276
{
271277
$param = $this->param;
272-
if ($param->get('all', null)) {
273-
$this->all = true;
274-
return;
275-
}
276-
277-
$limit = $param->get('limit', null);
278-
if ($limit !== null) {
279-
$this->limit = (int) $limit;
280-
}
281-
282-
$maxLimit = $this->config->get('pagination.max_limit', 1000);
283-
if ($this->limit !== null && $this->limit > $maxLimit) {
284-
$this->limit = $maxLimit;
285-
}
278+
$all = boolval($param->get('all', null));
279+
$perPage = $this->config->get('pagination.item_per_page', 20);
286280

281+
$this->limit = (int) $param->get('limit', $perPage);
287282
$page = $param->get('page', null);
288283
if ($page) {
289284
$this->page = (int) $page;
290285
}
291286

292-
if ($limit > 0 || $page > 0) {
293-
$this->all = false;
287+
if ($this->maxLimit === 0) {
288+
$this->maxLimit = $this->config->get('pagination.max_limit', 1000);
294289
}
295290

296-
if ($this->limit > 0) {
297-
$this->pagination->setItemsPerPage($this->limit);
291+
if ($this->maxPerPage === 0) {
292+
$this->maxPerPage = $this->config->get('pagination.max_per_page', 100);
298293
}
299294

300-
$currentPage = $this->page ?? 1;
295+
// Handle fetching all records
296+
if ($all) {
297+
$this->page = 1;
298+
$this->limit = $this->maxLimit;
299+
} else {
300+
if ($this->limit > $this->maxPerPage) {
301+
$this->limit = $this->maxPerPage;
302+
}
303+
}
301304

302-
$this->pagination->setCurrentPage($currentPage);
305+
$this->pagination->setItemsPerPage($this->limit);
306+
$this->pagination->setCurrentPage($this->page);
303307
}
304308

305309
/**

src/Http/Action/RestBaseAction.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,22 +146,20 @@ protected function handleRestPagination(
146146
string|array $sortFields = 'name',
147147
string $sortDir = 'ASC'
148148
): void {
149-
if ($this->all === false) {
150-
$totalItems = $repository->filters($this->filters)
151-
->query()
152-
->count('id');
149+
$totalItems = $repository->filters($this->filters)
150+
->query()
151+
->count('id');
153152

154-
$currentPage = (int) $this->param->get('page', 1);
153+
$currentPage = (int) $this->param->get('page', 1);
155154

156-
$this->pagination->setTotalItems($totalItems)
157-
->setCurrentPage($currentPage);
155+
$this->pagination->setTotalItems($totalItems)
156+
->setCurrentPage($currentPage);
158157

159-
$limit = $this->pagination->getItemsPerPage();
160-
$offset = $this->pagination->getOffset();
158+
$limit = $this->pagination->getItemsPerPage();
159+
$offset = $this->pagination->getOffset();
161160

162-
$query = $query->limit($limit)
163-
->offset($offset);
164-
}
161+
$query->limit($limit)
162+
->offset($offset);
165163

166164
if (count($this->sorts) > 0) {
167165
foreach ($this->sorts as $column => $order) {

tests/Http/Action/BaseActionTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ protected function handle(bool $bypassPagination = false): void
122122
]);
123123
$config = $this->getMockInstanceMap(Config::class, [
124124
'get' => [
125+
['pagination.max_per_page', 100, 100],
125126
['pagination.max_limit', 1000, 1000],
126127
],
127128
]);
@@ -144,20 +145,15 @@ protected function handle(bool $bypassPagination = false): void
144145
$this->assertEquals(200, $resp->getStatusCode());
145146

146147
$this->assertEquals(
147-
$bypassPagination ? null : 200,
148+
$bypassPagination ? 1000 : 100,
148149
$this->getPropertyValue(BaseAction::class, $o, 'limit')
149150
);
150151
$this->assertEquals('foo_view', $this->getPropertyValue(BaseAction::class, $o, 'viewName'));
151152
$this->assertEquals(
152-
$bypassPagination ? null : 1,
153+
$bypassPagination ? 1 : 1,
153154
$this->getPropertyValue(BaseAction::class, $o, 'page')
154155
);
155156

156-
$this->assertEquals(
157-
$bypassPagination ? true : false,
158-
$this->getPropertyValue(BaseAction::class, $o, 'all')
159-
);
160-
161157
$this->assertEquals(
162158
['permissions' => [5,7], 'status' => 'Y', 'multi' => [5, 7]],
163159
$this->getPropertyValue(BaseAction::class, $o, 'filters')
@@ -181,7 +177,15 @@ protected function redirectBackToOrigin(string $originId = '0', ?string $originR
181177
]]
182178
],
183179
]);
180+
$config = $this->getMockInstanceMap(Config::class, [
181+
'get' => [
182+
['pagination.max_per_page', 100, 100],
183+
['pagination.max_limit', 1000, 1000],
184+
],
185+
]);
186+
184187
$this->setClassCreateObjectMaps(ActionHelper::class, [
188+
'config' => $config,
185189
'logger' => $logger,
186190
'routeHelper' => $routeHelper,
187191
]);

tests/Http/Action/BaseConfigurationActionTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Platine\Test\Framework\Http\Action;
66

7+
use Platine\Config\Config;
78
use Platine\Dev\PlatineTestCase;
89
use Platine\Framework\Config\AppDatabaseConfig;
910
use Platine\Framework\Config\DatabaseConfigLoader;
@@ -64,7 +65,15 @@ public function testRespondRequestMethodGet(): void
6465
['GET'],
6566
],
6667
]);
68+
$config = $this->getMockInstanceMap(Config::class, [
69+
'get' => [
70+
['pagination.max_per_page', 100, 100],
71+
['pagination.max_limit', 1000, 1000],
72+
],
73+
]);
74+
6775
$this->setClassCreateObjectMaps(ActionHelper::class, [
76+
'config' => $config,
6877
'logger' => $logger,
6978
]);
7079
$actionHelper = $this->createObject(ActionHelper::class);
@@ -77,8 +86,7 @@ public function testRespondRequestMethodGet(): void
7786
$this->assertEquals(200, $resp->getStatusCode());
7887

7988
$this->assertEquals('', $this->getPropertyValue(BaseAction::class, $o, 'viewName'));
80-
$this->assertEquals(null, $this->getPropertyValue(BaseAction::class, $o, 'page'));
81-
$this->assertFalse($this->getPropertyValue(BaseAction::class, $o, 'all'));
89+
$this->assertEquals(1, $this->getPropertyValue(BaseAction::class, $o, 'page'));
8290
}
8391

8492
public function testRespondSaveFormValidationFailed(): void
@@ -100,7 +108,14 @@ public function testRespondSaveFormValidationFailed(): void
100108
['POST'],
101109
],
102110
]);
111+
$config = $this->getMockInstanceMap(Config::class, [
112+
'get' => [
113+
['pagination.max_per_page', 100, 100],
114+
['pagination.max_limit', 1000, 1000],
115+
],
116+
]);
103117
$this->setClassCreateObjectMaps(ActionHelper::class, [
118+
'config' => $config,
104119
'logger' => $logger,
105120
'context' => $viewContext,
106121
]);
@@ -114,8 +129,7 @@ public function testRespondSaveFormValidationFailed(): void
114129
$this->assertInstanceOf(TemplateResponse::class, $resp);
115130
$this->assertEquals(200, $resp->getStatusCode());
116131
$this->assertEquals('', $this->getPropertyValue(BaseAction::class, $o, 'viewName'));
117-
$this->assertEquals(null, $this->getPropertyValue(BaseAction::class, $o, 'page'));
118-
$this->assertFalse($this->getPropertyValue(BaseAction::class, $o, 'all'));
132+
$this->assertEquals(1, $this->getPropertyValue(BaseAction::class, $o, 'page'));
119133
}
120134

121135
public function testRespondSaveSuccess(): void
@@ -151,7 +165,14 @@ public function testRespondSaveSuccess(): void
151165
[['name' => 'foo', 'status' => 'bar']],
152166
],
153167
]);
168+
$config = $this->getMockInstanceMap(Config::class, [
169+
'get' => [
170+
['pagination.max_per_page', 100, 100],
171+
['pagination.max_limit', 1000, 1000],
172+
],
173+
]);
154174
$this->setClassCreateObjectMaps(ActionHelper::class, [
175+
'config' => $config,
155176
'logger' => $logger,
156177
'context' => $viewContext,
157178
]);
@@ -168,7 +189,6 @@ public function testRespondSaveSuccess(): void
168189
$this->assertInstanceOf(RedirectResponse::class, $resp);
169190
$this->assertEquals(302, $resp->getStatusCode());
170191
$this->assertEquals('', $this->getPropertyValue(BaseAction::class, $o, 'viewName'));
171-
$this->assertEquals(null, $this->getPropertyValue(BaseAction::class, $o, 'page'));
172-
$this->assertFalse($this->getPropertyValue(BaseAction::class, $o, 'all'));
192+
$this->assertEquals(1, $this->getPropertyValue(BaseAction::class, $o, 'page'));
173193
}
174194
}

tests/Http/Action/BaseResourceActionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Platine\Test\Framework\Http\Action;
66

7+
use Platine\Config\Config;
78
use Platine\Dev\PlatineTestCase;
89
use Platine\Framework\Helper\ActionHelper;
910
use Platine\Framework\Http\Exception\HttpNotFoundException;
@@ -22,7 +23,14 @@ public function testResponseNotFound(): void
2223
$request = $this->getMockInstance(ServerRequest::class, [
2324
'getAttribute' => $route,
2425
]);
26+
$config = $this->getMockInstanceMap(Config::class, [
27+
'get' => [
28+
['pagination.max_per_page', 100, 100],
29+
['pagination.max_limit', 1000, 1000],
30+
],
31+
]);
2532
$this->setClassCreateObjectMaps(ActionHelper::class, [
33+
'config' => $config,
2634
'logger' => $logger,
2735
]);
2836
$actionHelper = $this->createObject(ActionHelper::class);
@@ -40,7 +48,14 @@ public function testResponseSucess(): void
4048
$request = $this->getMockInstance(ServerRequest::class, [
4149
'getAttribute' => $route,
4250
]);
51+
$config = $this->getMockInstanceMap(Config::class, [
52+
'get' => [
53+
['pagination.max_per_page', 100, 100],
54+
['pagination.max_limit', 1000, 1000],
55+
],
56+
]);
4357
$this->setClassCreateObjectMaps(ActionHelper::class, [
58+
'config' => $config,
4459
'logger' => $logger,
4560
]);
4661
$actionHelper = $this->createObject(ActionHelper::class);

tests/Http/Action/RestBaseActionTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ private function handleRestPagination(bool $defaultSort = false): void
6969
'getTotalItems' => 16,
7070
'getInfo' => ['page' => 1],
7171
]);
72+
$config = $this->getMockInstanceMap(Config::class, [
73+
'get' => [
74+
['pagination.max_per_page', 100, 100],
75+
['pagination.max_limit', 1000, 1000],
76+
],
77+
]);
7278
$this->setClassCreateObjectMaps(ActionHelper::class, [
79+
'config' => $config,
7380
'logger' => $logger,
7481
'pagination' => $pagination,
7582
]);
@@ -368,6 +375,7 @@ protected function handle(bool $bypassPagination = false): void
368375
]);
369376
$config = $this->getMockInstanceMap(Config::class, [
370377
'get' => [
378+
['pagination.max_per_page', 100, 100],
371379
['pagination.max_limit', 1000, 1000],
372380
],
373381
]);
@@ -385,19 +393,14 @@ protected function handle(bool $bypassPagination = false): void
385393
$this->assertEquals(200, $resp->getStatusCode());
386394

387395
$this->assertEquals(
388-
$bypassPagination ? null : 101,
396+
$bypassPagination ? 1000 : 100,
389397
$this->getPropertyValue(RestBaseAction::class, $o, 'limit')
390398
);
391399
$this->assertEquals(
392-
$bypassPagination ? null : 1,
400+
$bypassPagination ? 1 : 1,
393401
$this->getPropertyValue(RestBaseAction::class, $o, 'page')
394402
);
395403

396-
$this->assertEquals(
397-
$bypassPagination ? true : false,
398-
$this->getPropertyValue(RestBaseAction::class, $o, 'all')
399-
);
400-
401404
$this->assertEquals(
402405
['name' => 'DESC', 'status' => 'ASC'],
403406
$this->getPropertyValue(RestBaseAction::class, $o, 'sorts')

0 commit comments

Comments
 (0)