Skip to content

Commit cc39fc1

Browse files
committed
refactor: eliminate data array with object
1 parent 27b4a03 commit cc39fc1

File tree

5 files changed

+123
-29
lines changed

5 files changed

+123
-29
lines changed

phpmyfaq/admin/stat.adminlog.php

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,24 @@
1717

1818
use phpMyFAQ\Administration\AdminLog;
1919
use phpMyFAQ\Component\Alert;
20+
use phpMyFAQ\Configuration;
2021
use phpMyFAQ\Date;
2122
use phpMyFAQ\Enums\PermissionType;
2223
use phpMyFAQ\Filter;
2324
use phpMyFAQ\Pagination;
2425
use phpMyFAQ\Session\Token;
2526
use phpMyFAQ\Strings;
2627
use phpMyFAQ\Translation;
28+
use phpMyFAQ\User\CurrentUser;
2729

2830
if (!defined('IS_VALID_PHPMYFAQ')) {
2931
http_response_code(400);
3032
exit();
3133
}
3234

35+
$faqConfig = Configuration::getConfigurationInstance();
36+
$user = CurrentUser::getCurrentUser($faqConfig);
37+
3338
$logging = new AdminLog($faqConfig);
3439
$csrfToken = Filter::filterInput(INPUT_GET, 'csrf', FILTER_SANITIZE_SPECIAL_CHARS);
3540

