Skip to content

Commit

Permalink
Chat feature and CQRS - messegnger Initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
nkamuo committed Sep 30, 2023
1 parent f6ecce1 commit 7aee0dd
Show file tree
Hide file tree
Showing 141 changed files with 5,712 additions and 23 deletions.
12 changes: 12 additions & 0 deletions config/graphql/types/Scalar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# VehicleStatus:
# type: enum
# config:
# enumClass: App\Entity\Transport\Vehicle\VehicleStatus
# # values:
# # GREEN:
# # description: "The color green"

UploadFile:
type: custom-scalar
config:
scalarType: '@=newObject("Overblog\\GraphQLBundle\\Upload\\Type\\GraphQLUploadType")'
6 changes: 5 additions & 1 deletion config/packages/graphql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ overblog_graphql:
driver:
query: DriverQuery
mutation: DriverMutation

admin:
query: AdminQuery
mutation: AdminMutation
Expand All @@ -33,3 +33,7 @@ overblog_graphql:
- type: attribute
dir: "%kernel.project_dir%/src/GraphQL"
suffix: null

- type: attribute
dir: "%kernel.project_dir%/src/CQRS"
suffix: null
37 changes: 37 additions & 0 deletions migrations/Version20230930081522.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230930081522 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE shipment_order_shipment_document (shipment_order_id BINARY(16) NOT NULL COMMENT \'(DC2Type:ulid)\', shipment_document_id BINARY(16) NOT NULL COMMENT \'(DC2Type:ulid)\', INDEX IDX_4D2269C72BC89259 (shipment_order_id), INDEX IDX_4D2269C7EC01E0A3 (shipment_document_id), PRIMARY KEY(shipment_order_id, shipment_document_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE shipment_order_shipment_document ADD CONSTRAINT FK_4D2269C72BC89259 FOREIGN KEY (shipment_order_id) REFERENCES shipment_order (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE shipment_order_shipment_document ADD CONSTRAINT FK_4D2269C7EC01E0A3 FOREIGN KEY (shipment_document_id) REFERENCES shipment_document (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE shipment_document ADD meta LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\', ADD type VARCHAR(32) NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE shipment_order_shipment_document DROP FOREIGN KEY FK_4D2269C72BC89259');
$this->addSql('ALTER TABLE shipment_order_shipment_document DROP FOREIGN KEY FK_4D2269C7EC01E0A3');
$this->addSql('DROP TABLE shipment_order_shipment_document');
$this->addSql('ALTER TABLE shipment_document DROP meta, DROP type');
}
}
31 changes: 31 additions & 0 deletions migrations/Version20230930084905.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230930084905 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE shipment_document_attachment CHANGE src src VARCHAR(225) NOT NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE shipment_document_attachment CHANGE src src VARCHAR(64) NOT NULL');
}
}
6 changes: 6 additions & 0 deletions src/CQRS/Command/EntityCreationCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace App\CQRS\Command;

interface EntityCreationCommandInterface extends EntityMutationCommandInterface{

}
24 changes: 24 additions & 0 deletions src/CQRS/Command/EntityCreationCommandTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace App\CQRS\Command;
use Symfony\Component\Uid\Ulid;



/**
* @deprecated 0.0.0
*/
trait EntityCreationCommandTrait{
private ?Ulid $uid = null;


public function getUid(): ?Ulid{
return $this->uid;
}

public function setUid(?Ulid $uid): static{
$this->uid = $uid;
return $this;
}


}
11 changes: 11 additions & 0 deletions src/CQRS/Command/EntityMutationCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace App\CQRS\Command;
use App\CQRS\MessageInterface;
use Symfony\Component\Uid\Ulid;

interface EntityMutationCommandInterface extends MessageInterface{

public function getUid(): ?Ulid;

public function setUid(?Ulid $uid): static;
}
21 changes: 21 additions & 0 deletions src/CQRS/Command/EntityMutationCommandTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace App\CQRS\Command;
use Symfony\Component\Uid\Ulid;



trait EntityMutationCommandTrait{
private ?Ulid $uid = null;


public function getUid(): ?Ulid{
return $this->uid;
}

public function setUid(?Ulid $uid): static{
$this->uid = $uid;
return $this;
}


}
6 changes: 6 additions & 0 deletions src/CQRS/Command/EntityUpdateCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace App\CQRS\Command;

interface EntityUpdateCommandInterface extends EntityMutationCommandInterface{

}
36 changes: 36 additions & 0 deletions src/CQRS/Command/MutationCommandContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace App\CQRS\Command;
use App\CQRS\Command\Result\CQRSEntityMutationResult;
use App\CQRS\Command\Result\CQRSEntryCreationCommandResult;
use App\CQRS\Command\Result\CQRSEntryUpdateCommandResult;
use App\CQRS\CommandBusInterface;
use Symfony\Component\Uid\Ulid;
class MutationCommandContext{


private ?CQRSEntityMutationResult $result = null;

public function __construct(private CommandBusInterface $commandBus, private ?string $clientMuationId = null){

}


public function dispatch(EntityMutationCommandInterface $command, array $stamps = []): CQRSEntityMutationResult{

if(!($uid = $command->getUid())){
$uid = new Ulid();
$command->setUid($uid);
}

$this->commandBus->dispatch($command, $stamps);

return $this->result ??= (
($command instanceof EntityCreationCommandInterface) ?
new CQRSEntryCreationCommandResult($uid, $this->clientMuationId)
:
new CQRSEntryUpdateCommandResult($uid, $this->clientMuationId)
);

}

}
22 changes: 22 additions & 0 deletions src/CQRS/Command/Result/CQRSCommandResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
namespace App\CQRS\Command\Result;
use Overblog\GraphQLBundle\Annotation\Field;
use Overblog\GraphQLBundle\Annotation\Type;


