Skip to content

Commit

Permalink
Init MetadataUpdaterWorkflow.
Browse files Browse the repository at this point in the history
  • Loading branch information
zerai committed Sep 21, 2024
1 parent 9d93d35 commit dc5ea39
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
127 changes: 127 additions & 0 deletions _metadata/src/Core/Process/MetadataUpdaterWorkflow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php declare(strict_types=1);

/**
* This file is part of the medicalmundi/marketplace-engine
*
* @copyright (c) 2024 MedicalMundi
*
* This software consists of voluntary contributions made by many individuals
* {@link https://github.com/medicalmundi/marketplace-engine/graphs/contributors developer} and is licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @license https://github.com/MedicalMundi/marketplace-engine/blob/main/LICENSE MIT
*/

namespace Metadata\Core\Process;

use Ecotone\Messaging\Attribute\InternalHandler;
use Ecotone\Messaging\Attribute\Parameter\Header;
use Ecotone\Modelling\Attribute\CommandHandler;

use Ecotone\Modelling\EventBus;
use Metadata\Core\MetadataValidationEngine\MetadataValidator;
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ExternalMetadataDto;
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ForReadingExternalMetadataSource;
use Metadata\Core\ValueObject\Repository;
use Psr\Log\LoggerInterface;
use UnexpectedValueException;

class MetadataUpdaterWorkflow
{
public function __construct(
private readonly LoggerInterface $logger,
private readonly ForReadingExternalMetadataSource $metadataReader,
) {
}

#[CommandHandler(
outputChannelName: 'process.enrich'
)]
public function validateInputData(UpdateModuleMetadata $command): ?UpdateModuleMetadata
{
$this->logger->info('MetadataUpdater - initialize update process for module id: ' . $command->moduleId);

try {
$repository = Repository::createFromRepositoryUrl($command->repositoryUrl);

if ($repository->isSupported()) {
return $command;
} else {
$this->logger->info('MetadataUpdater - unsupported repository detected url: ' . $command->repositoryUrl);
}
} catch (UnexpectedValueException $exception) {
$this->logger->info('MetadataUpdater - error: ' . $exception->getMessage());
}

return null;
}

#[InternalHandler(
inputChannelName: 'process.enrich',
outputChannelName: 'process.readMetadata',
changingHeaders: true,
)]
public function enrichCommand(UpdateModuleMetadata $command): array
{
$repository = Repository::createFromRepositoryUrl($command->repositoryUrl);

return [
'repository' => $repository,
];
}

#[InternalHandler(
inputChannelName: 'process.readMetadata',
outputChannelName: 'process.validateMetadata',
changingHeaders: true,
)]
public function readMetadata(
UpdateModuleMetadata $command,
): ?array {
/**
* if no error enrich command or fail
*/
$metadataDto = $this->metadataReader->readMetadataFromExternalSource($command->repositoryUrl);

return [
'metadata_dto' => $metadataDto,
];
}

#[InternalHandler(
inputChannelName: 'process.validateMetadata',
outputChannelName: 'process.notifyProcessResult',
changingHeaders: true,
)]
public function validateMetadata(
#[Header('metadata_dto')]
ExternalMetadataDto $metadataDto,
MetadataValidator $metadataValidator
): array {
$metadataValidationResult = $metadataValidator->validate($metadataDto->toArray());

return [
'metadata_validation_result' => $metadataValidationResult,
];
}

#[InternalHandler(
inputChannelName: 'process.notifyProcessResult',
)]
public function triggerMetadataUpdate(
UpdateModuleMetadata $command,
#[Header('metadata_validation_result')]
bool $validationStatus,
#[Header('metadata_dto')]
ExternalMetadataDto $metadataDto,
EventBus $eventBus
): void {
if ($validationStatus) {
$eventBus->publish(new ModuleMetadataDetected($command->moduleId, $metadataDto));
} else {
echo 'complete with no metadata';
//$eventBus->publish(new ModuleMetadataDetected($command->moduleId, $metadataDto));
}
}
}
27 changes: 27 additions & 0 deletions _metadata/src/Core/Process/ModuleMetadataDetected.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types=1);

