Skip to content

Commit

Permalink
Merge pull request #1 from arquivei/atualizacao_skeleton_php8_laravel8
Browse files Browse the repository at this point in the history
Atualizacao versao Laravel e PHP
  • Loading branch information
arq-joao-cruz authored Dec 17, 2021
2 parents bac2782 + 7cc2c5c commit 4b5469f
Show file tree
Hide file tree
Showing 49 changed files with 1,304 additions and 382 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ SASL_USERNAME=
SASL_PASSWORD=
GROUP_ID=
EVENTS_STREAM=
KAFKA_PREFIX=

# ARQUIVEI BOOTSTRAP
BOOT_SET_LOGGER_TRACE_ID_MIDDLEWARE=false
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY . /application

RUN composer install $COMPOSER_ARGS

FROM arquivei/php:7.4-cli-alpine
FROM arquivei/php:8.0-cli-alpine

WORKDIR /application

Expand Down
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Base project for Command line Workers using Laravel

## Requirements

+ PHP 7.4+
+ PHP 8.0+
+ Composer
+ Git
+ docker-composer 1.26+
Expand Down Expand Up @@ -36,3 +36,33 @@ sudo chmod -r 777 storage/
```shell script
php artisan start:worker
```

## About Example Producer

+ Variables taken from .env file or class initialization options
+ Will post in the topic:
+ KAFKA_PREFIX + EVENTS_STREAM
+ com.arquivei.stonks-events

## About Example Consumer

+ Variables taken from .env file or class initialization options
+ Will consume from the queue:
+ KAFKA_PREFIX + EVENTS_STREAM + 'topic'
+ com.arquivei.stonks-events.example-app_example-event


## Run Example Producer or Consumer

+ Edit .env with kafka information
+ With artisan run the command example:consumer then example:producer

## Run Example Integration Tests

+ Start docker compose with zookeeper, kafka and kafdrop:
```shell script
docker-compose up
```
* Run tests: ProducerCommandtTest.php and ExampleConsumerTest.php
* Go to http://localhost:9090 to see topics created in local kafka

49 changes: 49 additions & 0 deletions app/Adapters/Event/EventSenderAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace App\Adapters\Event;

use Arquivei\Events\Sender\Exceptions\EmptyExportersException;
use Arquivei\Events\Sender\Exceptions\PusherException;
use Arquivei\Events\Sender\Factories\LatestSchemaFactory;
use Arquivei\Events\Sender\Pusher;
use Arquivei\Events\Sender\Schemas\BaseSchema;
use Core\Dependencies\Event\Event;
use Core\Dependencies\Event\EventSenderInterface;

class EventSenderAdapter implements EventSenderInterface
{
private Pusher $pusher;
private string $stream;

public function __construct(
EventSenderConfig $config,
private LatestSchemaFactory $latestSchemaFactory,
) {
$this->pusher = $config->getPusher();
$this->stream = $config->getEventsStream();
}

/**
* @throws EmptyExportersException|PusherException
*/
public function push(Event $event, string $stream = null, string $key = null): void
{
if (is_null($stream)) {
$stream = $this->stream;
}
$schema = $this->buildSchema($event);
$this->pusher->push($schema, $stream, $key);
}

private function buildSchema(Event $event): BaseSchema
{
return $this->latestSchemaFactory->createFromParameters(
$event->getSource(),
$event->getType(),
$event->getDataVersion(),
$event->getData(),
);
}
}
26 changes: 26 additions & 0 deletions app/Adapters/Event/EventSenderConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace App\Adapters\Event;

use Arquivei\Events\Sender\Pusher;

class EventSenderConfig
{
public function __construct(
private Pusher $pusher,
private string $eventsStream,
) {
}

public function getPusher(): Pusher
{
return $this->pusher;
}

public function getEventsStream(): string
{
return $this->eventsStream;
}
}
37 changes: 37 additions & 0 deletions app/Adapters/Event/ExampleEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Adapters\Event;

use Core\Dependencies\Event\Event;

class ExampleEvent implements Event
{
private string $source = 'example-app';
private string $type = 'example-event';

public function __construct(
private array $data,
private int $dataVersion
) {
}

public function getData(): array
{
return $this->data;
}

public function getSource(): string
{
return $this->source;
}

public function getType(): string
{
return $this->type;
}

public function getDataVersion(): int
{
return $this->dataVersion;
}
}
42 changes: 0 additions & 42 deletions app/Adapters/EventSenderAdapter.php

This file was deleted.

48 changes: 48 additions & 0 deletions app/Adapters/Kafka/KafkaConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Adapters\Kafka;

class KafkaConfig
{
public function __construct(
private string $brokers,
private string $saslUsername,
private string $saslPassword,
private string $saslMechanism,
private string $securityProtocol,
private string $eventsStream,
) {
}

public function getBrokers(): string
{
return $this->brokers;
}

public function getSaslUsername(): string
{
return $this->saslUsername;
}

public function getSaslPassword(): string
{
return $this->saslPassword;
}

public function getSaslMechanism(): string
{
return $this->saslMechanism;
}

public function getSecurityProtocol(): string
{
return $this->securityProtocol;
}

public function getEventsStream(): string
{
return $this->eventsStream;
}
}
Loading

0 comments on commit 4b5469f

Please sign in to comment.