[TASK] Streamline PHPDoc comment matches function/method signature
[Packages/TYPO3.CMS.git] / typo3 / sysext / opendocs / Classes / Service / OpenDocumentService.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Opendocs\Service;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
19
20 /**
21 * Access to open and recent documents
22 */
23 class OpenDocumentService
24 {
25 /**
26 * @var BackendUserAuthentication
27 */
28 protected $backendUser;
29
30 /**
31 * Constructor
32 * @param BackendUserAuthentication|null $backendUser
33 */
34 public function __construct(BackendUserAuthentication $backendUser = null)
35 {
36 $this->backendUser = $backendUser ?: $GLOBALS['BE_USER'];
37 }
38
39 /**
40 * Get the list of open documents for the current user
41 *
42 * @return array
43 */
44 public function getOpenDocuments(): array
45 {
46 $openDocuments = [];
47 $sessionOpenDocuments = $this->backendUser->getModuleData('FormEngine', 'ses');
48
49 if ($sessionOpenDocuments !== null) {
50 $openDocuments = $sessionOpenDocuments[0];
51 }
52
53 return $openDocuments;
54 }
55
56 /**
57 * Get the list of recent documents for the current user
58 *
59 * @return array
60 */
61 public function getRecentDocuments(): array
62 {
63 return $this->backendUser->getModuleData('opendocs::recent') ?: [];
64 }
65
66 /**
67 * Close a document and add it to the list of recent documents
68 *
69 * @param string $identifier a document identifier (MD5 hash)
70 */
71 public function closeDocument(string $identifier): void
72 {
73 $openDocuments = $this->getOpenDocuments();
74
75 if (!isset($openDocuments[$identifier])) {
76 return;
77 }
78
79 $document = $openDocuments[$identifier];
80 unset($openDocuments[$identifier]);
81
82 $this->storeOpenDocuments($openDocuments);
83 $this->addToRecentDocuments($identifier, $document);
84 }
85
86 /**
87 * Store a list of open documents
88 *
89 * @param array $openDocuments
90 */
91 protected function storeOpenDocuments(array $openDocuments): void
92 {
93 list(, $lastOpenDocumentIdentifier) = $this->backendUser->getModuleData('FormEngine', 'ses');
94 $this->backendUser->pushModuleData('FormEngine', [$openDocuments, $lastOpenDocumentIdentifier]);
95 }
96
97 /**
98 * Add a document to the list of recent documents
99 *
100 * @param string $identifier identifier of the document
101 * @param array $document document data
102 */
103 protected function addToRecentDocuments(string $identifier, array $document): void
104 {
105 $recentDocuments = $this->getRecentDocuments();
106 $recentDocuments = array_merge(
107 [$identifier => $document],
108 $recentDocuments
109 );
110
111 // Allow a maximum of 8 recent documents
112 if (count($recentDocuments) > 8) {
113 $recentDocuments = array_slice($recentDocuments, 0, 8);
114 }
115
116 $this->backendUser->pushModuleData('opendocs::recent', $recentDocuments);
117 }
118 }