Skip to content

Commit

Permalink
Initial domain structure extracted from current proyects
Browse files Browse the repository at this point in the history
Co-authored-by: Alejandro Mascort Colomer <[email protected]>
Co-authored-by: Diego García <[email protected]>
Co-authored-by: Miquel Mariño Espinosa <[email protected]>
Co-authored-by: Juan Cama Villafan <[email protected]>
Co-authored-by: Victor del Valle <[email protected]>
Co-authored-by: Alejandro García Sánchez <[email protected]>
  • Loading branch information
7 people committed Sep 29, 2021
0 parents commit 805721f
Show file tree
Hide file tree
Showing 70 changed files with 2,086 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: pipeline

on:
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Build environment
run: |
make start
- name: Unit tests
run: make phpunit
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# phpstorm project files
.idea

# netbeans project files
nbproject/*

# zend studio for eclipse project files
.buildpath
.project
.settings

# windows thumbnail cache
Thumbs.db

# Mac DS_Store Files
.DS_Store

/vendor/
composer.lock
/.phpunit.result.cache
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM php:8.0-cli-alpine

RUN apk update && \
apk add --no-cache \
libzip-dev \
git \
openssl-dev && \
docker-php-ext-install -j$(nproc) \
zip

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin/ --filename=composer

ENV PATH /var/app/bin:/var/app/vendor/bin:$PATH

WORKDIR /var/app
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
UID=$(shell id -u)
GID=$(shell id -g)
CONTAINER=php

start: erase cache-folders build composer-install bash

erase:
docker-compose down -v

build:
docker-compose build && \
docker-compose pull

cache-folders:
mkdir -p ~/.composer && chown ${UID}:${GID} ~/.composer

composer-install:
docker-compose run --rm -u ${UID}:${GID} ${CONTAINER} composer install

bash:
docker-compose run --rm -u ${UID}:${GID} ${CONTAINER} sh

phpunit: ## execute project unit tests
docker-compose run --rm -u ${UID}:${GID} ${CONTAINER} phpunit --no-coverage
62 changes: 62 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "colvin/common-domain-php",
"description": "Domain structure for shared projects",
"authors": [
{
"name": "Aaron Bernabeu Rodríguez",
"email": "[email protected]"
},
{
"name": "Alejandro Mascort Colomer",
"email": "[email protected]"
},
{
"name": "Alejandro García Sánchez",
"email": "[email protected]"
},
{
"name": "Diego García",
"email": "[email protected]"
},
{
"name": "Miquel Mariño Espinosa",
"email": "[email protected]"
},
{
"name": "Juan Cama Villafan",
"email": "[email protected]"
},
{
"name": "Victor del Valle",
"email": "[email protected]"
}
],
"license": "MIT",
"type": "library",
"autoload": {
"psr-4": {
"Colvin\\CommonDomain\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Colvin\\CommonDomain\\Tests\\": "tests/"
}
},
"require": {
"php": "^8.0",
"ext-json": "*",
"symfony/uid": "^5.3"
},
"require-dev": {
"phpro/grumphp": "^1.5",
"phpunit/phpunit": "^9.5"
},
"scripts": {
"post-install-cmd": [
"rm -rf .git/hooks",
"mkdir -p .git/hooks",
"cp -r ./config/hooks/* .git/hooks"
]
}
}
6 changes: 6 additions & 0 deletions config/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

SCRIPT=$(docker-compose run --no-deps --rm php sh -c "grumphp git:pre-commit" 2>&1)
STATUS=$?
echo "$SCRIPT"
exit $STATUS
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3.8'

services:
php:
build: .
volumes:
- .:/var/app
- ~/.composer:/.composer
7 changes: 7 additions & 0 deletions grumphp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
grumphp:
tasks:
composer:
strict: true
jsonlint: ~
phplint: ~
phpunit: ~
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>

<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
15 changes: 15 additions & 0 deletions src/Application/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Application;

use Colvin\CommonDomain\Domain\Message\ActionMessage;

abstract class Command extends ActionMessage
{
public static function messageType(): string
{
return 'command';
}
}
15 changes: 15 additions & 0 deletions src/Application/Query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Application;

use Colvin\CommonDomain\Domain\Message\ActionMessage;

abstract class Query extends ActionMessage
{
public static function messageType(): string
{
return 'query';
}
}
15 changes: 15 additions & 0 deletions src/Domain/DomainEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Domain;

use Colvin\CommonDomain\Domain\Message\AggregateMessage;

abstract class DomainEvent extends AggregateMessage
{
public static function messageType(): string
{
return 'domain_event';
}
}
20 changes: 20 additions & 0 deletions src/Domain/Exception/AggregateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Domain\Exception;

abstract class AggregateException extends DomainException
{
abstract public function id(): string;

abstract public function aggregateType(): string;

final public function jsonSerialize(): array
{
return [
'id' => $this->id(),
'aggregateType' => $this->aggregateType(),
];
}
}
9 changes: 9 additions & 0 deletions src/Domain/Exception/DomainException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Domain\Exception;

abstract class DomainException extends \DomainException implements \JsonSerializable
{
}
9 changes: 9 additions & 0 deletions src/Domain/Exception/ExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Domain\Exception;

abstract class ExistsException extends AggregateException
{
}
15 changes: 15 additions & 0 deletions src/Domain/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Domain\Exception;

abstract class LogicException extends DomainException
{
abstract public function data(): array;

final public function jsonSerialize(): array
{
return $this->data();
}
}
9 changes: 9 additions & 0 deletions src/Domain/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Domain\Exception;

abstract class NotFoundException extends AggregateException
{
}
15 changes: 15 additions & 0 deletions src/Domain/Message/ActionMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Domain\Message;

use Colvin\CommonDomain\Domain\Model\ValueObject\Uuid;

abstract class ActionMessage extends Message
{
final public static function fromPayload(Uuid $messageId, array $payload): static
{
return new static($messageId, $payload);
}
}
50 changes: 50 additions & 0 deletions src/Domain/Message/AggregateMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Colvin\CommonDomain\Domain\Message;

use Colvin\CommonDomain\Domain\Model\ValueObject\DateTimeValueObject;
use Colvin\CommonDomain\Domain\Model\ValueObject\Uuid;

abstract class AggregateMessage extends Message
{
final protected function __construct(
Uuid $messageId,
private Uuid $aggregateId,
private DateTimeValueObject $occurredOn,
array $payload
) {
parent::__construct($messageId, $payload);
}

final public static function fromPayload(
Uuid $messageId,
Uuid $aggregateId,
DateTimeValueObject $occurredOn,
array $payload
): static {
return new static($messageId, $aggregateId, $occurredOn, $payload);
}

public function aggregateId(): Uuid
{
return $this->aggregateId;
}

final public function jsonSerialize(): array
{
return \array_merge(
parent::jsonSerialize(),
[
'aggregateId' => $this->aggregateId->value(),
'occurredOn' => $this->occurredOn->jsonSerialize(),
],
);
}

public function occurredOn(): DateTimeValueObject
{
return $this->occurredOn;
}
}
Loading

0 comments on commit 805721f

Please sign in to comment.