phpstan, phpcs, phpunit fixes

This commit is contained in:
Dennis Eichhorn 2023-01-27 22:12:09 +01:00
parent 8f7b0eb7c4
commit 7d72ca7800
4 changed files with 138 additions and 16 deletions

View File

@ -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);
}
}

View File

@ -52,7 +52,7 @@ final class ResourceMapper extends DataMapperFactory
/**
* Belongs to.
*
* @var array<string, array{mapper:string, external:string, column?:string, by?:string}>
* @var array<string, array{mapper:class-string, external:string, column?:string, by?:string}>
* @since 1.0.0
*/
public const BELONGS_TO = [

View File

@ -0,0 +1,17 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Modules\Localization
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
return ['Navigation' => [
'OnlineResourceWatcher' => 'Online Resource Watcher',
]];

86
Theme/Backend/Lang/de.lang.php Executable file
View File

@ -0,0 +1,86 @@
<?php
/**
* Karaka
*
* PHP Version 8.1
*
* @package Modules\OnlineResourceWatcher
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://jingga.app
*/
declare(strict_types=1);
return [[
'Action' => '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',
]];