phpstan and phpcs fixes

This commit is contained in:
Dennis Eichhorn 2022-03-29 16:41:17 +02:00
parent e4f9e7447a
commit ee53a5172b
9 changed files with 125 additions and 126 deletions

View File

@ -10,7 +10,7 @@
"icon": null,
"order": 40,
"from": "Wokflow",
"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": "Wokflow",
"permission": { "permission": 2, "type": null, "element": null },
"permission": { "permission": 2, "category": null, "element": null },
"parent": 1005501001,
"children": []
},
@ -39,7 +39,7 @@
"icon": null,
"order": 5,
"from": "Wokflow",
"permission": { "permission": 2, "type": null, "element": null },
"permission": { "permission": 2, "category": null, "element": null },
"parent": 1005501001,
"children": []
},
@ -54,7 +54,7 @@
"icon": null,
"order": 10,
"from": "Wokflow",
"permission": { "permission": 4, "type": null, "element": null },
"permission": { "permission": 4, "category": null, "element": null },
"parent": 1005501001,
"children": []
}

View File

@ -112,6 +112,10 @@ final class Installer extends InstallerAbstract
$tempPath = __DIR__ . '/../../../temp/';
$workflowFiles = \scandir(__DIR__ . '/../../..' . $data['path']);
if ($workflowFiles === false) {
return;
}
foreach ($workflowFiles as $filePath) {
if (!\is_file(__DIR__ . '/../../..' . $data['path'] . '/' . $filePath) || $filePath === '..' || $filePath === '.') {
continue;
@ -131,6 +135,6 @@ final class Installer extends InstallerAbstract
]);
}
$module->apiTemplateCreate($request, $response);
$module->apiWorkflowTemplateCreate($request, $response);
}
}

View File

