Skip to content

Commit

Permalink
[viera-connector-homekit-connector-bridge] Prepared viera connector t…
Browse files Browse the repository at this point in the history
…o homekit connector bridge (#306)
  • Loading branch information
actions-user committed Sep 29, 2024
1 parent 287a11d commit c69cd0d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
33 changes: 9 additions & 24 deletions src/Subscribers/ModuleEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Doctrine\Common;
use Doctrine\ORM;
use Doctrine\Persistence;
use FastyBird\Library\Application\Events as ApplicationEvents;
use FastyBird\Library\Application\Utilities as ApplicationUtilities;
use FastyBird\Library\Exchange\Documents as ExchangeDocuments;
use FastyBird\Library\Exchange\Exceptions as ExchangeExceptions;
use FastyBird\Library\Exchange\Publisher as ExchangePublisher;
Expand Down Expand Up @@ -53,10 +53,9 @@ final class ModuleEntities implements Common\EventSubscriber

private const ACTION_DELETED = 'deleted';

private bool $useAsync = false;

public function __construct(
private readonly ORM\EntityManagerInterface $entityManager,
private readonly ApplicationUtilities\EventLoopStatus $eventLoopStatus,
private readonly ExchangeDocuments\DocumentFactory $documentFactory,
private readonly ExchangePublisher\Publisher $publisher,
private readonly ExchangePublisher\Async\Publisher $asyncPublisher,
Expand All @@ -72,14 +71,10 @@ public function __construct(
public function getSubscribedEvents(): array
{
return [
0 => ORM\Events::prePersist,
1 => ORM\Events::postPersist,
2 => ORM\Events::postUpdate,
3 => ORM\Events::postRemove,

ApplicationEvents\EventLoopStarted::class => 'enableAsync',
ApplicationEvents\EventLoopStopped::class => 'disableAsync',
ApplicationEvents\EventLoopStopping::class => 'disableAsync',
ORM\Events::prePersist,
ORM\Events::postPersist,
ORM\Events::postUpdate,
ORM\Events::postRemove,
];
}

Expand Down Expand Up @@ -180,16 +175,6 @@ public function postRemove(Persistence\Event\LifecycleEventArgs $eventArgs): voi
$this->publishEntity($entity, self::ACTION_DELETED);
}

public function enableAsync(): void
{
$this->useAsync = true;
}

public function disableAsync(): void
{
$this->useAsync = false;
}

/**
* @throws ExchangeExceptions\InvalidState
* @throws MetadataExceptions\InvalidArgument
Expand Down Expand Up @@ -235,7 +220,7 @@ private function publishEntity(Entities\Entity $entity, string $action): void
}

if ($publishRoutingKey !== null) {
$this->getPublisher()->publish(
$this->getPublisher($this->eventLoopStatus->isRunning())->publish(
MetadataTypes\Sources\Module::TRIGGERS,
$publishRoutingKey,
$this->documentFactory->create(
Expand Down Expand Up @@ -263,9 +248,9 @@ private function validateNamespace(object $entity): bool
return false;
}

private function getPublisher(): ExchangePublisher\Publisher|ExchangePublisher\Async\Publisher
private function getPublisher(bool $async): ExchangePublisher\Publisher|ExchangePublisher\Async\Publisher
{
return $this->useAsync ? $this->asyncPublisher : $this->publisher;
return $async ? $this->asyncPublisher : $this->publisher;
}

}
26 changes: 17 additions & 9 deletions tests/cases/unit/Subscribers/ModuleEntitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Doctrine\ORM;
use Doctrine\Persistence;
use Exception;
use FastyBird\Library\Application\Events as ApplicationEvents;
use FastyBird\Library\Application\Utilities as ApplicationUtilities;
use FastyBird\Library\Exchange\Documents as ExchangeDocuments;
use FastyBird\Library\Exchange\Publisher as ExchangePublisher;
use FastyBird\Library\Metadata\Types as MetadataTypes;
Expand All @@ -31,22 +31,21 @@ public function testSubscriberEvents(): void

$documentFactory = $this->createMock(ExchangeDocuments\DocumentFactory::class);

$eventLoopStatus = $this->createMock(ApplicationUtilities\EventLoopStatus::class);

$subscriber = new Subscribers\ModuleEntities(
$entityManager,
$eventLoopStatus,
$documentFactory,
$publisher,
$asyncPublisher,
);

self::assertSame([
0 => ORM\Events::prePersist,
1 => ORM\Events::postPersist,
2 => ORM\Events::postUpdate,
3 => ORM\Events::postRemove,

ApplicationEvents\EventLoopStarted::class => 'enableAsync',
ApplicationEvents\EventLoopStopped::class => 'disableAsync',
ApplicationEvents\EventLoopStopping::class => 'disableAsync',
ORM\Events::prePersist,
ORM\Events::postPersist,
ORM\Events::postUpdate,
ORM\Events::postRemove,
], $subscriber->getSubscribedEvents());
}

Expand Down Expand Up @@ -113,8 +112,11 @@ public function testPublishCreatedEntity(): void
->method('create')
->willReturn($document);

$eventLoopStatus = $this->createMock(ApplicationUtilities\EventLoopStatus::class);

$subscriber = new Subscribers\ModuleEntities(
$entityManager,
$eventLoopStatus,
$documentFactory,
$publisher,
$asyncPublisher,
Expand Down Expand Up @@ -194,8 +196,11 @@ public function testPublishUpdatedEntity(): void
->method('create')
->willReturn($document);

$eventLoopStatus = $this->createMock(ApplicationUtilities\EventLoopStatus::class);

$subscriber = new Subscribers\ModuleEntities(
$entityManager,
$eventLoopStatus,
$documentFactory,
$publisher,
$asyncPublisher,
Expand Down Expand Up @@ -277,8 +282,11 @@ public function testPublishDeletedEntity(): void
->method('create')
->willReturn($document);

$eventLoopStatus = $this->createMock(ApplicationUtilities\EventLoopStatus::class);

$subscriber = new Subscribers\ModuleEntities(
$entityManager,
$eventLoopStatus,
$documentFactory,
$publisher,
$asyncPublisher,
Expand Down
2 changes: 1 addition & 1 deletion tools/phpstan.config.tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ parameters:
-
message: '#^Return type of call to method PHPUnit\\Framework\\TestCase::createMock\(\) contains unresolvable type.#'
path: ../tests/cases/unit/Subscribers/ModuleEntitiesTest.php
count: 7
count: 11

0 comments on commit c69cd0d

Please sign in to comment.