/**
* This file is part of the medicalmundi/marketplace-engine
*
* @copyright (c) 2024 MedicalMundi
*
* This software consists of voluntary contributions made by many individuals
* {@link https://github.com/medicalmundi/marketplace-engine/graphs/contributors developer} and is licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @license https://github.com/MedicalMundi/marketplace-engine/blob/main/LICENSE MIT
*/

namespace Metadata\Core\Process;

use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ExternalMetadataDto;

class ModuleMetadataDetected
{
public function __construct(
public readonly string $moduleId,
public readonly ExternalMetadataDto $externalMetadataDto,
) {
}
}
25 changes: 25 additions & 0 deletions _metadata/src/Core/Process/UpdateModuleMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

/**
* This file is part of the medicalmundi/marketplace-engine
*
* @copyright (c) 2024 MedicalMundi
*
* This software consists of voluntary contributions made by many individuals
* {@link https://github.com/medicalmundi/marketplace-engine/graphs/contributors developer} and is licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @license https://github.com/MedicalMundi/marketplace-engine/blob/main/LICENSE MIT
*/

namespace Metadata\Core\Process;

class UpdateModuleMetadata
{
public function __construct(
public readonly string $moduleId,
public readonly string $repositoryUrl
) {
}
}
85 changes: 85 additions & 0 deletions _metadata/tests/Unit/Core/Process/MetadataUpdaterWorkflowTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php declare(strict_types=1);

/**
* This file is part of the medicalmundi/marketplace-engine
*
* @copyright (c) 2024 MedicalMundi
*
* This software consists of voluntary contributions made by many individuals
* {@link https://github.com/medicalmundi/marketplace-engine/graphs/contributors developer} and is licensed under the MIT license.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* @license https://github.com/MedicalMundi/marketplace-engine/blob/main/LICENSE MIT
*/

namespace MetadataTests\Unit\Core\Process;

use Ecotone\Lite\EcotoneLite;
use Metadata\AdapterForReadingExternalMetadataSourceStub\StubAdapterForReadingExternalMetadataSource;
use Metadata\Core\MetadataValidationEngine\MetadataValidator;
use Metadata\Core\Port\Driven\ForReadingExternalMetadataSource\ExternalMetadataDto;
use Metadata\Core\Process\MetadataUpdaterWorkflow;
use Metadata\Core\Process\ModuleMetadataDetected;
use Metadata\Core\Process\UpdateModuleMetadata;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Ramsey\Uuid\Uuid;

#[CoversClass(MetadataUpdaterWorkflow::class)]
class MetadataUpdaterWorkflowTest extends TestCase
{
#[Test]
public function shouldNotifyASuccessfulMetadataDetection()
{
/** We could provide some Stub implementations */
$stubAdapterForReadingExternalMetadataSource = new StubAdapterForReadingExternalMetadataSource();



$ecotoneLite = EcotoneLite::bootstrapFlowTesting(
[MetadataUpdaterWorkflow::class],
[
MetadataUpdaterWorkflow::class => new MetadataUpdaterWorkflow(new NullLogger(), $stubAdapterForReadingExternalMetadataSource),
MetadataValidator::class => new MetadataValidator(),
]
);

$moduleId = Uuid::uuid4()->toString();
$repositoryUrl = 'https://github.com/MedicalMundi/oe-module-todo-list';


$aMetadataDto = new ExternalMetadataDto(
enableSync: true,
category: 'billing',
tags: ['sms', 'fax']
);

$stubAdapterForReadingExternalMetadataSource->setExternalMetadataDto($repositoryUrl, $aMetadataDto);

$expectedMessage = new ModuleMetadataDetected($moduleId, $aMetadataDto);



$this->assertEquals(
[$expectedMessage],
$ecotoneLite
->sendCommand(new UpdateModuleMetadata($moduleId, $repositoryUrl))
// no -- ->getRecordedCommandHeaders()

// no
// no -- ->getMessageChannel('module.getDefaultBranch')
// no -- ->getRecordedCommandsWithRouting()
// no -- ->getRecordedCommands()
// no -- ->getRecordedCommandHeaders()
// no -- ->getRecordedEcotoneMessagesFrom('module.getDefaultBranch')
//
// no -- ->receiveMessageFrom('module.getDefaultBranch')


->getRecordedEvents()
);
}
}

0 comments on commit dc5ea39

Please sign in to comment.