From b425efaf8e37d38a8a15e738b27d9bfd21aaba28 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 29 Mar 2022 16:41:17 +0200 Subject: [PATCH] phpstan and phpcs fixes --- Admin/Install/Navigation.install.json | 10 ++-- Controller/ApiController.php | 78 ++++++++++++++++++--------- Controller/BackendController.php | 2 +- Models/WikiDocHistory.php | 50 ++++++++++++++--- 4 files changed, 103 insertions(+), 37 deletions(-) diff --git a/Admin/Install/Navigation.install.json b/Admin/Install/Navigation.install.json index 865ab03..63f00ac 100755 --- a/Admin/Install/Navigation.install.json +++ b/Admin/Install/Navigation.install.json @@ -10,7 +10,7 @@ "icon": null, "order": 50, "from": "Knowledgebase", - "permission": { "permission": 2, "type": null, "element": null }, + "permission": { "permission": 2, "category": null, "element": null }, "parent": 1003301001, "children": [ { @@ -24,7 +24,7 @@ "icon": null, "order": 1, "from": "Knowledgebase", - "permission": { "permission": 2, "type": null, "element": null }, + "permission": { "permission": 2, "category": null, "element": null }, "parent": 1005901001, "children": [] }, @@ -39,7 +39,7 @@ "icon": null, "order": 5, "from": "Knowledgebase", - "permission": { "permission": 2, "type": null, "element": null }, + "permission": { "permission": 2, "category": null, "element": null }, "parent": 1005901001, "children": [] }, @@ -54,7 +54,7 @@ "icon": null, "order": 10, "from": "Knowledgebase", - "permission": { "permission": 4, "type": null, "element": null }, + "permission": { "permission": 4, "category": null, "element": null }, "parent": 1005901001, "children": [] }, @@ -69,7 +69,7 @@ "icon": null, "order": 15, "from": "Knowledgebase", - "permission": { "permission": 2, "type": null, "element": null }, + "permission": { "permission": 2, "category": null, "element": null }, "parent": 1005901001, "children": [] } diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 89e1ac5..45a2072 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -93,9 +93,19 @@ final class ApiController extends Controller $this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Wiki', 'Wiki successfully created.', $doc); } + /** + * Create media files for wiki document + * + * @param WikiDoc $doc Wiki document + * @param RequestAbstract $request Request incl. media do upload + * + * @return void + * + * @since 1.0.0 + */ private function createWikiMedia(WikiDoc $doc, RequestAbstract $request) : void { - $path = $this->createWikiDir($doc); + $path = $this->createWikiDir($doc); $account = AccountMapper::get()->where('id', $request->header->account)->execute(); if (!empty($uploadedFiles = $request->getFiles() ?? [])) { @@ -114,20 +124,21 @@ final class ApiController extends Controller MediaMapper::create()->execute($media); WikiDocMapper::writer()->createRelationTable('media', [$media->getId()], $doc->getId()); - $ref = new Reference(); - $ref->name = $media->name; - $ref->source = new NullMedia($media->getId()); + $accountPath = '/Accounts/' . $account->getId() . ' ' . $account->login . '/Knowledgebase/' . ($doc->category?->getId() ?? '0') . '/' . $doc->getId(); + + $ref = new Reference(); + $ref->name = $media->name; + $ref->source = new NullMedia($media->getId()); $ref->createdBy = new NullAccount($request->header->account); - $ref->setVirtualPath($accountPath = '/Accounts/' . $account->getId() . ' ' . $account->login . '/Knowledgebase/' . $doc->category->getId() . '/' . $doc->getId()); + $ref->setVirtualPath($accountPath); ReferenceMapper::create()->execute($ref); if ($collection === null) { $collection = $this->app->moduleManager->get('Media')->createRecursiveMediaCollection( - '/Modules/Media/Files', $accountPath, $request->header->account, - __DIR__ . '/../../../Modules/Media/Files/Accounts/' . $account->getId() . '/Knowledgebase/' . $doc->category->getId() . '/' . $doc->getId() + __DIR__ . '/../../../Modules/Media/Files/Accounts/' . $account->getId() . '/Knowledgebase/' . ($doc->category?->getId() ?? '0') . '/' . $doc->getId() ); } @@ -141,8 +152,8 @@ final class ApiController extends Controller foreach ($mediaFiles as $media) { WikiDocMapper::writer()->createRelationTable('media', [(int) $media], $doc->getId()); - $ref = new Reference(); - $ref->source = new NullMedia((int) $media); + $ref = new Reference(); + $ref->source = new NullMedia((int) $media); $ref->createdBy = new NullAccount($request->header->account); $ref->setVirtualPath($path); @@ -150,7 +161,6 @@ final class ApiController extends Controller if ($collection === null) { $collection = $this->app->moduleManager->get('Media')->createRecursiveMediaCollection( - '/Modules/Media/Files', $path, $request->header->account, __DIR__ . '/../../../Modules/Media/Files' . $path @@ -162,10 +172,19 @@ final class ApiController extends Controller } } + /** + * Create media directory path + * + * @param WikiDoc $doc Doc + * + * @return string + * + * @since 1.0.0 + */ private function createWikiDir(WikiDoc $doc) : string { return '/Modules/Knowledgebase/' - . $doc->category->getId() . '/' + . ($doc->category?->getId() ?? '0') . '/' . $doc->getId(); } @@ -182,17 +201,17 @@ final class ApiController extends Controller */ public function createWikiDocFromRequest(RequestAbstract $request, ResponseAbstract $response, $data = null) : WikiDoc { - $doc = new WikiDoc(); - $doc->createdBy = new NullAccount($request->header->account); - $doc->name = (string) $request->getData('title'); - $doc->doc = Markdown::parse((string) ($request->getData('plain') ?? '')); - $doc->docRaw = (string) ($request->getData('plain') ?? ''); - $doc->isVersioned = (bool) ($request->getData('versioned') ?? false); - $doc->category = new NullWikiCategory((int) ($request->getData('category') ?? 1)); + $doc = new WikiDoc(); + $doc->createdBy = new NullAccount($request->header->account); + $doc->name = (string) $request->getData('title'); + $doc->doc = Markdown::parse((string) ($request->getData('plain') ?? '')); + $doc->docRaw = (string) ($request->getData('plain') ?? ''); + $doc->isVersioned = (bool) ($request->getData('versioned') ?? false); + $doc->category = new NullWikiCategory((int) ($request->getData('category') ?? 1)); + $doc->app = new NullWikiApp((int) ($request->getData('app') ?? 1)); + $doc->version = (string) ($request->getData('version') ?? ''); $doc->setLanguage((string) ($request->getData('language') ?? $request->getLanguage())); $doc->setStatus((int) ($request->getData('status') ?? WikiStatus::INACTIVE)); - $doc->app = new NullWikiApp((int) ($request->getData('app') ?? 1)); - $doc->version = (string) ($request->getData('version') ?? ''); if (!empty($tags = $request->getDataJson('tags'))) { foreach ($tags as $tag) { @@ -214,6 +233,15 @@ final class ApiController extends Controller return $doc; } + /** + * Create history from document + * + * @param WikiDoc $doc Document + * + * @return WikiDocHistory + * + * @since 1.0.0 + */ private function createHistory(WikiDoc $doc) : WikiDocHistory { $history = WikiDocHistory::createFromDoc($doc); @@ -378,12 +406,12 @@ final class ApiController extends Controller private function updateDocFromRequest(RequestAbstract $request) : WikiDoc { /** @var WikiDoc $doc */ - $doc = WikiDocMapper::get()->where('id', (int) $request->getData('id'))->execute(); + $doc = WikiDocMapper::get()->where('id', (int) $request->getData('id'))->execute(); $doc->isVersioned = (bool) ($request->getData('versioned') ?? $doc->isVersioned); - $doc->name = (string) ($request->getData('title') ?? $doc->name); - $doc->docRaw = (string) ($request->getData('plain') ?? $doc->docRaw); - $doc->doc = Markdown::parse((string) ($request->getData('plain') ?? $doc->docRaw)); - $doc->version = (string) ($request->getData('version') ?? $doc->version); + $doc->name = (string) ($request->getData('title') ?? $doc->name); + $doc->docRaw = (string) ($request->getData('plain') ?? $doc->docRaw); + $doc->doc = Markdown::parse((string) ($request->getData('plain') ?? $doc->docRaw)); + $doc->version = (string) ($request->getData('version') ?? $doc->version); return $doc; } diff --git a/Controller/BackendController.php b/Controller/BackendController.php index 30be18e..34c556c 100755 --- a/Controller/BackendController.php +++ b/Controller/BackendController.php @@ -296,7 +296,7 @@ final class BackendController extends Controller { $view = new View($this->app->l11nManager, $request, $response); - $app = (int) ($request->getData('app') ?? $this->app->orgId); + $app = (int) ($request->getData('app') ?? $this->app->orgId); $document = WikiDocMapper::get() ->with('tags') diff --git a/Models/WikiDocHistory.php b/Models/WikiDocHistory.php index 7a94913..1c17f5b 100644 --- a/Models/WikiDocHistory.php +++ b/Models/WikiDocHistory.php @@ -14,6 +14,8 @@ declare(strict_types=1); namespace Modules\Knowledgebase\Models; +use Modules\Admin\Models\Account; +use Modules\Admin\Models\NullAccount; use phpOMS\Localization\ISO639x1Enum; /** @@ -74,6 +76,22 @@ class WikiDocHistory implements \JsonSerializable */ public string $docRaw = ''; + /** + * Created. + * + * @var \DateTimeImmutable + * @since 1.0.0 + */ + public \DateTimeImmutable $createdAt; + + /** + * Creator. + * + * @var Account + * @since 1.0.0 + */ + public Account $createdBy; + /** * Language. * @@ -82,15 +100,35 @@ class WikiDocHistory implements \JsonSerializable */ private string $language = ISO639x1Enum::_EN; + /** + * Constructor. + * + * @since 1.0.0 + */ + public function __construct() + { + $this->createdBy = new NullAccount(); + $this->createdAt = new \DateTimeImmutable('now'); + } + + /** + * Create history form model + * + * @param WikiDoc $doc Document + * + * @return self + * + * @since 1.0.0 + */ public static function createFromDoc(WikiDoc $doc) : self { - $hist = new self(); - $hist->article = $doc->getId(); + $hist = new self(); + $hist->article = $doc->getId(); $hist->createdBy = $doc->createdBy; - $hist->name = $doc->name; - $hist->doc = $doc->doc; - $hist->docRaw = $doc->docRaw; - $hist->version = $doc->version; + $hist->name = $doc->name; + $hist->doc = $doc->doc; + $hist->docRaw = $doc->docRaw; + $hist->version = $doc->version; return $hist; }