Skip to content

Commit

Permalink
Support attribute
Browse files Browse the repository at this point in the history
With this addition, it is now possible to use `#[Rebuy\Amqp\Consumer\Annotation\Consumer]` instead of a doctrine annotation.
  • Loading branch information
Spea committed Jul 22, 2023
1 parent 2d3e3a6 commit db333c7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": "^8.1",
"php-amqplib/php-amqplib": "^3.0.0",
"doctrine/annotations": "^1.10|^2.0.1",
"doctrine/annotations": "^1.13.3|^2.0.1",
"symfony/event-dispatcher": "^4.4|^5.0|^6.0",
"doctrine/collections": "^1.6|^2.1.2"
},
Expand Down
21 changes: 10 additions & 11 deletions src/Rebuy/Amqp/Consumer/Annotation/Consumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

namespace Rebuy\Amqp\Consumer\Annotation;

use Attribute;

/**
* @Annotation
* @Target({"METHOD"})
* @NamedArgumentConstructor
*/
#[Attribute(Attribute::IS_REPEATABLE | Attribute::TARGET_METHOD)]
class Consumer
{
const DEFAULT_PREFETCH_COUNT = 1;

/**
* @Required
* @var string
*/
public $name;
private const DEFAULT_PREFETCH_COUNT = 1;

/**
* @var int
*/
public $prefetchCount = self::DEFAULT_PREFETCH_COUNT;
public function __construct(
public string $name,
public int $prefetchCount = self::DEFAULT_PREFETCH_COUNT
) {
}
}
16 changes: 14 additions & 2 deletions src/Rebuy/Amqp/Consumer/Annotation/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public function getConsumerMethods($obj)
$class = new ReflectionClass($obj);
$consumerMethods = [];
foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
/** @var Consumer $annotation */
$annotation = $this->reader->getMethodAnnotation($method, Consumer::class);
$annotation = $this->getConsumerAnnotationOrAttribute($method);
if (null === $annotation) {
continue;
}
Expand Down Expand Up @@ -70,4 +69,17 @@ private function validateMethod(ReflectionMethod $method)
throw new InvalidArgumentException('A @Consumer\'s parameter must implement ' . MessageInterface::class);
}
}

private function getConsumerAnnotationOrAttribute(ReflectionMethod $method): ?Consumer
{
$reflectionAttributes = $method->getAttributes();
foreach ($reflectionAttributes as $attribute) {
if ($attribute->getName() === Consumer::class) {
return $attribute->newInstance();
}
}

return $this->reader->getMethodAnnotation($method, Consumer::class);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function invoke_should_invoke_reflection()
self::TEST_PREFIX,
$consumer,
$reflectionMethod->reveal(),
new ConsumerAnnotation()
new ConsumerAnnotation('name')
);
$container->invoke($payload);
}
Expand All @@ -46,7 +46,7 @@ public function get_bindings_should_return_empty_array_if_interface_is_not_imple
$consumer = new ConsumerWithInvalidParameter();
$method = new ReflectionMethod($consumer, 'classWithoutImplementingInterface');

$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation());
$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation('name'));
$result = $container->getBindings();

verify($result)->empty();
Expand All @@ -60,12 +60,26 @@ public function get_bindings_should_return_empty_array_if_parameter_count_is_not
$consumer = new ConsumerWithTwoParameters();
$method = new ReflectionMethod($consumer, 'consume');

$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation());
$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation('name'));
$result = $container->getBindings();

verify($result)->empty();
}

/**
* @test
*/
public function getRoutingKey_should_return_null_if_the_class_does_not_implement_the_MessageInterface()
{
$consumer = new ConsumerWithInvalidParameter();
$method = new ReflectionMethod($consumer, 'classWithoutImplementingInterface');

$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation('name'));
$result = $container->getRoutingKey();

verify($result)->empty();
}

