From 7d72ca78000294e155a0d9c9eb56a2867052a9e2 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 27 Jan 2023 22:12:09 +0100 Subject: [PATCH] phpstan, phpcs, phpunit fixes --- Controller/ApiController.php | 49 +++++++++---- Models/ResourceMapper.php | 2 +- Theme/Backend/Lang/Navigation.de.lang.php | 17 +++++ Theme/Backend/Lang/de.lang.php | 86 +++++++++++++++++++++++ 4 files changed, 138 insertions(+), 16 deletions(-) create mode 100644 Theme/Backend/Lang/Navigation.de.lang.php create mode 100755 Theme/Backend/Lang/de.lang.php diff --git a/Controller/ApiController.php b/Controller/ApiController.php index 4665c93..f148abf 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -15,21 +15,21 @@ declare(strict_types=1); namespace Modules\OnlineResourceWatcher\Controller; use Modules\Admin\Models\NullAccount; +use Modules\OnlineResourceWatcher\Models\Report; +use Modules\OnlineResourceWatcher\Models\ReportMapper; +use Modules\OnlineResourceWatcher\Models\ReportStatus; use Modules\OnlineResourceWatcher\Models\Resource; use Modules\OnlineResourceWatcher\Models\ResourceMapper; -use Modules\OnlineResourceWatcher\Models\Report; -use Modules\OnlineResourceWatcher\Models\ReportStatus; -use Modules\OnlineResourceWatcher\Models\ReportMapper; use Modules\OnlineResourceWatcher\Models\ResourceStatus; use phpOMS\Message\Http\RequestStatusCode; use phpOMS\Message\NotificationLevel; use phpOMS\Message\RequestAbstract; use phpOMS\Message\ResponseAbstract; use phpOMS\Model\Message\FormValidation; -use phpOMS\System\SystemUtils; use phpOMS\System\File\Local\Directory; -use phpOMS\Utils\StringUtils; +use phpOMS\System\SystemUtils; use phpOMS\Utils\ImageUtils; +use phpOMS\Utils\StringUtils; /** * OnlineResourceWatcher controller class. @@ -132,6 +132,7 @@ final class ApiController extends Controller */ public function apiCheckResources(RequestAbstract $request, ResponseAbstract $response, mixed $data = null) : void { + /** @var Resource[] $resources */ $resources = ResourceMapper::getAll() ->where('status', ResourceStatus::ACTIVE) ->execute(); @@ -182,7 +183,7 @@ final class ApiController extends Controller $report = new Report(); $report->resource = $resource->getId(); $report->versionPath = (string) $check['timestamp']; - $report->status = ReportStatus::DOWNLOAD_ERROR; + $report->status = ReportStatus::DOWNLOAD_ERROR; ReportMapper::create()->execute($report); @@ -206,9 +207,12 @@ final class ApiController extends Controller // new resource if ($check['loop'] === 60 * 10 && !\is_dir($basePath . '/' . $id)) { $filesNew = \scandir($path); + if ($filesNew === false) { + $filesNew = []; + } $fileName = ''; - if (in_array('index.htm', $filesNew) || \in_array('index.html', $filesNew)) { + if (\in_array('index.htm', $filesNew) || \in_array('index.html', $filesNew)) { $fileName = \in_array('index.htm', $filesNew) ? 'index.htm' : 'index.html'; } else { foreach ($filesNew as $file) { @@ -248,7 +252,7 @@ final class ApiController extends Controller $resource->lastVersionPath = (string) $check['timestamp']; $resource->lastVersionDate = $report->createdAt; $resource->hash = $hash; - $resource->checkedAt = $report->createdAt; + $resource->checkedAt = $report->createdAt; ResourceMapper::update()->execute($resource); Directory::copy($path, $basePath . '/' . $id . '/' . $check['timestamp']); @@ -263,13 +267,17 @@ final class ApiController extends Controller // existing resource $resourcePaths = \scandir($basePath . '/' . $id); + if ($resourcePaths === false) { + $resourcePaths = []; + } + \natsort($resourcePaths); $lastVersionTimestamp = \end($resourcePaths); - if ($lastVersionTimestamp === '.' && $lastVersionTimestamp === '..') { + if ($lastVersionTimestamp === '.' || $lastVersionTimestamp === '..') { $lastVersionTimestamp = \reset($resourcePaths); - if ($lastVersionTimestamp === '.' && $lastVersionTimestamp === '..') { + if ($lastVersionTimestamp === '.' || $lastVersionTimestamp === '..') { Directory::delete($basePath . '/' . $id); } } @@ -277,9 +285,15 @@ final class ApiController extends Controller $lastVersionPath = $basePath . '/' . $id . '/' . $lastVersionTimestamp; $filesNew = \scandir($path); + if ($filesNew === false) { + $filesNew = []; + } // Using this because the index.htm gets created last and at the time of the check below it may not yet exist. $filesOld = \scandir($lastVersionPath); + if ($filesOld === false) { + $filesOld = []; + } $oldPath = ''; $newPath = ''; @@ -322,8 +336,13 @@ final class ApiController extends Controller continue; } - $md5Old = $resource->hash; - $md5New = \md5_file($newPath); + $md5Old = $resource->hash; + $md5New = \md5_file($newPath); + + if ($md5New === false) { + $md5New = ''; + } + $hasDifferentHash = $md5Old !== $md5New; // @todo: check if old path exists and if not, don't calculate a diff @@ -331,12 +350,12 @@ final class ApiController extends Controller $difference = 0; if ($hasDifferentHash) { if (\in_array($extension, ['md', 'txt', 'doc', 'docx', 'pdf', 'xls', 'xlsx'])) { - $contentOld = \Modules\Media\Controller\ApiController::loadFileContent($oldPath); - $contentNew = \Modules\Media\Controller\ApiController::loadFileContent($newPath); + $contentOld = \Modules\Media\Controller\ApiController::loadFileContent($oldPath, $extension); + $contentNew = \Modules\Media\Controller\ApiController::loadFileContent($newPath, $extension); $difference = \levenshtein($contentOld, $contentNew); } elseif (\in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) { - $difference = ImageUtils::difference($oldPath, $newPath, $path . '/_' . $file, 0); + $difference = ImageUtils::difference($oldPath, $newPath, $path . '/_' . \basename($newPath), 0); } } diff --git a/Models/ResourceMapper.php b/Models/ResourceMapper.php index 1aa3d1e..d1a2a66 100755 --- a/Models/ResourceMapper.php +++ b/Models/ResourceMapper.php @@ -52,7 +52,7 @@ final class ResourceMapper extends DataMapperFactory /** * Belongs to. * - * @var array + * @var array * @since 1.0.0 */ public const BELONGS_TO = [ diff --git a/Theme/Backend/Lang/Navigation.de.lang.php b/Theme/Backend/Lang/Navigation.de.lang.php new file mode 100644 index 0000000..a1eca9f --- /dev/null +++ b/Theme/Backend/Lang/Navigation.de.lang.php @@ -0,0 +1,17 @@ + [ + 'OnlineResourceWatcher' => 'Online Resource Watcher', +]]; diff --git a/Theme/Backend/Lang/de.lang.php b/Theme/Backend/Lang/de.lang.php new file mode 100755 index 0000000..9133ed5 --- /dev/null +++ b/Theme/Backend/Lang/de.lang.php @@ -0,0 +1,86 @@ + 'Action', + 'Add' => 'Add', + 'Admin' => 'Admin', + 'Back' => 'Back', + 'Bills' => 'Bills', + 'By' => 'By', + 'Cancel' => 'Cancel', + 'Checked' => 'Checked', + 'Dashboard' => 'Dashboard', + 'Date' => 'Date', + 'Filter' => 'Filter', + 'ForgotPassword' => 'Forgot Password', + 'Home' => 'Home', + 'ID' => 'ID', + 'Imprint' => 'Imprint', + 'Legal' => 'Legal', + 'Log' => 'Log', + 'Logo' => 'Logo', + 'SignOut' => 'Sign Out', + 'Logs' => 'Logs', + 'Organization' => 'Organization', + 'Organizations' => 'Organizations', + 'Password' => 'Password', + 'PrivacyPolicy' => 'Privacy Policy', + 'Ref' => 'Ref', + 'Report' => 'Report', + 'Reports' => 'Reports', + 'Reset' => 'Reset', + 'Resource' => 'Resource', + 'Resources' => 'Resources', + 'Create' => 'Create', + 'Delete' => 'Delete', + 'Suspend' => 'Suspend', + 'Send' => 'Send', + 'Save' => 'Save', + 'Search' => 'Search', + 'Settings' => 'Settings', + 'SignIn' => 'Sign In', + 'Status' => 'Status', + 'Submit' => 'Submit', + 'Terms' => 'Terms', + 'Trigger' => 'Trigger', + 'Type' => 'Type', + 'User' => 'User', + 'Username' => 'Username', + 'Users' => 'Users', + 'New' => 'New', + 'Number' => 'Number', + 'CustomerNo' => 'Customer No.', + 'CustomerName' => 'Customer Name', + 'Amount' => 'Amount', + 'Members' => 'Members', + 'Name' => 'Name', + 'Active' => 'Active', + 'Address' => 'Address', + 'Postal' => 'Postal', + 'City' => 'City', + 'BillingEmail' => 'Billing Email', + 'Statistics' => 'Statistics', + 'PlanSettings' => 'Plan Settings', + 'BillingSettings' => 'Billing Settings', + 'UserSettings' => 'User Settings', + 'Login' => 'Login', + 'Email' => 'Email', + 'NewPassword' => 'New Password', + 'CreateResource' => 'Create Resource', + 'Inform' => 'Inform', + 'Element' => 'Element', + 'Exit' => 'Exit', + 'Url' => 'Url', +]];