Skip to content

Instantly share code, notes, and snippets.

@codeliner codeliner/OrgaState.php
Last active Sep 6, 2017

Embed
What would you like to do?
Workshop material
<?php
declare(strict_types = 1);
namespace App\Projection;
use App\Api\MessageDescription;
use App\Infrastructure\MongoDb\MongoConnection;
use MongoDB\Collection;
use Prooph\Common\Messaging\Message;
use Prooph\EventStore\Projection\AbstractReadModel;
use Prooph\EventStore\Projection\ReadModelProjector;
chdir(dirname(__DIR__));
require 'vendor/autoload.php';
/** @var \Psr\Container\ContainerInterface $container */
$container = require 'config/container.php';
/** @var \Prooph\EventMachine\EventMachine $eventMachine */
$eventMachine = $container->get('eventMachine');
$eventMachine->bootstrap();
/** @var \Prooph\EventStore\Projection\ProjectionManager $projectionManager */
$projectionManager = $container->get('projectionManager');
$readModel = new class($container->get('mongoConnection')) extends AbstractReadModel
{
const COLLECTION = 'rejected_invitations';
/**
* @var MongoConnection
*/
private $mongoConnection;
public function __construct(MongoConnection $mongoConnection)
{
$this->mongoConnection = $mongoConnection;
}
public function init(): void
{
//nothing required for mongodb
}
public function isInitialized(): bool
{
return true;
}
public function reset(): void
{
$this->delete();
}
public function delete(): void
{
$this->getCollection()->drop();
}
protected function add(array $doc): void
{
$this->getCollection()->insertOne($doc);
}
public function getCollection(): Collection
{
return $this->mongoConnection->selectCollection(self::COLLECTION);
}
};
$projection = $projectionManager->createReadModelProjection(
'rejected_invitations_projection',
$readModel,
[
ReadModelProjector::OPTION_PERSIST_BLOCK_SIZE => 1
]
);
$projection->fromStream('event_stream')
->when([
MessageDescription::EVENT_ORGA_INVITATION_WAS_REJECTED => function(array $state, Message $orgaInvitationWasRejected) {
/** @var AbstractReadModel $readModel */
$readModel = $this->readModel();
$readModel->stack('add', $orgaInvitationWasRejected->payload());
},
])
->run(false);
<?php
declare(strict_types=1);
namespace App\Model\Organization;
final class OrgaState
{
public $orgaId;
public $orgaName;
public $owner;
public $members = [];
public $pendingInvitations = [];
public static function fromArray(array $userProps): self
{
$self = new self();
$self->merge($userProps);
return $self;
}
public function merge(array $props)
{
foreach ($props as $prop => $value) {
if(!property_exists(__CLASS__, $prop)) {
throw new \InvalidArgumentException(__CLASS__ . " does not have a property $prop");
}
$this->{$prop} = $value;
}
}
public function addInvitation(string $memberId)
{
$this->pendingInvitations[$memberId] = true;
}
public function rmInvitation(string $memberId)
{
unset($this->pendingInvitations[$memberId]);
}
}
{
"variables": [],
"info": {
"name": "Workshop Sample Collection",
"_postman_id": "c7bad410-2e9d-a61f-61e8-68a5ea568430",
"description": "",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "App.RegisterUser",
"request": {
"url": "http://localhost:8080/api/messagebox/App.RegisterUser",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Accept",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"payload\": {\n\t\t\"userId\": \"932577dd-3593-47bd-8bd0-3bd7bf7e111f\",\n\t\t\"email\": \"contact@prooph.de\"\n\t\t\"username\": \"codeliner\"\n\t}\n}"
},
"description": "Register a new user"
},
"response": []
},
{
"name": "App.ChangeUsername",
"request": {
"url": "http://localhost:8080/api/messagebox/App.ChangeUsername",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Accept",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"payload\": {\n\t\t\"userId\": \"f75899a0-5fc9-4608-8c36-f13dde1bbb78\",\n\t\t\"newUsername\": \"your name here\"\n\t}\n}"
},
"description": "Change name of a user"
},
"response": []
},
{
"name": "App.ValidateEmail",
"request": {
"url": "http://localhost:8080/api/messagebox/App.ValidateEmail",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Accept",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"payload\": {\n\t\t\"userId\": \"932577dd-3593-47bd-8bd0-3bd7bf7e111f\",\n\t\t\"validationCode\": \"contact@prooph.de\"\n\t}\n}"
},
"description": "Validate email of a new user"
},
"response": []
},
{
"name": "App.InitiateOrgaCreation",
"request": {
"url": "http://localhost:8080/api/messagebox/App.InitiateOrgaCreation",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Accept",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"payload\": {\n\t\t\"userId\": \"932577dd-3593-47bd-8bd0-3bd7bf7e111f\",\n\t\t\"orgaId\": \"8fdc4ff1-1097-4abf-846e-45f8767c2ff0\",\n\t\t\"orgaName\": \"Acme\"\n\t}\n}"
},
"description": "Initiate creation of an organization"
},
"response": []
},
{
"name": "App.CreateOrga",
"request": {
"url": "http://localhost:8080/api/messagebox/App.CreateOrga",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Accept",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"payload\": {\n\t\t\"owner\": \"932577dd-3593-47bd-8bd0-3bd7bf7e111f\",\n\t\t\"orgaId\": \"\",\n\t\t\"orgaName\": \"Acme\"\n\t}\n}"
},
"description": "Create an Orga"
},
"response": []
},
{
"name": "App.InviteOrgaMember",
"request": {
"url": "http://localhost:8080/api/messagebox/App.InviteOrgaMember",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Accept",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"payload\": {\n\t\t\"invitedBy\": \"932577dd-3593-47bd-8bd0-3bd7bf7e111f\",\n\t\t\"orgaId\": \"8fdc4ff1-1097-4abf-846e-45f8767c2ff0\",\n\t\t\"memberId\": \"1f4d56ab-e8d8-429a-afab-9fa17a3e0ae6\"\n\t}\n}"
},
"description": "Initiate creation of an organization"
},
"response": []
},
{
"name": "App.RejectOrgaInvitation",
"request": {
"url": "http://localhost:8080/api/messagebox/App.RejectOrgaInvitation",
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": ""
},
{
"key": "Accept",
"value": "application/json",
"description": ""
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"payload\": {\n\t\t\"orgaId\": \"8fdc4ff1-1097-4abf-846e-45f8767c2ff0\",\n\t\t\"memberId\": \"1f4d56ab-e8d8-429a-afab-9fa17a3e0ae6\"\n\t}\n}"
},
"description": "Initiate creation of an organization"
},
"response": []
}
]
}
<?php
declare(strict_types=1);
namespace App\Model\User;
final class UserState
{
public $userId;
public $email;
public $username;
public $validated = false;
public $validationCode;
public $ownOrganizations = [];
public static function fromArray(array $userProps): self
{
$self = new self();
$self->merge($userProps);
return $self;
}
public function merge(array $props)
{
foreach ($props as $prop => $value) {
if(!property_exists(__CLASS__, $prop)) {
throw new \InvalidArgumentException(__CLASS__ . " does not have a property $prop");
}
$this->{$prop} = $value;
}
}
public function addOrganization(string $orgaId)
{
$this->ownOrganizations[] = $orgaId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.