#[Type]
class CQRSCommandResult{

#[Field()]
private ?string $clientMutationId;


public function __construct(?string $clientMutationId){
$this->clientMutationId = $clientMutationId;
}

public function getClientMutationId(): ?string{
return $this->clientMutationId;
}

}
32 changes: 32 additions & 0 deletions src/CQRS/Command/Result/CQRSEntityMutationResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace App\CQRS\Command\Result;

use Overblog\GraphQLBundle\Annotation\Deprecated;
use Overblog\GraphQLBundle\Annotation\Field;
use Overblog\GraphQLBundle\Annotation\Type;
use Symfony\Component\Uid\Ulid;

#[Type()]
class CQRSEntityMutationResult extends CQRSCommandResult{


#[Field(type: 'Ulid')]
#[Deprecated("use id instead")]
private ?Ulid $uid = null;

public function __construct(Ulid $uid, ?string $clientMutationId = null){
$this->uid = $uid;
parent::__construct($clientMutationId);
}


public function getUid(): ?Ulid{
return $this->uid;
}

#[Field(name: 'id', type: 'Ulid')]
public function getId(): ?Ulid{
return $this->getUid();
}

}
9 changes: 9 additions & 0 deletions src/CQRS/Command/Result/CQRSEntryCreationCommandResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace App\CQRS\Command\Result;
use Overblog\GraphQLBundle\Annotation\Type;


#[Type()]
class CQRSEntryCreationCommandResult extends CQRSEntityMutationResult{

}
9 changes: 9 additions & 0 deletions src/CQRS/Command/Result/CQRSEntryUpdateCommandResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace App\CQRS\Command\Result;
use Overblog\GraphQLBundle\Annotation\Type;


#[Type()]
class CQRSEntryUpdateCommandResult extends CQRSEntityMutationResult{

}
62 changes: 62 additions & 0 deletions src/CQRS/CommandBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
namespace App\CQRS;

use App\CQRS\Command\MutationCommandContext;
use App\CQRS\Command\Result\CQRSEntryCreationCommandResult;
use Symfony\Component\Messenger\MessageBusInterface as BaseMessageBusInterface;
use Symfony\Component\Uid\Factory\UlidFactory;

class CommandBus extends MessageBus implements CommandBusInterface{


/**
* @var array<string,CQRSEntryCreationCommandResult>
*/
private array $results = [];


/**
* @var array<string,MutationCommandContext>
*/
private array $mutationContexts = [];


public function __construct(private BaseMessageBusInterface $commandBus,private UlidFactory $ulidFactory) {
parent::__construct($commandBus);
}

public function getMutationContext(?string $clientMutationId = null): MutationCommandContext{

if( is_string($clientMutationId) && array_key_exists($clientMutationId,$this->mutationContexts)){
return $this->mutationContexts[$clientMutationId];
}

$context = new MutationCommandContext($this, $clientMutationId);

if(is_string($clientMutationId))
return $this->mutationContexts[$clientMutationId] = $context;

return $context;
}

public function dispatchContextual(EntityCreationCommandInterface $command, ?string $clientMuationId): CQRSEntryCreationCommandResult{
return $this->getMutationContext($clientMuationId)->dispatch($command);
}


// public function getOptimisticEntryCreationResult(?string $clientMutationId = null): CQRSEntryCreationCommandResult{

// if( is_string($clientMutationId) && array_key_exists($clientMutationId,$this->results)){
// return $this->results[$clientMutationId];
// }

// $ulid = $this->ulidFactory->create();
// $result = new CQRSEntryCreationCommandResult($ulid);

// if(is_string($clientMutationId))
// return $this->results[$clientMutationId] = $result;

// return $result;
// }

}
11 changes: 11 additions & 0 deletions src/CQRS/CommandBusInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace App\CQRS;
use App\CQRS\Command\MutationCommandContext;
// use App\CQRS\Command\Result\CQRSEntryCreationCommandResult;


interface CommandBusInterface extends MessageBusInterface{

public function getMutationContext(?string $clientMutationId = null): MutationCommandContext;
// public function getOptimisticEntryCreationResult(?string $clientMutationId = null): CQRSEntryCreationCommandResult;
}
14 changes: 14 additions & 0 deletions src/CQRS/Composition/InitiatingUserAwareMessageInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace App\CQRS\Composition;
use App\CQRS\Query\QueryInterface;
use Symfony\Component\Uid\Ulid;


interface InitiatingUserAwareMessageInterface extends QueryInterface{

/**
* The Id of the user Initialting this Message
* @return Ulid
*/
public function getInitiatingUserId(): ?Ulid;
}
Loading

0 comments on commit 7aee0dd

Please sign in to comment.