@ -23,6 +23,7 @@ use Modules\Workflow\Models\PermissionCategory;
use Modules\Workflow\Models\WorkflowInstance;
use Modules\Workflow\Models\WorkflowInstanceAbstract;
use Modules\Workflow\Models\WorkflowInstanceMapper;
use Modules\Workflow\Models\WorkflowStatus;
use Modules\Workflow\Models\WorkflowTemplate;
use Modules\Workflow\Models\WorkflowTemplateMapper;
use phpOMS\Account\PermissionType;
@ -50,6 +51,62 @@ use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder;
*/
final class ApiController extends Controller
{
/**
* Api method to make a call to the cli app
*
* @param mixed ...$data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function runWorkflowFromHook(...$data) : void
{
$workflows = WorkflowTemplateMapper::getAll()->where('status', WorkflowStatus::ACTIVE)->execute();
foreach ($workflows as $workflow) {
$hooks = $workflow->getHooks();
foreach ($hooks as $hook) {
$triggerIsRegex = \stripos($data[':triggerGroup'], '/') === 0;
$matched = false;
if ($triggerIsRegex) {
$matched = \preg_match($data[':triggerGroup'], $hook) === 1;
} else {
$matched = $data[':triggerGroup'] === $hook;
}
if (!$matched && \stripos($hook, '/') === 0) {
$matched = \preg_match($hook, $data[':triggerGroup']) === 1;
}
if ($matched) {
$this->runWorkflow($workflow, $hook, $data);
}
}
}
}
/**
* Api method to make a call to the cli app
*
* @param WorkflowTemplate $workflow Workflow template
* @param string $hook Event hook
* @param array $data Event data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function runWorkflow(WorkflowTemplate $workflow, string $hook, array $data) : void
{
include $workflow->source->getAbsolutePath();
}
/**
* Routing end-point for application behaviour.
*
@ -205,7 +262,7 @@ final class ApiController extends Controller
}
/**
* Create view from template
* Create view from template/instance
*
* @param WorkflowInstanceAbstract $instance Instance to create view from
* @param RequestAbstract $request Request
@ -217,11 +274,13 @@ final class ApiController extends Controller
*
* @since 1.0.0
*/
private function createView(WorkflowInstanceAbstract $instance, RequestAbstract $request, ResponseAbstract $response) : View
{
/** @var array<string, \Modules\Media\Models\Media|\Modules\Media\Models\Media[]> $tcoll */
private function createView(
WorkflowInstanceAbstract $instance,
RequestAbstract $request,
ResponseAbstract $response
) : View {
$tcoll = [];
$files = $instance->source->getSources();
$files = $instance->template->source->getSources();
/** @var \Modules\Media\Models\Media $tMedia */
foreach ($files as $tMedia) {
@ -345,7 +404,7 @@ final class ApiController extends Controller
$files[] = $upload;
}
/** @var Collection $collection */
/** @var \Modules\Media\Models\Collection $collection */
$collection = $this->app->moduleManager->get('Media')->createMediaCollectionFromMedia(
(string) ($request->getData('name') ?? ''),
(string) ($request->getData('description') ?? ''),
@ -378,6 +437,10 @@ final class ApiController extends Controller
$path = $upload->getAbsolutePath();
$content = \file_get_contents($path);
if ($content === false) {
$content = '';
}
$content = \str_replace('{workflow_id}', (string) $template->getId(), $content);
\file_put_contents($path, $content);
}
@ -385,6 +448,15 @@ final class ApiController extends Controller
$this->fillJsonResponse($request, $response, NotificationLevel::OK, 'Template', 'Template successfully created', $template);
}
/**
* Create media directory path
*
* @param WorkflowTemplate $template Workflow template
*
* @return string
*
* @since 1.0.0
*/
private function createTemplateDir(WorkflowTemplate $template) : string
{
return '/Modules/Workflow/'
@ -434,7 +506,6 @@ final class ApiController extends Controller
}
$workflowTemplate->createdBy = new NullAccount($request->header->account);
$workflowTemplate->virtualPath = (string) ($request->getData('virtualpath') ?? '/');
return $workflowTemplate;
}
@ -475,6 +546,8 @@ final class ApiController extends Controller
foreach ($definitions as $definition) {
SchemaBuilder::createFromSchema($definition, $this->app->dbPool->get('schema'))->execute();
}
return;
}
}
@ -553,6 +626,7 @@ final class ApiController extends Controller
}
}
/** @var \Modules\Workflow\Models\WorkflowControllerInterface $controller */
$instance = $controller->createInstanceFromRequest($request);
return $instance;

View File

