Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions bundle/src/Comms/Async/AsyncMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Hyvor\Internal\Bundle\Comms\Async;

use Hyvor\Internal\Bundle\Comms\Event\AbstractEvent;
use Hyvor\Internal\Component\Component;
use Symfony\Component\Messenger\Attribute\AsMessage;

#[AsMessage]
class AsyncMessage {

public function __construct(
private AbstractEvent $event,
private Component $to
) {}

public function getEvent(): AbstractEvent
{
return $this->event;
}

public function getTo(): Component
{
return $this->to;
}

}
25 changes: 25 additions & 0 deletions bundle/src/Comms/Async/AsyncMessageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Hyvor\Internal\Bundle\Comms\Async;

use Hyvor\Internal\Bundle\Comms\CommsInterface;
use Hyvor\Internal\Bundle\Comms\Exception\CommsApiFailedException;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
class AsyncMessageHandler
{

public function __construct(
private CommsInterface $comms
) {}

/**
* @throws CommsApiFailedException
*/
public function __invoke(AsyncMessage $message): void
{
$this->comms->send($message->getEvent(), $message->getTo());
}

}
66 changes: 48 additions & 18 deletions bundle/src/Comms/Comms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Hyvor\Internal\Bundle\Comms;

use Hyvor\Internal\Bundle\Comms\Async\AsyncMessage;
use Hyvor\Internal\Bundle\Comms\Event\AbstractEvent;
use Hyvor\Internal\Bundle\Comms\Exception\CommsApiFailedException;
use Hyvor\Internal\Component\Component;
use Hyvor\Internal\Component\InstanceUrlResolver;
use Hyvor\Internal\InternalConfig;
use Symfony\Component\Clock\ClockAwareTrait;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\TransportNamesStamp;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
Expand All @@ -22,6 +26,7 @@ public function __construct(
private HttpClientInterface $httpClient,
private InternalConfig $internalConfig,
private InstanceUrlResolver $instanceUrlResolver,
private MessageBusInterface $bus,
) {
}

Expand All @@ -42,25 +47,8 @@ public function send(
AbstractEvent $event,
?Component $to = null,
): object|null {
$allowedFrom = $event->from();
$allowedTo = $event->to();

if ($to === null) {
if (count($allowedTo) !== 1) {
throw new \InvalidArgumentException('event to() must return exactly one component when $to is null');
}
$to = $allowedTo[0];
}

$from = $this->internalConfig->getComponent();

if (!empty($allowedFrom) && !in_array($from, $allowedFrom, true)) {
throw new \InvalidArgumentException("event is not allowed to be sent from component {$from->value}");
}

if (!empty($allowedTo) && !in_array($to, $allowedTo, true)) {
throw new \InvalidArgumentException("event is not allowed to be sent to component {$to->value}");
}
$to = $this->validateAndGetTo($event, $to);

$componentUrl = $this->instanceUrlResolver->privateUrlOf($to);
$url = $componentUrl . '/api/comms/event';
Expand Down Expand Up @@ -111,4 +99,46 @@ public function send(
// @codeCoverageIgnoreEnd
}

/**
* @throws ExceptionInterface
*/
public function sendAsync(AbstractEvent $event, ?Component $to = null, string $transport = 'async'): void
{
$to = $this->validateAndGetTo($event, $to);

$message = new AsyncMessage(
$event,
$to
);

$this->bus->dispatch($message, [
new TransportNamesStamp($transport)
]);
}

public function validateAndGetTo(AbstractEvent $event, ?Component $to): Component
{
$allowedFrom = $event->from();
$allowedTo = $event->to();

if ($to === null) {
if (count($allowedTo) !== 1) {
throw new \InvalidArgumentException('event to() must return exactly one component when $to is null');
}
$to = $allowedTo[0];
}

$from = $this->internalConfig->getComponent();

if (!empty($allowedFrom) && !in_array($from, $allowedFrom, true)) {
throw new \InvalidArgumentException("event is not allowed to be sent from component {$from->value}");
}

if (!empty($allowedTo) && !in_array($to, $allowedTo, true)) {
throw new \InvalidArgumentException("event is not allowed to be sent to component {$to->value}");
}

return $to;
}

}
2 changes: 2 additions & 0 deletions bundle/src/Comms/CommsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public function send(
?Component $to = null,
): object|null;

public function sendAsync(AbstractEvent $event, ?Component $to = null, string $transport = 'async'): void;

}
36 changes: 36 additions & 0 deletions bundle/src/Comms/Event/FromCore/Member/MemberRemoved.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Hyvor\Internal\Bundle\Comms\Event\FromCore\Member;

use Hyvor\Internal\Bundle\Comms\Event\AbstractEvent;
use Hyvor\Internal\Component\Component;

class MemberRemoved extends AbstractEvent
{

public function __construct(
private int $organizationId,
private int $userId
) {}

public function getOrganizationId(): int
{
return $this->organizationId;
}

public function getUserId(): int
{
return $this->userId;
}

public function from(): array
{
return [Component::CORE];
}

public function to(): array
{
return [];
}

}
29 changes: 29 additions & 0 deletions bundle/src/Comms/Event/FromCore/User/UserDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Hyvor\Internal\Bundle\Comms\Event\FromCore\User;

use Hyvor\Internal\Bundle\Comms\Event\AbstractEvent;
use Hyvor\Internal\Component\Component;

class UserDeleted extends AbstractEvent
{

public function __construct(
private int $userId
) {}

public function getUserId(): int
{
return $this->userId;
}

public function from(): array
{
return [Component::CORE];
}

public function to(): array
{
return [];
}
}
37 changes: 37 additions & 0 deletions bundle/src/Comms/Event/ToCore/Organization/VerifyMember.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Hyvor\Internal\Bundle\Comms\Event\ToCore\Organization;

use Hyvor\Internal\Bundle\Comms\Event\AbstractEvent;
use Hyvor\Internal\Component\Component;

/**
* @extends AbstractEvent<VerifyMemberResponse>
*/
class VerifyMember extends AbstractEvent {

public function __construct(
private int $organizationId,
private int $userId,
) {}

public function getOrganizationId(): int
{
return $this->organizationId;
}

public function getUserId(): int
{
return $this->userId;
}

public function from(): array
{
return [];
}

public function to(): array
{
return [Component::CORE];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Hyvor\Internal\Bundle\Comms\Event\ToCore\Organization;

readonly class VerifyMemberResponse {

public function __construct(
private bool $isMember,
private ?string $role,
) {}

public function isMember(): bool
{
return $this->isMember;
}

public function getRole(): ?string
{
return $this->role;
}

}
Loading
Loading