Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
webdevilopers / EmploymentContract.php
Last active July 15, 2020 08:17
Creating event-sourced aggregate roots through CQRS read models
<?php
namespace AcmersonnelManagement\Domain\Model\EmploymentContract;
final class EmploymentContract extends AggregateRoot
{
/** @var EmploymentContractId $employmentContractId */
private $id;
/** @var PersonId $personId */
@webdevilopers
webdevilopers / AgencyPublisher.php
Created June 6, 2020 09:16
Symfony Messenger - Subscribing multiple Handlers to Messages
<?php
namespace Acme\TemporaryWork\Infrastructure\ProcessManager;
use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface;
use Prooph\EventSourcing\AggregateChanged;
use Acme\TemporaryWork\Domain\Model\Agency\Event\AgencyHired;
use Acme\TemporaryWork\Domain\Model\Agency\Event\AgencyModified;
use Acme\PersonnelManagement\Infrastructure\RabbitMq\Producer;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
@webdevilopers
webdevilopers / EventPublisher.php
Created June 4, 2020 09:26
Replacing Prooph Service Event Bus with Symfony Messenger using custom Event Publisher
<?php
namespace Acme\Context\Infrastructure\Prooph;
use Prooph\Common\Event\ActionEvent;
use Prooph\EventStore\ActionEventEmitterEventStore;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Plugin\AbstractPlugin;
use Prooph\EventStore\TransactionalActionEventEmitterEventStore;
use Symfony\Component\Messenger\MessageBusInterface;
@webdevilopers
webdevilopers / AbstractControllerTest.php
Last active May 8, 2020 07:37
Functional Testing Symfony Controllers with JWT and Symfony Messenger
<?php
namespace Tests\Acme\PersonnelManagement\Infrastructure\Symfony\AcmePersonnelManagementBundle\Controller;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
abstract class AbstractControllerTest extends WebTestCase
{
@webdevilopers
webdevilopers / ErrorResponseBuilder.php
Last active March 23, 2023 13:57
API friendly error handling with Symfony Messenger and Event Listener
<?php
namespace Acme\Common\Infrastructure\Symfony\Messenger;
use Prooph\EventStore\Exception\ConcurrencyException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\RuntimeException;
use Symfony\Component\Messenger\Exception\ValidationFailedException;
@webdevilopers
webdevilopers / Deployment.php
Last active March 10, 2020 15:40
Domain-Driven Design - Creating aggregates and enforcing invariants spanning multiple aggregates
<?php
final class Deployment1
{
public static function with(DeploymentId $anId, EmploymentContractId $anEmploymentContractId,
DeploymentPeriod $aPeriod): Deployment
{
// No period range validation, final period was already validated and passed
}
}
@webdevilopers
webdevilopers / DefaultGridFSRepository.php
Last active January 18, 2020 09:04
Uploading Files with Metadata using MongoDB GridFS in a PHP Domain-Driven Design application
<?php
class DefaultGridFSRepository extends DocumentRepository implements GridFSRepository
{
public function uploadFromStream(string $filename, $source, ?UploadOptions $uploadOptions = null)
{
$options = $this->prepareOptions($uploadOptions);
$databaseIdentifier = $this->getDocumentBucket()->uploadFromStream($filename, $source, $options);
$documentIdentifier = $this->class->getPHPIdentifierValue($databaseIdentifier);
@webdevilopers
webdevilopers / functions.php
Created January 11, 2020 09:23
How to convert PHP MongoDB BSON Binary to (L)UUID or ObjectId
<?php
use MongoDB\BSON\Binary;
use MongoDB\BSON\ObjectId;
function mongoBinaryToUuid(Binary $bin): string
{
$hex = bin2hex($bin->getData());
return substr($hex, 0, 32);
@webdevilopers
webdevilopers / MaterialQuantityDto.php
Created November 29, 2019 11:18
Dynamic form validation groups with callback in Symfony 2.8
<?php
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class MaterialQuantityDto
{
/**
* @var string
* @Assert\NotNull(groups={"available_material"})
@webdevilopers
webdevilopers / JsonObject.php
Created April 15, 2019 16:08
UPDATE and INSERT statements with Doctrine DBAL and MySQL JSON type
<?php
class JsonObject
{
/** @var string $result */
private $result;
private function __construct(string $result)
{
$this->result = $result;