From 9a2694fcb0093afee13d0cd91de15b34b747cc62 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Julius=20H=C3=A4rtl?= <jus@bitgrid.net>
Date: Mon, 25 Nov 2019 14:09:38 +0100
Subject: [PATCH] Code style fixes and cleanup
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Julius Härtl <jus@bitgrid.net>
---
 .../CleanupDirectEditingTokens.php            |  2 +-
 apps/files/lib/Capabilities.php               | 16 ++++-
 .../Controller/DirectEditingController.php    |  6 +-
 .../Version18000Date20191014105105.php        | 70 +++++++++----------
 lib/private/DirectEditing/Manager.php         |  5 +-
 .../DirectEditing/ACreateFromTemplate.php     |  2 +-
 lib/public/DirectEditing/IEditor.php          |  6 +-
 tests/lib/DirectEditing/ManagerTest.php       | 20 +++++-
 8 files changed, 75 insertions(+), 52 deletions(-)

diff --git a/apps/files/lib/BackgroundJob/CleanupDirectEditingTokens.php b/apps/files/lib/BackgroundJob/CleanupDirectEditingTokens.php
index 77907fab281..8d4a3f23787 100644
--- a/apps/files/lib/BackgroundJob/CleanupDirectEditingTokens.php
+++ b/apps/files/lib/BackgroundJob/CleanupDirectEditingTokens.php
@@ -7,7 +7,7 @@ use OCP\DirectEditing\IManager;
 
 class CleanupDirectEditingTokens extends TimedJob {
 
-	const INTERVAL_MINUTES = 15 * 60;
+	private const INTERVAL_MINUTES = 15 * 60;
 
 	/**
 	 * @var IManager
diff --git a/apps/files/lib/Capabilities.php b/apps/files/lib/Capabilities.php
index c37e32b6b59..19b59971c4c 100644
--- a/apps/files/lib/Capabilities.php
+++ b/apps/files/lib/Capabilities.php
@@ -30,6 +30,8 @@ use OCP\Capabilities\ICapability;
 use OCP\DirectEditing\ACreateEmpty;
 use OCP\DirectEditing\ACreateFromTemplate;
 use OCP\DirectEditing\IEditor;
+use OCP\DirectEditing\RegisterDirectEditorEvent;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IConfig;
 
 /**
@@ -38,17 +40,25 @@ use OCP\IConfig;
  * @package OCA\Files
  */
 class Capabilities implements ICapability {
+
 	/** @var IConfig */
 	protected $config;
 
+	/** @var Manager */
+	protected $directEditingManager;
+
+	/** @var IEventDispatcher */
+	protected $eventDispatcher;
+
 	/**
 	 * Capabilities constructor.
 	 *
 	 * @param IConfig $config
 	 */
-	public function __construct(IConfig $config, Manager $manager) {
+	public function __construct(IConfig $config, Manager $manager, IEventDispatcher $eventDispatcher) {
 		$this->config = $config;
 		$this->directEditingManager = $manager;
+		$this->eventDispatcher = $eventDispatcher;
 	}
 
 	/**
@@ -66,7 +76,9 @@ class Capabilities implements ICapability {
 		];
 	}
 
-	private function getDirectEditingCapabilitites() {
+	private function getDirectEditingCapabilitites(): array {
+		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
+
 		$capabilities = [
 			'editors' => [],
 			'creators' => []
diff --git a/apps/files/lib/Controller/DirectEditingController.php b/apps/files/lib/Controller/DirectEditingController.php
index e8791316442..11d09e2f074 100644
--- a/apps/files/lib/Controller/DirectEditingController.php
+++ b/apps/files/lib/Controller/DirectEditingController.php
@@ -66,7 +66,7 @@ class DirectEditingController extends OCSController {
 	 * @NoAdminRequired
 	 */
 	public function create(string $path, string $editorId, string $creatorId, string $templateId = null): DataResponse {
-		$this->eventDispatcher->dispatch(RegisterDirectEditorEvent::class, new RegisterDirectEditorEvent($this->directEditingManager));
+		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
 
 		try {
 			$token = $this->directEditingManager->create($path, $editorId, $creatorId, $templateId);
@@ -83,7 +83,7 @@ class DirectEditingController extends OCSController {
 	 * @NoAdminRequired
 	 */
 	public function open(int $fileId, string $editorId = null): DataResponse {
-		$this->eventDispatcher->dispatch(RegisterDirectEditorEvent::class, new RegisterDirectEditorEvent($this->directEditingManager));
+		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
 
 		try {
 			$token = $this->directEditingManager->open($fileId, $editorId);
@@ -102,7 +102,7 @@ class DirectEditingController extends OCSController {
 	 * @NoAdminRequired
 	 */
 	public function templates(string $editorId, string $creatorId): DataResponse {
-		$this->eventDispatcher->dispatch(RegisterDirectEditorEvent::class, new RegisterDirectEditorEvent($this->directEditingManager));
+		$this->eventDispatcher->dispatchTyped(new RegisterDirectEditorEvent($this->directEditingManager));
 
 		try {
 			return new DataResponse($this->directEditingManager->getTemplates($editorId, $creatorId));
diff --git a/core/Migrations/Version18000Date20191014105105.php b/core/Migrations/Version18000Date20191014105105.php
index b291c0b5e46..634f9f91faf 100644
--- a/core/Migrations/Version18000Date20191014105105.php
+++ b/core/Migrations/Version18000Date20191014105105.php
@@ -50,44 +50,42 @@ class Version18000Date20191014105105 extends SimpleMigrationStep {
 	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
 		/** @var ISchemaWrapper $schema */
 		$schema = $schemaClosure();
-		if (!$schema->hasTable('direct_edit')) {
-			$table = $schema->createTable('direct_edit');
+		$table = $schema->createTable('direct_edit');
 
-			$table->addColumn('id', Type::BIGINT, [
-				'autoincrement' => true,
-				'notnull' => true,
-			]);
-			$table->addColumn('editor_id', Type::STRING, [
-				'notnull' => true,
-				'length' => 64,
-			]);
-			$table->addColumn('token', Type::STRING, [
-				'notnull' => true,
-				'length' => 64,
-			]);
-			$table->addColumn('file_id', Type::BIGINT, [
-				'notnull' => true,
-			]);
-			$table->addColumn('user_id', Type::STRING, [
-				'notnull' => false,
-				'length' => 64,
-			]);
-			$table->addColumn('share_id', Type::BIGINT, [
-				'notnull' => false
-			]);
-			$table->addColumn('timestamp', Type::BIGINT, [
-				'notnull' => true,
-				'length' => 20,
-				'unsigned' => true,
-			]);
-			$table->addColumn('accessed', Type::BOOLEAN, [
-				'notnull' => true,
-				'default' => false
-			]);
+		$table->addColumn('id', Type::BIGINT, [
+			'autoincrement' => true,
+			'notnull' => true,
+		]);
+		$table->addColumn('editor_id', Type::STRING, [
+			'notnull' => true,
+			'length' => 64,
+		]);
+		$table->addColumn('token', Type::STRING, [
+			'notnull' => true,
+			'length' => 64,
+		]);
+		$table->addColumn('file_id', Type::BIGINT, [
+			'notnull' => true,
+		]);
+		$table->addColumn('user_id', Type::STRING, [
+			'notnull' => false,
+			'length' => 64,
+		]);
+		$table->addColumn('share_id', Type::BIGINT, [
+			'notnull' => false
+		]);
+		$table->addColumn('timestamp', Type::BIGINT, [
+			'notnull' => true,
+			'length' => 20,
+			'unsigned' => true,
+		]);
+		$table->addColumn('accessed', Type::BOOLEAN, [
+			'notnull' => true,
+			'default' => false
+		]);
 
-			$table->setPrimaryKey(['id']);
-			$table->addIndex(['token']);
-		}
+		$table->setPrimaryKey(['id']);
+		$table->addIndex(['token']);
 
 		return $schema;
 	}
diff --git a/lib/private/DirectEditing/Manager.php b/lib/private/DirectEditing/Manager.php
index fdf0a1f0f0f..26adad9e572 100644
--- a/lib/private/DirectEditing/Manager.php
+++ b/lib/private/DirectEditing/Manager.php
@@ -64,15 +64,12 @@ class Manager implements IManager {
 		ISecureRandom $random,
 		IDBConnection $connection,
 		IUserSession $userSession,
-		IRootFolder $rootFolder,
-		IEventDispatcher $eventDispatcher
+		IRootFolder $rootFolder
 	) {
 		$this->random = $random;
 		$this->connection = $connection;
 		$this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
 		$this->rootFolder = $rootFolder;
-		$eventDispatcher->dispatch(RegisterDirectEditorEvent::class, new RegisterDirectEditorEvent($this));
-
 	}
 
 	public function registerDirectEditor(IEditor $directEditor): void {
diff --git a/lib/public/DirectEditing/ACreateFromTemplate.php b/lib/public/DirectEditing/ACreateFromTemplate.php
index a731e8be595..89420a63743 100644
--- a/lib/public/DirectEditing/ACreateFromTemplate.php
+++ b/lib/public/DirectEditing/ACreateFromTemplate.php
@@ -32,7 +32,7 @@ abstract class ACreateFromTemplate extends ACreateEmpty {
 	 * List of available templates for the create from template action
 	 *
 	 * @since 18.0.0
-	 * @return array
+	 * @return ATemplate[]
 	 */
 	abstract public function getTemplates(): array;
 
diff --git a/lib/public/DirectEditing/IEditor.php b/lib/public/DirectEditing/IEditor.php
index a4fc87f7e15..a2bc0d74255 100644
--- a/lib/public/DirectEditing/IEditor.php
+++ b/lib/public/DirectEditing/IEditor.php
@@ -56,7 +56,7 @@ interface IEditor {
 	 * A list of mimetypes that should open the editor by default
 	 *
 	 * @since 18.0.0
-	 * @return array
+	 * @return string[]
 	 */
 	public function getMimetypes(): array;
 
@@ -64,7 +64,7 @@ interface IEditor {
 	 * A list of mimetypes that can be opened in the editor optionally
 	 *
 	 * @since 18.0.0
-	 * @return array
+	 * @return string[]
 	 */
 	public function getMimetypesOptional(): array;
 
@@ -72,7 +72,7 @@ interface IEditor {
 	 * Return a list of file creation options to be presented to the user
 	 *
 	 * @since 18.0.0
-	 * @return array of ICreateFromTemplate|ICreateEmpty
+	 * @return ACreateFromTemplate[]|ACreateEmpty[]
 	 */
 	public function getCreators(): array;
 
diff --git a/tests/lib/DirectEditing/ManagerTest.php b/tests/lib/DirectEditing/ManagerTest.php
index b2e58efd8e2..a3d29efbce5 100644
--- a/tests/lib/DirectEditing/ManagerTest.php
+++ b/tests/lib/DirectEditing/ManagerTest.php
@@ -7,7 +7,6 @@ use OC\Files\Node\File;
 use OCP\AppFramework\Http\DataResponse;
 use OCP\AppFramework\Http\NotFoundResponse;
 use OCP\AppFramework\Http\Response;
-use OCP\AppFramework\Http\TemplateResponse;
 use OCP\DirectEditing\ACreateEmpty;
 use OCP\DirectEditing\IEditor;
 use OCP\DirectEditing\IToken;
@@ -16,6 +15,7 @@ use OCP\Files\IRootFolder;
 use OCP\IDBConnection;
 use OCP\IUserSession;
 use OCP\Security\ISecureRandom;
+use PHPUnit\Framework\MockObject\MockObject;
 use Test\TestCase;
 
 class CreateEmpty extends ACreateEmpty {
@@ -82,9 +82,25 @@ class ManagerTest extends TestCase {
 	 */
 	private $editor;
 	/**
-	 * @var \PHPUnit\Framework\MockObject\MockObject
+	 * @var MockObject|ISecureRandom
 	 */
 	private $random;
+	/**
+	 * @var IDBConnection
+	 */
+	private $connection;
+	/**
+	 * @var MockObject|IUserSession
+	 */
+	private $userSession;
+	/**
+	 * @var MockObject|IRootFolder
+	 */
+	private $rootFolder;
+	/**
+	 * @var MockObject|Folder
+	 */
+	private $userFolder;
 
 	protected function setUp() {
 		parent::setUp();
-- 
GitLab