55namespace App \Infrastructure \Command ;
66
77use App \Domain \user \Entity \User ;
8+ use App \Infrastructure \Avro \Interfaces \SchemaRegistryClientInterface ;
89use AvroSchema ;
910use Doctrine \ORM \EntityManagerInterface ;
10- use FlixTech \AvroSerializer \Objects \RecordSerializer ;
11- use FlixTech \SchemaRegistryApi \Registry \Cache \AvroObjectCacheAdapter ;
12- use FlixTech \SchemaRegistryApi \Registry \CachedRegistry ;
13- use FlixTech \SchemaRegistryApi \Registry \PromisingRegistry ;
14- use GuzzleHttp \Client ;
1511use Jobcloud \Kafka \Consumer \KafkaConsumerBuilder ;
1612use Jobcloud \Kafka \Exception \KafkaConsumerConsumeException ;
1713use Jobcloud \Kafka \Exception \KafkaConsumerEndOfPartitionException ;
1814use Jobcloud \Kafka \Exception \KafkaConsumerTimeoutException ;
1915use Jobcloud \Kafka \Message \Decoder \AvroDecoder ;
20- use Jobcloud \Kafka \Message \Encoder \AvroEncoder ;
21- use Jobcloud \Kafka \Message \Registry \AvroSchemaRegistry ;
16+ use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
2217use Symfony \Component \Console \Attribute \AsCommand ;
2318use Symfony \Component \Console \Command \Command ;
2419use Symfony \Component \Console \Input \InputArgument ;
3227)]
3328class UserConsumerCommand extends Command
3429{
35- private EntityManagerInterface $ entityManager ;
36-
37- public function __construct (EntityManagerInterface $ entityManager , string $ name = null )
38- {
30+ public function __construct (
31+ private EntityManagerInterface $ entityManager ,
32+ protected readonly SchemaRegistryClientInterface $ schemaRegistryClient ,
33+ string $ name = null
34+ ) {
3935 $ this ->entityManager = $ entityManager ;
4036 parent ::__construct ($ name );
4137 }
@@ -49,23 +45,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4945 {
5046 $ io = new SymfonyStyle ($ input , $ output );
5147
52- $ schemaRegistryClient = new CachedRegistry (
53- new PromisingRegistry (
54- new Client (['base_uri ' => 'schema-registry:8081 ' ])
55- ),
56- new AvroObjectCacheAdapter ()
57- );
58-
59- $ registry = new AvroSchemaRegistry ($ schemaRegistryClient );
60-
61- $ recordSerializer = new RecordSerializer (
62- $ schemaRegistryClient ,
63- [
64- RecordSerializer::OPTION_REGISTER_MISSING_SCHEMAS => false ,
65- RecordSerializer::OPTION_REGISTER_MISSING_SUBJECTS => true ,
66- ]
67- );
68-
6948 $ schema = <<<'JSON'
7049 {
7150 "type": "record",
@@ -80,15 +59,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8059
8160 $ avroSchema = AvroSchema::parse ($ schema );
8261
83- $ decoder = new AvroDecoder ($ registry , $ recordSerializer );
84-
8562 $ consumer = KafkaConsumerBuilder::create ()
8663 ->withAdditionalConfig (
8764 [
8865 'enable.auto.commit ' => false ,
8966 ]
9067 )
91- ->withDecoder ($ decoder )
68+ ->withDecoder ($ this -> schemaRegistryClient -> getDecoder () )
9269 ->withAdditionalBroker ('kafka:9092 ' )
9370 ->withConsumerGroup ('testGroup ' )
9471 ->withAdditionalSubscription ('users ' )
@@ -100,7 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
10077 try {
10178 $ message = $ consumer ->consume ();
10279
103- $ userData = $ recordSerializer ->decodeMessage ($ message ->getBody (), $ avroSchema );
80+ $ userData = $ this -> schemaRegistryClient -> getRecordSerializer () ->decodeMessage ($ message ->getBody (), $ avroSchema );
10481
10582 $ io ->success ('Message received: ' . $ userData ['name ' ]);
10683
0 commit comments