Skip to content

Commit

Permalink
Cleanup run qa tools
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesbochmann committed Sep 2, 2024
1 parent c7f8f99 commit 10b59ed
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 383 deletions.
1 change: 1 addition & 0 deletions .github/workflows/phpci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
matrix:
command:
- "test:phpcs"
- "test:rector",
- "test:phpcompatibility"
- "test:phpmd"
- "test:phpstan"
Expand Down
6 changes: 4 additions & 2 deletions Classes/Cleaner/AbstractCommandCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,21 @@ abstract class AbstractCommandCleaner implements SingletonInterface, CleanerInte
*/
public function __construct()
{
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(self::class);
}

protected function executeCommand(string $command, string $parameters): bool
{
$command = CommandUtility::getCommand($command).' '.$parameters;
$output = $returnValue = '';
$output = null;
$returnValue = 0;
CommandUtility::exec($command, $output, $returnValue);
if ($returnValue) {
$this->logger->warning('exec', ['cmd' => $command, 'output' => $output, 'returnValue' => $returnValue]);

return false;
}

$this->logger->info('exec', ['cmd' => $command, 'output' => $output, 'returnValue' => $returnValue]);

return true;
Expand Down
10 changes: 2 additions & 8 deletions Classes/Cleaner/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ class Registry
*/
protected static $registeredCleaners = [];

/**
* @return void
*/
public static function registerCleaner(string $className, int $priority)
public static function registerCleaner(string $className, int $priority): void
{
if (isset(self::$registeredCleaners[$priority])) {
throw new \Exception('Priority '.$priority.' for cleaner '.$className.' already in use');
Expand All @@ -62,10 +59,7 @@ public static function registerCleaner(string $className, int $priority)
asort(self::$registeredCleaners);
}

/**
* @return void
*/
public static function unregisterCleaner(string $className)
public static function unregisterCleaner(string $className): void
{
foreach (self::$registeredCleaners as $priority => $cleaner) {
if ($cleaner instanceof $className) {
Expand Down
2 changes: 1 addition & 1 deletion Classes/Command/CleanerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CleanerCommand extends Command
{
public function __construct(
protected CleanerService $cleanerService,
protected Helper $taskHelper
protected Helper $taskHelper,
) {
parent::__construct();
}
Expand Down
14 changes: 9 additions & 5 deletions Classes/EventListener/UploadedFileEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

use DMK\Mkcleaner\Service\CleanerService;
use TYPO3\CMS\Core\Resource\Event\AfterFileAddedEvent;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/***************************************************************
* Copyright notice
Expand All @@ -55,16 +54,21 @@
***************************************************************/

/**
* MKKVS hook for uploaded file (create/delete).
* Class UploadedFileEventListener.
*
* @author Markus Crasser
* @author Hannes Bochmann
* @license http://www.gnu.org/licenses/lgpl.html
* GNU Lesser General Public License, version 3 or later
*/
class UploadedFileEventListener
{
public function cleanUp(AfterFileAddedEvent $event): void
public function __construct(
protected CleanerService $cleanerService,
) {
}

public function cleanUpFile(AfterFileAddedEvent $event): void
{
GeneralUtility::makeInstance(CleanerService::class)->cleanupFile($event->getFile());
$this->cleanerService->cleanupFile($event->getFile());
}
}
12 changes: 3 additions & 9 deletions Classes/Service/CleanerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,22 @@
use TYPO3\CMS\Core\SingletonInterface;

/**
* Class Mat2Service.
* Class CleanerService.
*
* @author Hannes Bochmann
* @license http://www.gnu.org/licenses/lgpl.html
* GNU Lesser General Public License, version 3 or later
*/
class CleanerService implements SingletonInterface
{
/**
* @return void
*/
public function cleanupFolder(Folder $folder)
public function cleanupFolder(Folder $folder): void
{
foreach ($folder->getFiles(0, 0, Folder::FILTER_MODE_USE_OWN_AND_STORAGE_FILTERS, true) as $file) {
$this->cleanupFile($file);
}
}

/**
* @return void
*/
public function cleanupFile(FileInterface $file)
public function cleanupFile(FileInterface $file): void
{
foreach (Registry::getRegisteredCleaners() as $cleaner) {
if ($cleaner->canHandleFile($file)) {
Expand Down
2 changes: 1 addition & 1 deletion Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
tags:
- name: event.listener
identifier: 'UploadedFileEventListener'
method: 'cleanUp'
method: 'cleanUpFile'
event: TYPO3\CMS\Core\Resource\Event\AfterFileAddedEvent

DMK\Mkcleaner\Command\CleanerCommand:
Expand Down
73 changes: 73 additions & 0 deletions Tests/CleanerTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* Copyright notice
*
* (c) DMK E-BUSINESS GmbH <dev@dmk-ebusiness.com>
* All rights reserved
*
* This file is part of the "mkcleaner" Extension for TYPO3 CMS.
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* GNU Lesser General Public License can be found at
* www.gnu.org/licenses/lgpl.html
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
*/

namespace DMK\Mkcleaner\Tests;

use DMK\Mkcleaner\Cleaner\AbstractCommandCleaner;
use TYPO3\CMS\Core\Log\Logger;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

/**
* Class CleanerTestCase.
*
* @author Hannes Bochmann
* @license http://www.gnu.org/licenses/lgpl.html
* GNU Lesser General Public License, version 3 or later
*/
abstract class CleanerTestCase extends UnitTestCase
{
protected bool $resetSingletonInstances = true;

protected Logger $logger;

protected string $fixturesFolder;

protected function setUp(): void
{
parent::setUp();

$this->logger = $this->getMockBuilder(Logger::class)
->disableOriginalConstructor()
->getMock();
$logManager = $this->getMockBuilder(LogManager::class)
->disableOriginalConstructor()
->getMock();
$logManager
->expects(self::once())
->method('getLogger')
->with(AbstractCommandCleaner::class)
->willReturn($this->logger);
GeneralUtility::setSingletonInstance(LogManager::class, $logManager);
$this->fixturesFolder = realpath(__DIR__.'/Fixtures');
$GLOBALS['TYPO3_CONF_VARS']['SYS']['binPath'] = '';
$GLOBALS['TYPO3_CONF_VARS']['BE']['disable_exec_function'] = false;
$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_path'] = '';
defined('LF') ?: define('LF', chr(10));
}
}
Loading

0 comments on commit 10b59ed

Please sign in to comment.