@@ -44,16 +49,16 @@
4449
'adminlog' === $action
4550
) {
4651
$date = new Date($faqConfig);
47-
$perpage = 15;
52+
$perPage = 15;
4853
$pages = Filter::filterInput(INPUT_GET, 'pages', FILTER_VALIDATE_INT);
4954
$page = Filter::filterInput(INPUT_GET, 'page', FILTER_VALIDATE_INT, 1);
5055

5156
if (is_null($pages)) {
52-
$pages = round(($logging->getNumberOfEntries() + ($perpage / 3)) / $perpage, 0);
57+
$pages = round(($logging->getNumberOfEntries() + ($perPage / 3)) / $perPage, 0);
5358
}
5459

55-
$start = ($page - 1) * $perpage;
56-
$lastPage = $start + $perpage;
60+
$start = ($page - 1) * $perPage;
61+
$lastPage = $start + $perPage;
5762

5863
$baseUrl = sprintf(
5964
'%sadmin/?action=adminlog&page=%d',
@@ -65,15 +70,16 @@
6570
$options = [
6671
'baseUrl' => $baseUrl,
6772
'total' => $logging->getNumberOfEntries(),
68-
'perPage' => $perpage,
73+
'perPage' => $perPage,
6974
'pageParamName' => 'page',
7075
];
7176
$pagination = new Pagination($options);
7277

7378
$loggingData = $logging->getAll();
7479
?>
7580

76-
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
81+
<div
82+
class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
7783
<h1 class="h2">
7884
<i aria-hidden="true" class="bi bi-list-ol"></i> <?= Translation::get('ad_menu_adminlog') ?>
7985
</h1>
@@ -105,8 +111,8 @@
105111
<?php
106112
$counter = $displayedCounter = 0;
107113

108-
foreach ($loggingData as $loggingId => $loggingValue) {
109-
if ($displayedCounter >= $perpage) {
114+
foreach ($loggingData as $value) {
115+
if ($displayedCounter >= $perPage) {
110116
++$displayedCounter;
111117
continue;
112118
}
@@ -117,15 +123,15 @@
117123
}
118124
++$displayedCounter;
119125

120-
$user->getUserById($loggingValue['usr'], true);
126+
$user->getUserById($value->getUserId(), true);
121127
?>
122128
<tr>
123-
<td><?= $loggingId ?></td>
124-
<td><?= $date->format(date('Y-m-d H:i', $loggingValue['time'])) ?></td>
129+
<td><?= $value->getId() ?></td>
130+
<td><?= $date->format(date('Y-m-d H:i', $value->getTime())) ?></td>
125131
<td><?= Strings::htmlentities($user->getLogin()) ?></td>
126-
<td><?= $loggingValue['ip'] ?></td>
132+
<td><?= $value->getIp() ?></td>
127133
<td><small><?php
128-
$text = Strings::htmlentities($loggingValue['text']);
134+
$text = Strings::htmlentities($value->getText());
129135
$text = str_replace('Loginerror', Translation::get('ad_log_lger'), $text);
130136
$text = str_replace('Session expired', Translation::get('ad_log_sess'), $text);
131137
$text = str_replace('Useredit', Translation::get('ad_log_edit'), $text);

phpmyfaq/src/phpMyFAQ/Administration/AdminLog.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use phpMyFAQ\Configuration;
2121
use phpMyFAQ\Database;
2222
use phpMyFAQ\User;
23+
use phpMyFAQ\Entity\AdminLog as AdminLogEntity;
2324
use Symfony\Component\HttpFoundation\Request;
2425

2526
/**
@@ -50,6 +51,7 @@ public function getNumberOfEntries(): int
5051

5152
/**
5253
* Returns all data from the admin log.
54+
* @return AdminLogEntity[]
5355
*/
5456
public function getAll(): array
5557
{
@@ -68,7 +70,14 @@ public function getAll(): array
6870
$result = $this->configuration->getDb()->query($query);
6971

7072
while ($row = $this->configuration->getDb()->fetchObject($result)) {
71-
$data[$row->id] = ['time' => $row->time, 'usr' => $row->usr, 'text' => $row->text, 'ip' => $row->ip];
73+
$adminLog = new AdminLogEntity();
74+
$adminLog
75+
->setId($row->id)
76+
->setTime($row->time)
77+
->setUserId($row->usr)
78+
->setText($row->text)
79+
->setIp($row->ip);
80+
$data[$row->id] = $adminLog;
7281
}
7382

7483
return $data;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace phpMyFAQ\Entity;
4+
5+
class AdminLog
6+
{
7+
private int $id;
8+
9+
private int $time;
10+
11+
private int $userId;
12+
13+
private string $text;
14+
15+
private string $ip;
16+
public function getId(): int
17+
{
18+
return $this->id;
19+
}
20+
21+
public function setId(int $id): AdminLog
22+
{
23+
$this->id = $id;
24+
return $this;
25+
}
26+
27+
public function getTime(): int
28+
{
29+
return $this->time;
30+
}
31+
32+
public function setTime(int $time): AdminLog
33+
{
34+
$this->time = $time;
35+
return $this;
36+
}
37+
38+
public function getUserId(): int
39+
{
40+
return $this->userId;
41+
}
42+
43+
public function setUserId(int $userId): AdminLog
44+
{
45+
$this->userId = $userId;
46+
return $this;
47+
}
48+
49+
public function getText(): string
50+
{
51+
return $this->text;
52+
}
53+
54+
public function setText(string $text): AdminLog
55+
{
56+
$this->text = $text;
57+
return $this;
58+
}
59+
60+
public function getIp(): string
61+
{
62+
return $this->ip;
63+
}
64+
65+
public function setIp(string $ip): AdminLog
66+
{
67+
$this->ip = $ip;
68+
return $this;
69+
}
70+
}

phpmyfaq/src/phpMyFAQ/Entity/CategoryEntity.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getActive(): bool
5252
/**
5353
* @param bool $active
5454
*/
55-
public function setActive($active): CategoryEntity
55+
public function setActive(bool $active): CategoryEntity
5656
{
5757
$this->active = $active;
5858

@@ -127,7 +127,7 @@ public function getParentId(): int
127127
/**
128128
* @param int $parentId
129129
*/
130-
public function setParentId($parentId): CategoryEntity
130+
public function setParentId(int $parentId): CategoryEntity
131131
{
132132
$this->parentId = $parentId;
133133

@@ -142,7 +142,7 @@ public function getUserId(): int
142142
/**
143143
* @param int $userId
144144
*/
145-
public function setUserId($userId): CategoryEntity
145+
public function setUserId(int $userId): CategoryEntity
146146
{
147147
$this->userId = $userId;
148148

tests/phpMyFAQ/Administration/AdminLogTest.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace phpMyFAQ\Administration;
44

55
use phpMyFAQ\Configuration;
6+
use phpMyFAQ\Core\Exception;
67
use phpMyFAQ\Database\Sqlite3;
8+
use phpMyFAQ\Entity\AdminLog as AdminLogEntity;
79
use phpMyFAQ\System;
810
use phpMyFAQ\User;
911
use PHPUnit\Framework\TestCase;
@@ -62,27 +64,34 @@ public function testGetNumberOfEntries(): void
6264
$this->assertEquals(2, $this->adminLog->getNumberOfEntries());
6365
}
6466

67+
/**
68+
* @throws Exception
69+
*/
6570
public function testGetAll(): void
6671
{
6772
$_SERVER['REQUEST_TIME'] = $this->now;
6873
$this->adminLog->log(new User($this->configuration), 'foo');
6974
$this->adminLog->log(new User($this->configuration), 'bar');
7075

7176
$result = $this->adminLog->getAll();
77+
78+
$adminLogEntity = new AdminLogEntity();
79+
$one = $adminLogEntity->setId(1)
80+
->setTime($this->now)
81+
->setUserId(-1)
82+
->setText('foo')
83+
->setIp('127.0.0.1');
84+
$adminLogEntity = new AdminLogEntity();
85+
$two = $adminLogEntity->setId(2)
86+
->setTime($this->now)
87+
->setUserId(-1)
88+
->setText('bar')
89+
->setIp('127.0.0.1');
90+
7291
$this->assertEquals(
7392
[
74-
2 => [
75-
'time' => $this->now,
76-
'usr' => -1,
77-
'text' => 'bar',
78-
'ip' => '127.0.0.1'
79-
],
80-
1 => [
81-
'time' => $this->now,
82-
'usr' => -1,
83-
'text' => 'foo',
84-
'ip' => '127.0.0.1'
85-
]
93+
2 => $two,
94+
1 => $one
8695
],
8796
$result
8897
);

0 commit comments

Comments
 (0)