Skip to content

Commit a41d361

Browse files
committed
refactor: added interfaces, code cleanup
1 parent 32096d3 commit a41d361

File tree

6 files changed

+116
-28
lines changed

6 files changed

+116
-28
lines changed

phpmyfaq/src/phpMyFAQ/Bookmark/BookmarkFormatter.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,20 @@ public function format(object $bookmark): ?array
6767
$category = new Category($this->configuration);
6868
$categoryId = $category->getCategoryIdFromFaq((int) $faqData['id']);
6969

70-
$url = strtr('base:index.php?action=faq&id=id:&cat=cat:&artlang=lang:', [
71-
'base:' => $this->configuration->getDefaultUrl(),
72-
'id:' => (string) (int) $faqData['id'],
73-
'cat:' => (string) $categoryId,
74-
'lang:' => (string) ($faqData['lang'] ?? ''),
75-
]);
70+
$base = rtrim($this->configuration->getDefaultUrl(), characters: '/') . '/index.php';
71+
$query = http_build_query(
72+
[
73+
'action' => 'faq',
74+
'id' => (int) $faqData['id'],
75+
'cat' => $categoryId,
76+
'artlang' => $faqData['lang'] ?? '',
77+
],
78+
numeric_prefix: '',
79+
arg_separator: '&',
80+
encoding_type: PHP_QUERY_RFC3986,
81+
);
82+
83+
$url = $base . '?' . $query;
7684

7785
$link = new Link($url, $this->configuration);
7886
$title = (string) ($faqData['title'] ?? '');

phpmyfaq/src/phpMyFAQ/Bookmark/BookmarkRepository.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
/**
4-
* Bookmark Respository.
4+
* Bookmark Repository.
55
*
66
* This Source Code Form is subject to the terms of the Mozilla Public License,
77
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
@@ -24,7 +24,7 @@
2424
use phpMyFAQ\Database;
2525
use phpMyFAQ\User\CurrentUser;
2626

27-
readonly class BookmarkRepository
27+
readonly class BookmarkRepository implements BookmarkRepositoryInterface
2828
{
2929
public function __construct(
3030
private Configuration $configuration,
@@ -40,11 +40,10 @@ public function add(int $faqId): bool
4040

4141
$table = Database::getTablePrefix() . 'faqbookmarks';
4242
$userId = $this->currentUser->getUserId();
43-
$query = strtr('INSERT INTO table: (userid, faqid) VALUES (userId:, faqId:)', [
44-
'table:' => $table,
45-
'userId:' => (string) $userId,
46-
'faqId:' => (string) $faqId,
47-
]);
43+
44+
$query = <<<SQL
45+
INSERT INTO {$table} (userid, faqid) VALUES ({$userId},{$faqId})
46+
SQL;
4847

4948
return (bool) $this->configuration->getDb()->query($query);
5049
}
@@ -56,10 +55,10 @@ public function getAll(): array
5655
{
5756
$table = Database::getTablePrefix() . 'faqbookmarks';
5857
$userId = $this->currentUser->getUserId();
59-
$query = strtr('SELECT faqid FROM table: WHERE userid = userId:', [
60-
'table:' => $table,
61-
'userId:' => (string) $userId,
62-
]);
58+
59+
$query = <<<SQL
60+
SELECT faqid FROM {$table} WHERE userid = {$userId}
61+
SQL;
6362

6463
$result = $this->configuration->getDb()->query($query);
6564
$data = $this->configuration->getDb()->fetchAll($result);
@@ -75,11 +74,10 @@ public function remove(int $faqId): bool
7574

7675
$table = Database::getTablePrefix() . 'faqbookmarks';
7776
$userId = $this->currentUser->getUserId();
78-
$query = strtr('DELETE FROM table: WHERE userid = userId: AND faqid = faqId:', [
79-
'table:' => $table,
80-
'userId:' => (string) $userId,
81-
'faqId:' => (string) $faqId,
82-
]);
77+
78+
$query = <<<SQL
79+
DELETE FROM {$table} WHERE userid = {$userId} AND faqid = {$faqId}
80+
SQL;
8381

8482
return (bool) $this->configuration->getDb()->query($query);
8583
}
@@ -88,10 +86,10 @@ public function removeAll(): bool
8886
{
8987
$table = Database::getTablePrefix() . 'faqbookmarks';
9088
$userId = $this->currentUser->getUserId();
91-
$query = strtr('DELETE FROM table: WHERE userid = userId:', [
92-
'table:' => $table,
93-
'userId:' => (string) $userId,
94-
]);
89+
90+
$query = <<<SQL
91+
DELETE FROM {$table} WHERE userid = {$userId}
92+
SQL;
9593

9694
return (bool) $this->configuration->getDb()->query($query);
9795
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/**
4+
* Bookmark Repository Interface.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author phpMyFAQ Team
12+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace phpMyFAQ\Bookmark;
18+
19+
interface BookmarkRepositoryInterface
20+
{
21+
/**
22+
* Add a bookmark for the given FAQ id.
23+
*/
24+
public function add(int $faqId): bool;
25+
26+
/**
27+
* Return all bookmark rows for the current user.
28+
*
29+
* @return array<int, object>
30+
*/
31+
public function getAll(): array;
32+
33+
/**
34+
* Remove the bookmark for the given FAQ id.
35+
*/
36+
public function remove(int $faqId): bool;
37+
38+
/**
39+
* Remove all bookmarks for the current user.
40+
*/
41+
public function removeAll(): bool;
42+
}

phpmyfaq/src/phpMyFAQ/Comment/CommentsRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
use phpMyFAQ\Entity\Comment;
2525
use phpMyFAQ\Entity\CommentType;
2626

27-
readonly class CommentsRepository
27+
readonly class CommentsRepository implements CommentsRepositoryInterface
2828
{
2929
public function __construct(
3030
private CoreConfiguration $configuration,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Interface for comments repository.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author phpMyFAQ Team
12+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace phpMyFAQ\Comment;
18+
19+
use phpMyFAQ\Entity\Comment;
20+
21+
interface CommentsRepositoryInterface
22+
{
23+
/** @return array<int, object> */
24+
public function fetchByReferenceIdAndType(int $referenceId, string $type): array;
25+
26+
public function insert(Comment $comment): bool;
27+
28+
public function deleteByTypeAndId(string $type, int $commentId): bool;
29+
30+
/** @return array<int, object> */
31+
public function countByTypeGroupedByRecordId(string $type = \phpMyFAQ\Entity\CommentType::FAQ): array;
32+
33+
/** @return array<int, object> */
34+
public function countByCategoryForFaq(): array;
35+
36+
/** @return array<int, object> */
37+
public function fetchAllWithCategories(string $type = \phpMyFAQ\Entity\CommentType::FAQ): array;
38+
39+
public function isCommentAllowed(int $recordId, string $recordLang, string $commentType = 'faq'): bool;
40+
}

phpmyfaq/src/phpMyFAQ/Controller/Administration/Api/FaqController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function update(Request $request): JsonResponse
344344
->setRevisionId($revisionId)
345345
->setSolutionId($solutionId)
346346
->setActive($active === 'yes')
347-
->setSticky($sticky !== 'no' ? true : false)
347+
->setSticky($sticky !== 'no')
348348
->setQuestion(Filter::removeAttributes(html_entity_decode(
349349
(string) $question,
350350
ENT_QUOTES | ENT_HTML5,

0 commit comments

Comments
 (0)