@ -16,9 +16,11 @@ namespace Modules\Workflow\Controller;
use Modules\Media\Models\CollectionMapper;
use Modules\Media\Models\NullMedia;
use Modules\Workflow\Models\WorkflowControllerInterface;
use Modules\Workflow\Models\WorkflowInstanceMapper;
use Modules\Workflow\Models\WorkflowStatus;
use Modules\Workflow\Models\WorkflowTemplateMapper;
use Modules\Workflow\Models\WorkflowTemplateStatus;
use phpOMS\Contract\RenderableInterface;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
@ -34,60 +36,6 @@ use phpOMS\Views\View;
*/
final class BackendController extends Controller
{
/**
* Api method to make a call to the cli app
*
* @param mixed $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function runWorkflowFromHook(...$data) : void
{
$workflows = WorkflowTemplateMapper::getAll()->where('status', WorkflowStatus::ACTIVE)->execute();
foreach ($workflows as $workflow) {
$hooks = $workflow->getHooks();
foreach ($hooks as $hook) {
$triggerIsRegex = \stripos($data[':triggerGroup'], '/') === 0;
$matched = false;
if ($triggerIsRegex) {
$matched = \preg_match($data[':triggerGroup'], $hook) === 1;
} else {
$matched = $data[':triggerGroup'] === $hook;
}
if (!$matched && \stripos($hook, '/') === 0) {
$matched = \preg_match($hook, $data[':triggerGroup']) === 1;
}
if ($matched) {
$this->runWorkflow($workflow, $hook, $data);
}
}
}
}
/**
* Api method to make a call to the cli app
*
* @param mixed $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function runWorkflow(WorkflowTemplate $workflow, string $hook, array $data) : void
{
include $workflow->media->getAbsolutePath();
}
/**
* Routing end-point for application behaviour.
*
@ -270,7 +218,7 @@ final class BackendController extends Controller
/** @var WorkflowControllerInterface $controller */
$controller = new WorkflowController($this->app, $template);
$view->addData('instance', $controller->getInstanceFromRequest($request));
$view->addData('instance', $controller->createInstanceFromRequest($request));
if (!(($instance = $template->findFile('instance-profile.tpl.php')) instanceof NullMedia)) {
$view->setTemplate('/' . \substr($instance->getPath(), 0, -8));

View File

@ -39,7 +39,7 @@ final class CliController extends Controller
/**
* Api method to make a call to the cli app
*
* @param mixed $data Generic data
* @param mixed ...$data Generic data
*
* @return void
*
@ -77,7 +77,9 @@ final class CliController extends Controller
/**
* Api method to make a call to the cli app
*
* @param mixed $data Generic data
* @param WorkflowTemplate $workflow Workflow template
* @param string $hook Event hook
* @param array $data Event data
*
* @return void
*
@ -87,7 +89,7 @@ final class CliController extends Controller
*/
public function runWorkflow(WorkflowTemplate $workflow, string $hook, array $data) : void
{
include $workflow->media->getAbsolutePath();
include $workflow->source->getAbsolutePath();
}
/**
@ -153,7 +155,6 @@ final class CliController extends Controller
->execute();
$controller = null;
$mapper = null;
$files = $template->source->getSources();
foreach ($files as $tMedia) {
@ -168,6 +169,7 @@ final class CliController extends Controller
}
}
/** @var \Modules\Workflow\Models\WorkflowControllerInterface $controller */
$instance = $controller->createInstanceFromRequest($request);
$controller->createInstanceDbModel($instance);

View File

@ -1,39 +0,0 @@
<?php
/**
* Karaka
*
* PHP Version 8.0
*
* @package Modules\Workflow\Models
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://karaka.app
*/
declare(strict_types=1);
namespace Modules\Workflow\Models;
/**
* Null model
*
* @package Modules\Workflow\Models
* @license OMS License 1.0
* @link https://karaka.app
* @since 1.0.0
*/
final class NullWorkflowInstance extends WorkflowInstance
{
/**
* Constructor
*
* @param int $id Model id
*
* @since 1.0.0
*/
public function __construct(int $id = 0)
{
$this->id = $id;
parent::__construct();
}
}

View File

@ -14,7 +14,6 @@ declare(strict_types=1);
namespace Modules\Workflow\Models;
use phpOMS\Contract\RenderableInterface;
use phpOMS\Message\RequestAbstract;
use phpOMS\Message\ResponseAbstract;
@ -54,8 +53,10 @@ interface WorkflowControllerInterface
* Change workflow instance state
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param null|mixed $data Data
*
* @return WorkflowInstanceAbstract
* @return void
*
* @since 1.0.0
*/

View File

@ -25,7 +25,7 @@ use Modules\Admin\Models\NullAccount;
* @link https://karaka.app
* @since 1.0.0
*/
class WorkflowInstanceAbstract
abstract class WorkflowInstanceAbstract
{
/**
* ID.

View File

@ -67,7 +67,7 @@ class WorkflowTemplate
/**
* Status.
*
* @var string
* @var int
* @since 1.0.0
*/
public int $status = WorkflowTemplateStatus::ACTIVE;
@ -120,6 +120,15 @@ class WorkflowTemplate
return $this->id;
}
/**
* Find file by file name
*
* @param string $name File name
*
* @return Media
*
* @since 1.0.0
*/
public function findFile(string $name) : Media
{
$files = $this->source->getSources();