|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace In2code\In2mcp\Domain\Service; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Exception; |
| 8 | +use In2code\In2mcp\Domain\Repository\BackendUserActivityRepository; |
| 9 | +use In2code\In2mcp\Domain\Repository\ContentRepository; |
| 10 | +use In2code\In2mcp\Domain\Repository\PageRepository; |
| 11 | +use In2code\In2mcp\Domain\Repository\RecentChangeRepository; |
| 12 | +use In2code\In2mcp\Exception\UserNotFoundException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Who changed a record last is information about backend users, so it is given to administrators only |
| 16 | + */ |
| 17 | +class LastEditorService |
| 18 | +{ |
| 19 | + /** |
| 20 | + * A history entry is written in the same request as the change, so its time may differ by a moment |
| 21 | + */ |
| 22 | + private const HISTORY_TOLERANCE_SECONDS = 60; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private readonly BackendUserActivityRepository $backendUserActivityRepository, |
| 26 | + private readonly RecentChangeRepository $recentChangeRepository, |
| 27 | + private readonly BackendUserService $backendUserService, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @throws UserNotFoundException |
| 33 | + */ |
| 34 | + public function isAvailable(): bool |
| 35 | + { |
| 36 | + return $this->backendUserService->isAdmin(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * A page counts as changed by whoever changed it, one of its translations or one of its content elements last |
| 41 | + * |
| 42 | + * @param array<int, int> $lastChangeByPage page uid => timestamp of its last change |
| 43 | + * @return array<int, string> page uid => username |
| 44 | + * @throws Exception |
| 45 | + * @throws UserNotFoundException |
| 46 | + */ |
| 47 | + public function findEditorsOfPages(array $lastChangeByPage): array |
| 48 | + { |
| 49 | + if ($this->isAvailable() === false || $lastChangeByPage === []) { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + $pageUids = array_keys($lastChangeByPage); |
| 54 | + $pageUidsByRecord = [ |
| 55 | + PageRepository::TABLE_NAME => array_combine($pageUids, $pageUids) |
| 56 | + + $this->recentChangeRepository->findRecordsOfPages(PageRepository::TABLE_NAME, $pageUids), |
| 57 | + ContentRepository::TABLE_NAME => $this->recentChangeRepository->findRecordsOfPages( |
| 58 | + ContentRepository::TABLE_NAME, |
| 59 | + $pageUids |
| 60 | + ), |
| 61 | + ]; |
| 62 | + return $this->findEditors($pageUidsByRecord, min($lastChangeByPage)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param array<int, int> $changeByContent content uid => timestamp of its change |
| 67 | + * @return array<int, string> content uid => username |
| 68 | + * @throws Exception |
| 69 | + * @throws UserNotFoundException |
| 70 | + */ |
| 71 | + public function findEditorsOfContent(array $changeByContent): array |
| 72 | + { |
| 73 | + if ($this->isAvailable() === false || $changeByContent === []) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
| 77 | + $contentUids = array_keys($changeByContent); |
| 78 | + return $this->findEditors( |
| 79 | + [ContentRepository::TABLE_NAME => array_combine($contentUids, $contentUids)], |
| 80 | + min($changeByContent) |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param list<array{tablename: string, recuid: int, userid: int}> $historyEntries newest first |
| 86 | + * @param array<string, array<int, int>> $keysByRecord table => record uid => key the editor is reported for |
| 87 | + * @return array<int, int> key => uid of the backend user of the newest entry |
| 88 | + */ |
| 89 | + public function pickNewestEditors(array $historyEntries, array $keysByRecord): array |
| 90 | + { |
| 91 | + $editors = []; |
| 92 | + foreach ($historyEntries as $entry) { |
| 93 | + $key = $keysByRecord[$entry['tablename']][$entry['recuid']] ?? null; |
| 94 | + if ($key !== null && array_key_exists($key, $editors) === false) { |
| 95 | + $editors[$key] = $entry['userid']; |
| 96 | + } |
| 97 | + } |
| 98 | + return $editors; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param array<string, array<int, int>> $keysByRecord |
| 103 | + * @return array<int, string> |
| 104 | + * @throws Exception |
| 105 | + */ |
| 106 | + private function findEditors(array $keysByRecord, int $oldestChange): array |
| 107 | + { |
| 108 | + $historyEntries = $this->backendUserActivityRepository->findHistoryOfRecords( |
| 109 | + array_map(static fn(array $keys): array => array_keys($keys), $keysByRecord), |
| 110 | + $oldestChange - self::HISTORY_TOLERANCE_SECONDS |
| 111 | + ); |
| 112 | + $editorUids = $this->pickNewestEditors($historyEntries, $keysByRecord); |
| 113 | + $usernames = $this->backendUserActivityRepository->findUsernames(array_values(array_unique($editorUids))); |
| 114 | + |
| 115 | + $editors = []; |
| 116 | + foreach ($editorUids as $key => $userUid) { |
| 117 | + $editors[$key] = $usernames[$userUid] ?? ('#' . $userUid); |
| 118 | + } |
| 119 | + return $editors; |
| 120 | + } |
| 121 | +} |
0 commit comments