From d7b250b2888ac011e74a452c62635167ec76f425 Mon Sep 17 00:00:00 2001 From: Alexander Kellner Date: Thu, 16 Jun 2022 19:47:56 +0200 Subject: [PATCH] [TASK] Code cleanup --- Classes/Controller/ModuleController.php | 58 +++++-------- Classes/Domain/Factory/FileFactory.php | 8 +- Classes/Domain/Model/File.php | 30 ++++--- .../Service}/SlidingWindowPagination.php | 87 ++++++++++++++++--- 4 files changed, 114 insertions(+), 69 deletions(-) rename Classes/{Utility => Domain/Service}/SlidingWindowPagination.php (81%) diff --git a/Classes/Controller/ModuleController.php b/Classes/Controller/ModuleController.php index b8f9b8a79..de1f955f5 100644 --- a/Classes/Controller/ModuleController.php +++ b/Classes/Controller/ModuleController.php @@ -2,9 +2,12 @@ declare(strict_types = 1); namespace In2code\Powermail\Controller; +use Doctrine\DBAL\DBALException; use In2code\Powermail\Domain\Model\Answer; use In2code\Powermail\Domain\Model\Mail; use In2code\Powermail\Domain\Repository\PageRepository; +use In2code\Powermail\Domain\Service\SlidingWindowPagination; +use In2code\Powermail\Exception\FileCannotBeCreatedException; use In2code\Powermail\Utility\BackendUtility; use In2code\Powermail\Utility\BasicFileUtility; use In2code\Powermail\Utility\ConfigurationUtility; @@ -15,9 +18,10 @@ use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Http\ForwardResponse; +use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException; use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException; -use TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException; use TYPO3\CMS\Extbase\Object\Exception; +use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator; use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException; use TYPO3\CMS\Extbase\Reflection\Exception\PropertyNotAccessibleException; @@ -26,23 +30,23 @@ */ class ModuleController extends AbstractController { - /** * @param string $forwardToAction * @throws StopActionException * @return void * @noinspection PhpUnused */ - public function dispatchAction($forwardToAction = 'list'): ResponseInterface + public function dispatchAction(string $forwardToAction = 'list'): ResponseInterface { $this->forward($forwardToAction); return $this->htmlResponse(); } /** - * @return void + * @return ResponseInterface * @throws InvalidQueryException * @throws RouteNotFoundException + * @throws NoSuchArgumentException * @noinspection PhpUnused */ public function listAction(): ResponseInterface @@ -50,23 +54,13 @@ public function listAction(): ResponseInterface $formUids = $this->mailRepository->findGroupedFormUidsToGivenPageUid((int)$this->id); $mails = $this->mailRepository->findAllInPid((int)$this->id, $this->settings, $this->piVars); - $currentPage = $this->request->hasArgument('currentPage') - ? (int)$this->request->getArgument('currentPage') - : 1; - - $itemsPerPage = (int)$this->settings['perPage'] ? (int)$this->settings['perPage'] : 10; - $maximumLinks = 15; - - // Pagination for Mails - $paginator = new \TYPO3\CMS\Extbase\Pagination\QueryResultPaginator( - $mails, - $currentPage, - $itemsPerPage - ); - $pagination = new \In2code\Powermail\Utility\SlidingWindowPagination( - $paginator, - $maximumLinks - ); + $currentPage = 1; + if ($this->request->hasArgument('currentPage')) { + $currentPage = $this->request->getArgument('currentPage'); + } + $itemsPerPage = $this->settings['perPage'] ?? 10; + $paginator = GeneralUtility::makeInstance(QueryResultPaginator::class, $mails, $currentPage, $itemsPerPage); + $pagination = GeneralUtility::makeInstance(SlidingWindowPagination::class, $paginator, 15); $firstFormUid = StringUtility::conditionalVariable($this->piVars['filter']['form'] ?? '', key($formUids)); $beUser = BackendUtility::getBackendUserAuthentication(); @@ -82,7 +76,7 @@ public function listAction(): ResponseInterface 'pagination' => $pagination, 'paginator' => $paginator ], - 'perPage' => ($this->settings['perPage'] ? $this->settings['perPage'] : 10), + 'perPage' => $this->settings['perPage'] ?? 10, 'writeAccess' => $beUser->check('tables_modify', Answer::TABLE_NAME) && $beUser->check('tables_modify', Mail::TABLE_NAME), ] @@ -209,8 +203,6 @@ public function overviewBeAction(): ResponseInterface /** * @return void - * @throws StopActionException - * @noinspection PhpUnused */ public function initializeCheckBeAction(): void { @@ -218,12 +210,10 @@ public function initializeCheckBeAction(): void } /** - * @param string $email email address - * @return void - * @throws Exception - * @noinspection PhpUnused + * @param string|null $email + * @return ResponseInterface */ - public function checkBeAction($email = null): ResponseInterface + public function checkBeAction(string $email = null): ResponseInterface { $this->view->assign('pid', $this->id); $this->sendTestEmail($email); @@ -233,7 +223,6 @@ public function checkBeAction($email = null): ResponseInterface /** * @param null $email * @return void - * @throws Exception */ protected function sendTestEmail($email = null): void { @@ -252,7 +241,6 @@ protected function sendTestEmail($email = null): void /** * @return void - * @throws StopActionException * @noinspection PhpUnused */ public function initializeConverterBeAction(): void @@ -262,7 +250,6 @@ public function initializeConverterBeAction(): void /** * @return void - * @throws StopActionException * @noinspection PhpUnused */ public function initializeFixUploadFolderAction(): void @@ -273,8 +260,7 @@ public function initializeFixUploadFolderAction(): void /** * @return void * @throws StopActionException - * @throws UnsupportedRequestTypeException - * @throws \Exception + * @throws FileCannotBeCreatedException * @noinspection PhpUnused */ public function fixUploadFolderAction(): void @@ -285,7 +271,6 @@ public function fixUploadFolderAction(): void /** * @return void - * @throws StopActionException * @noinspection PhpUnused */ public function initializeFixWrongLocalizedFormsAction(): void @@ -296,7 +281,7 @@ public function initializeFixWrongLocalizedFormsAction(): void /** * @return void * @throws StopActionException - * @throws UnsupportedRequestTypeException + * @throws DBALException * @noinspection PhpUnused */ public function fixWrongLocalizedFormsAction(): void @@ -307,7 +292,6 @@ public function fixWrongLocalizedFormsAction(): void /** * @return void - * @throws StopActionException * @noinspection PhpUnused */ public function initializeFixWrongLocalizedPagesAction(): void diff --git a/Classes/Domain/Factory/FileFactory.php b/Classes/Domain/Factory/FileFactory.php index 77871f0aa..41e822505 100644 --- a/Classes/Domain/Factory/FileFactory.php +++ b/Classes/Domain/Factory/FileFactory.php @@ -52,10 +52,10 @@ public function __construct(array $settings) */ public function getInstanceFromFilesArray(array $filesArray, string $marker, int $key): ?File { - $originalName = (string)$filesArray['name']['field'][$marker][$key]; - $size = (int)$filesArray['size']['field'][$marker][$key]; - $type = (string)$filesArray['type']['field'][$marker][$key]; - $temporaryName = (string)$filesArray['tmp_name']['field'][$marker][$key]; + $originalName = $filesArray['name']['field'][$marker][$key] ?? ''; + $size = $filesArray['size']['field'][$marker][$key] ?? 0; + $type = $filesArray['type']['field'][$marker][$key] ?? ''; + $temporaryName = $filesArray['tmp_name']['field'][$marker][$key] ?? ''; if (!empty($originalName) && !empty($temporaryName) && $size > 0) { return $this->makeFileInstance($marker, $originalName, $size, $type, $temporaryName); } diff --git a/Classes/Domain/Model/File.php b/Classes/Domain/Model/File.php index 66e04ceec..ba1570721 100644 --- a/Classes/Domain/Model/File.php +++ b/Classes/Domain/Model/File.php @@ -21,77 +21,77 @@ class File * * @var string */ - protected $marker = ''; + protected string $marker = ''; /** * Original name * * @var string */ - protected $originalName = ''; + protected string $originalName = ''; /** * Temporary uploaded name * * @var string|null */ - protected $temporaryName = null; + protected ?string $temporaryName = null; /** * New, cleaned and unique filename * * @var string */ - protected $newName = ''; + protected string $newName = ''; /** * Is there a problem with this file? * * @var bool */ - protected $valid = true; + protected bool $valid = true; /** * Like "image/png" * * @var string */ - protected $type = ''; + protected string $type = ''; /** * Filesize * * @var int */ - protected $size = 0; + protected int $size = 0; /** * Uploadfolder for this file * * @var string */ - protected $uploadFolder = 'uploads/tx_powermail/'; + protected string $uploadFolder = 'uploads/tx_powermail/'; /** * Already uploaded to uploadfolder? * * @var bool */ - protected $uploaded = false; + protected bool $uploaded = false; /** * File must be renamed? * * @var bool */ - protected $renamed = false; + protected bool $renamed = false; /** * Related field * * @var Field|null */ - protected $field = null; + protected ?Field $field = null; /** * @param string $marker @@ -134,8 +134,9 @@ public function getOriginalName(): string /** * @param string $originalName * @return File + * @noinspection PhpUnused */ - public function setOriginalName($originalName): File + public function setOriginalName(string $originalName): File { $this->originalName = $originalName; return $this; @@ -152,6 +153,7 @@ public function getTemporaryName(): string /** * @param string $temporaryName * @return File + * @noinspection PhpUnused */ public function setTemporaryName(string $temporaryName): File { @@ -344,10 +346,10 @@ public function isFileExisting(): bool * @throws InvalidSlotReturnException * @throws Exception */ - public function getNewPathAndFilename($absolute = false): string + public function getNewPathAndFilename(bool $absolute = false): string { $pathAndFilename = $this->getUploadFolder() . $this->getNewName(); - if ($absolute) { + if ($absolute === true) { $pathAndFilename = GeneralUtility::getFileAbsFileName($pathAndFilename); } $this->signalDispatch(__CLASS__, __FUNCTION__, [$pathAndFilename, $this]); diff --git a/Classes/Utility/SlidingWindowPagination.php b/Classes/Domain/Service/SlidingWindowPagination.php similarity index 81% rename from Classes/Utility/SlidingWindowPagination.php rename to Classes/Domain/Service/SlidingWindowPagination.php index 0ac452c17..2882d8b93 100644 --- a/Classes/Utility/SlidingWindowPagination.php +++ b/Classes/Domain/Service/SlidingWindowPagination.php @@ -2,33 +2,50 @@ declare(strict_types = 1); -/* - * This file is part of the TYPO3 CMS project. - * - * It is free software; you can redistribute it and/or modify it under - * the terms of the GNU General Public License, either version 2 - * of the License, or any later version. - * - * For the full copyright and license information, please read the - * LICENSE.txt file that was distributed with this source code. - * - * The TYPO3 project - inspiring people to share! - */ - -namespace In2code\Powermail\Utility; +namespace In2code\Powermail\Domain\Service; use TYPO3\CMS\Core\Pagination\PaginationInterface; use TYPO3\CMS\Core\Pagination\PaginatorInterface; +/** + * Class SlidingWindowPagination + */ final class SlidingWindowPagination implements PaginationInterface { + /** + * @var int + */ protected int $displayRangeStart = 0; + + /** + * @var int + */ protected int $displayRangeEnd = 0; + + /** + * @var bool + */ protected bool $hasLessPages = false; + + /** + * @var bool + */ protected bool $hasMorePages = false; + + /** + * @var int + */ protected int $maximumNumberOfLinks = 0; + + /** + * @var PaginatorInterface + */ protected PaginatorInterface $paginator; + /** + * @param PaginatorInterface $paginator + * @param int $maximumNumberOfLinks + */ public function __construct(PaginatorInterface $paginator, int $maximumNumberOfLinks = 0) { $this->paginator = $paginator; @@ -40,6 +57,9 @@ public function __construct(PaginatorInterface $paginator, int $maximumNumberOfL $this->calculateDisplayRange(); } + /** + * @return int|null + */ public function getPreviousPageNumber(): ?int { $previousPage = $this->paginator->getCurrentPageNumber() - 1; @@ -51,6 +71,9 @@ public function getPreviousPageNumber(): ?int return $previousPage >= $this->getFirstPageNumber() ? $previousPage : null; } + /** + * @return int|null + */ public function getNextPageNumber(): ?int { $nextPage = $this->paginator->getCurrentPageNumber() + 1; @@ -58,16 +81,25 @@ public function getNextPageNumber(): ?int return $nextPage <= $this->paginator->getNumberOfPages() ? $nextPage : null; } + /** + * @return int + */ public function getFirstPageNumber(): int { return 1; } + /** + * @return int + */ public function getLastPageNumber(): int { return $this->paginator->getNumberOfPages(); } + /** + * @return int + */ public function getStartRecordNumber(): int { if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) { @@ -77,6 +109,9 @@ public function getStartRecordNumber(): int return $this->paginator->getKeyOfFirstPaginatedItem() + 1; } + /** + * @return int + */ public function getEndRecordNumber(): int { if ($this->paginator->getCurrentPageNumber() > $this->paginator->getNumberOfPages()) { @@ -86,41 +121,65 @@ public function getEndRecordNumber(): int return $this->paginator->getKeyOfLastPaginatedItem() + 1; } + /** + * @return array + */ public function getAllPageNumbers(): array { return range($this->displayRangeStart, $this->displayRangeEnd); } + /** + * @return int + */ public function getDisplayRangeStart(): int { return $this->displayRangeStart; } + /** + * @return int + */ public function getDisplayRangeEnd(): int { return $this->displayRangeEnd; } + /** + * @return bool + */ public function getHasLessPages(): bool { return $this->hasLessPages; } + /** + * @return bool + */ public function getHasMorePages(): bool { return $this->hasMorePages; } + /** + * @return int + */ public function getMaximumNumberOfLinks(): int { return $this->maximumNumberOfLinks; } + /** + * @return PaginatorInterface + */ public function getPaginator(): PaginatorInterface { return $this->paginator; } + /** + * @return void + */ protected function calculateDisplayRange(): void { $maximumNumberOfLinks = $this->maximumNumberOfLinks;