-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
_metadata/tests/Unit/Core/Process/MetadataUpdaterWorkflowTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?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(); | ||
|
||
$moduleId = Uuid::uuid4()->toString(); | ||
$repositoryUrl = 'https://github.com/MedicalMundi/oe-module-todo-list'; | ||
$aMetadataDto = new ExternalMetadataDto( | ||
enableSync: true, | ||
category: 'billing', | ||
tags: ['sms', 'fax'] | ||
); | ||
$expectedMessage = new ModuleMetadataDetected($moduleId, $aMetadataDto); | ||
$stubAdapterForReadingExternalMetadataSource->setExternalMetadataDto($repositoryUrl, $aMetadataDto); | ||
$ecotoneLite = EcotoneLite::bootstrapFlowTesting( | ||
[MetadataUpdaterWorkflow::class], | ||
[ | ||
MetadataUpdaterWorkflow::class => new MetadataUpdaterWorkflow(new NullLogger(), $stubAdapterForReadingExternalMetadataSource), | ||
MetadataValidator::class => new MetadataValidator(), | ||
] | ||
); | ||
|
||
|
||
$this->assertEquals( | ||
[$expectedMessage], | ||
$ecotoneLite | ||
->sendCommand(new UpdateModuleMetadata($moduleId, $repositoryUrl)) | ||
->getRecordedEvents() | ||
); | ||
} | ||
} |