-
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.
* added CreateAmqpSchemaTask * added RegisterAmqpConsumerTask * added AmqpChannelWrapperProvider
- Loading branch information
Showing
5 changed files
with
152 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,38 @@ | ||
<?php | ||
|
||
|
||
namespace SAREhub\Service\Throttler\Amqp; | ||
|
||
|
||
use PhpAmqpLib\Channel\AMQPChannel; | ||
use SAREhub\Client\Amqp\AmqpChannelWrapper; | ||
use SAREhub\Commons\Misc\EnvironmentHelper; | ||
use SAREhub\Commons\Misc\InvokableProvider; | ||
|
||
class AmqpChannelWrapperProvider extends InvokableProvider | ||
{ | ||
const ENV_PREFETCH_COUNT = "AMQP_PREFETCH_COUNT"; | ||
const DEFAULT_PREFETCH_COUNT = 5; | ||
|
||
/** | ||
* @var AMQPChannel | ||
*/ | ||
private $channel; | ||
|
||
public function __construct(AMQPChannel $channel) | ||
{ | ||
$this->channel = $channel; | ||
} | ||
|
||
public function get() | ||
{ | ||
$wrapper = new AmqpChannelWrapper($this->channel); | ||
$wrapper->setPrefetchCountPerConsumer($this->getPrefetchCountFromEnv()); | ||
return $wrapper; | ||
} | ||
|
||
private function getPrefetchCountFromEnv(): int | ||
{ | ||
return EnvironmentHelper::getVar(self::ENV_PREFETCH_COUNT, self::DEFAULT_PREFETCH_COUNT); | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace SAREhub\Client\Amqp\Task; | ||
|
||
|
||
use SAREhub\Client\Amqp\AmqpEnvironmentManager; | ||
use SAREhub\Client\Amqp\AmqpEnvironmentSchema; | ||
use SAREhub\Client\Amqp\AmqpSchemaException; | ||
use SAREhub\Commons\Task\Task; | ||
|
||
class CreateAmqpSchemaTask implements Task | ||
{ | ||
/** | ||
* @var AmqpEnvironmentManager | ||
*/ | ||
private $manager; | ||
|
||
/** | ||
* @var AmqpEnvironmentSchema | ||
*/ | ||
private $schema; | ||
|
||
public function __construct(AmqpEnvironmentManager $manager, AmqpEnvironmentSchema $schema) | ||
{ | ||
$this->manager = $manager; | ||
$this->schema = $schema; | ||
} | ||
|
||
/** | ||
* @throws AmqpSchemaException | ||
*/ | ||
public function run() | ||
{ | ||
$this->manager->create($this->schema); | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace SAREhub\Client\Amqp\Task; | ||
|
||
use SAREhub\Client\Amqp\AmqpChannelWrapper; | ||
use SAREhub\Client\Amqp\AmqpConsumer; | ||
use SAREhub\Commons\Task\Task; | ||
|
||
|
||
class RegisterAmqpConsumerTask implements Task | ||
{ | ||
/** | ||
* @var AmqpChannelWrapper | ||
*/ | ||
private $channel; | ||
|
||
/** | ||
* @var AmqpConsumer | ||
*/ | ||
private $consumer; | ||
|
||
public function __construct(AmqpChannelWrapper $channel, AmqpConsumer $consumer) | ||
{ | ||
$this->channel = $channel; | ||
$this->consumer = $consumer; | ||
} | ||
|
||
public function run() | ||
{ | ||
$this->channel->registerConsumer($this->consumer); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/unit/SAREhub/Client/Amqp/Task/CreateAmqpSchemaTaskTest.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,23 @@ | ||
<?php | ||
|
||
namespace SAREhub\Client\Amqp\Task; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use PHPUnit\Framework\TestCase; | ||
use SAREhub\Client\Amqp\AmqpEnvironmentManager; | ||
use SAREhub\Client\Amqp\AmqpEnvironmentSchema; | ||
|
||
class CreateAmqpSchemaTaskTest extends TestCase | ||
{ | ||
use MockeryPHPUnitIntegration; | ||
|
||
public function testRun() | ||
{ | ||
$schemaManager = \Mockery::mock(AmqpEnvironmentManager::class); | ||
$schema = new AmqpEnvironmentSchema(); | ||
$task = new CreateAmqpSchemaTask($schemaManager, $schema); | ||
|
||
$schemaManager->expects("create")->with($schema); | ||
$task->run(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/unit/SAREhub/Client/Amqp/Task/RegisterAmqpConsumerTaskTest.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,23 @@ | ||
<?php | ||
|
||
namespace SAREhub\Client\Amqp\Task; | ||
|
||
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; | ||
use PHPUnit\Framework\TestCase; | ||
use SAREhub\Client\Amqp\AmqpChannelWrapper; | ||
use SAREhub\Client\Amqp\AmqpConsumer; | ||
|
||
class RegisterAmqpConsumerTaskTest extends TestCase | ||
{ | ||
use MockeryPHPUnitIntegration; | ||
|
||
public function testRun() | ||
{ | ||
$channel = \Mockery::mock(AmqpChannelWrapper::class); | ||
$consumer = \Mockery::mock(AmqpConsumer::class); | ||
$task = new RegisterAmqpConsumerTask($channel, $consumer); | ||
|
||
$channel->expects("registerConsumer")->with($consumer); | ||
$task->run(); | ||
} | ||
} |