/**
* @test
*/
Expand All @@ -74,7 +88,7 @@ public function get_bindings_should_return_empty_array_parameter_is_not_a_class(
$consumer = new ConsumerWithInvalidParameter();
$method = new ReflectionMethod($consumer, 'consume');

$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation());
$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation('name'));
$result = $container->getBindings();

verify($result)->empty();
Expand All @@ -88,7 +102,7 @@ public function get_bindings_should_return_array_with_two_bindings()
$consumer = new Consumer();
$method = new ReflectionMethod($consumer, 'consume');

$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation());
$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, new ConsumerAnnotation('name'));
$result = $container->getBindings();

verify($result)->notEmpty();
Expand All @@ -103,7 +117,7 @@ public function get_bindings_should_return_correct_bindings()
$consumer = new Consumer();
$method = new ReflectionMethod($consumer, 'consume');

$consumerAnnotation = new ConsumerAnnotation();
$consumerAnnotation = new ConsumerAnnotation('name');
$consumerAnnotation->name = "consume-method";
$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, $consumerAnnotation);
$result = $container->getBindings();
Expand All @@ -120,7 +134,7 @@ public function get_consumer_name_should_return_correct_name()
$consumer = new Consumer();
$method = new ReflectionMethod($consumer, 'consume');

$consumerAnnotation = new ConsumerAnnotation();
$consumerAnnotation = new ConsumerAnnotation('name');
$consumerAnnotation->name = "consume-method";
$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, $consumerAnnotation);
$result = $container->getConsumerName();
Expand All @@ -136,7 +150,7 @@ public function get_method_name_should_return_class_with_method_name()
$consumer = new Consumer();
$method = new ReflectionMethod($consumer, 'consume');

$consumerAnnotation = new ConsumerAnnotation();
$consumerAnnotation = new ConsumerAnnotation('name');
$container = new ConsumerContainer(self::TEST_PREFIX, $consumer, $method, $consumerAnnotation);

$result = $container->getMethodName();
Expand Down
18 changes: 17 additions & 1 deletion tests/Rebuy/Tests/Amqp/Consumer/Annotation/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Rebuy\Amqp\Consumer\Annotation\Consumer as ConsumerAnnotation;
use Rebuy\Amqp\Consumer\Annotation\Parser;
use Rebuy\Tests\Amqp\Consumer\Stubs\Consumer;
use Rebuy\Tests\Amqp\Consumer\Stubs\ConsumerWithAttributes;
use Rebuy\Tests\Amqp\Consumer\Stubs\ConsumerWithInvalidAnnotation;
use Rebuy\Tests\Amqp\Consumer\Stubs\ConsumerWithInvalidParameter;
use Rebuy\Tests\Amqp\Consumer\Stubs\ConsumerWithPrefetchCount;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function parser_should_use_default_prefetch_count()
}

/**
* @tests
* @test
*/
public function parser_should_use_prefetch_count_from_annotation()
{
Expand All @@ -62,6 +63,21 @@ public function parser_should_use_prefetch_count_from_annotation()
verify($consumerMethod->getPrefetchCount())->equals(100);
}

/**
* @test
*/
public function parser_should_support_attributes()
{
$parser = new Parser(new AnnotationReader(), 'prefix');
$consumer = new ConsumerWithAttributes();

$consumerMethods = $parser->getConsumerMethods($consumer);
$consumerMethod = $consumerMethods[0];

verify($consumerMethod->getConsumerName())->equals('prefix-consume-with-attributes');
verify($consumerMethod->getPrefetchCount())->equals(100);
}

/**
* @test
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/Rebuy/Tests/Amqp/Consumer/Stubs/ConsumerWithAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rebuy\Tests\Amqp\Consumer\Stubs;

class ConsumerWithAttributes
{
#[\Rebuy\Amqp\Consumer\Annotation\Consumer(name: 'consume-with-attributes', prefetchCount: 100)]
public function consume(Message $message)
{
}
}

0 comments on commit db333c7

Please sign in to comment.