Skip to content

Commit

Permalink
F/amqp util (#51)
Browse files Browse the repository at this point in the history
* added CreateAmqpSchemaTask

* added RegisterAmqpConsumerTask

* added AmqpChannelWrapperProvider
  • Loading branch information
Mararok committed Jun 27, 2018
1 parent c12c787 commit 83dda8b
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/SAREhub/Client/Amqp/AmqpChannelWrapperProvider.php
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);
}
}
36 changes: 36 additions & 0 deletions src/SAREhub/Client/Amqp/Task/CreateAmqpSchemaTask.php
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);
}
}
32 changes: 32 additions & 0 deletions src/SAREhub/Client/Amqp/Task/RegisterAmqpConsumerTask.php
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 tests/unit/SAREhub/Client/Amqp/Task/CreateAmqpSchemaTaskTest.php
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();
}
}
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();
}
}

0 comments on commit 83dda8b

Please sign in to comment.