diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..e69f3ac --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +DB_DSN=mysql:host=localhost;dbname=conticket +DB_USER=root +DB_PASSWORD= diff --git a/.gitignore b/.gitignore index b2a0a49..8379cb4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ composer.phar composer.lock bin vendor/ +.env app/cache/* app/logs/* app/phpunit.xml diff --git a/composer.json b/composer.json index e23eb6f..14017ac 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "prooph/event-store-doctrine-adapter": "^3.3", "prooph/service-bus": "^5.2", "ramsey/uuid": "^2.8", + "vlucas/phpdotenv": "^2.4", "zendframework/zend-expressive": "^1.0", "zendframework/zend-expressive-fastroute": "^1.0", "zendframework/zend-servicemanager": "^3.2" diff --git a/config/commands.php b/config/commands.php new file mode 100644 index 0000000..03fdd90 --- /dev/null +++ b/config/commands.php @@ -0,0 +1,30 @@ + [ + CreateConference::class => CreateConferenceHandlerFactory::class, + ], + ]; +})(); diff --git a/config/middlewares.php b/config/middlewares.php index 958c34d..a1a49a6 100644 --- a/config/middlewares.php +++ b/config/middlewares.php @@ -5,8 +5,10 @@ use Conticket\Conference\Infrastructure\Middleware\CreateConferenceMiddleware; use Conticket\Conference\Factory\Middleware\CreateConferenceMiddlewareFactory; -return [ - 'factories' => [ - CreateConferenceMiddleware::class => CreateConferenceMiddlewareFactory::class, - ], -]; +return (function () { + return [ + 'factories' => [ + CreateConferenceMiddleware::class => CreateConferenceMiddlewareFactory::class, + ], + ]; +})(); diff --git a/config/repositories.php b/config/repositories.php new file mode 100644 index 0000000..c20f9ed --- /dev/null +++ b/config/repositories.php @@ -0,0 +1,30 @@ + [ + ConferenceRepositoryInterface::class => ConferenceRepositoryFactory::class, + ], + ]; +})(); diff --git a/config/service-manager.php b/config/service-manager.php index 94570cd..39ff25f 100644 --- a/config/service-manager.php +++ b/config/service-manager.php @@ -8,7 +8,9 @@ return new \Zend\ServiceManager\ServiceManager( array_merge_recursive( require __DIR__ . '/services.php', - require __DIR__ . '/middlewares.php' + require __DIR__ . '/commands.php', + require __DIR__ . '/middlewares.php', + require __DIR__ . '/repositories.php' ) ); })(); diff --git a/config/services.php b/config/services.php index 57c9347..d256f4e 100644 --- a/config/services.php +++ b/config/services.php @@ -2,51 +2,27 @@ declare(strict_types=1); -use Conticket\Conference\Domain\Repository\ConferenceRepositoryInterface; -use Conticket\Conference\Factory\Repository\ConferenceRepositoryFactory; -use Conticket\Conference\Factory\CommandHandler\CreateConferenceHandlerFactory; -use Conticket\Conference\Domain\Command\CreateConference; +use Conticket\Conference\Infrastructure\Service\ApplicationFactory; use Conticket\Conference\Infrastructure\Service\CommandBusFactory; use Conticket\Conference\Infrastructure\Service\ConnectionFactory; use Conticket\Conference\Infrastructure\Service\EventStoreFactory; +use Conticket\Conference\Infrastructure\Service\PDOFactory; use Doctrine\DBAL\Connection; -use Interop\Container\ContainerInterface; use Prooph\EventStore\EventStore; use Prooph\ServiceBus\CommandBus; use Zend\Expressive\Application; use Zend\Expressive\Router\FastRouteRouter; +use Zend\ServiceManager\Factory\InvokableFactory; return (function () { return [ - // @todo move factories to proper classes 'factories' => [ - Application::class => function (ContainerInterface $container) { - return new Application($container->get(FastRouteRouter::class), $container); - }, - FastRouteRouter::class => function (ContainerInterface $container) { - return new FastRouteRouter(); - }, - - CommandBus::class => CommandBusFactory::class, - EventStore::class => EventStoreFactory::class, - Connection::class => ConnectionFactory::class, - - // @todo move commands/events to another config file - CreateConference::class => CreateConferenceHandlerFactory::class, - - // @todo move repository to another file - ConferenceRepositoryInterface::class => ConferenceRepositoryFactory::class, - - // @todo move db info to a class to get ENV vars - 'db_dsn' => function () { - return 'mysql:host=localhost;dbname=conticket'; - }, - 'db_user' => function () { - return 'root'; - }, - 'db_password' => function () { - return 'root'; - }, + Application::class => ApplicationFactory::class, + FastRouteRouter::class => InvokableFactory::class, + CommandBus::class => CommandBusFactory::class, + EventStore::class => EventStoreFactory::class, + Connection::class => ConnectionFactory::class, + \PDO::class => PDOFactory::class, ], ]; })(); diff --git a/public/index.php b/public/index.php index e7d1703..b1a99d2 100644 --- a/public/index.php +++ b/public/index.php @@ -7,6 +7,8 @@ (function () { require __DIR__ . '/../vendor/autoload.php'; + (new \Dotenv\Dotenv(__DIR__ . '/..'))->load(); + /* @var $serviceManager \Zend\ServiceManager\ServiceManager */ $serviceManager = require __DIR__ . '/../config/service-manager.php'; diff --git a/src/Conference/Domain/Command/CreateConference.php b/src/Conference/Domain/Command/CreateConference.php index 8f56d94..f487fc6 100644 --- a/src/Conference/Domain/Command/CreateConference.php +++ b/src/Conference/Domain/Command/CreateConference.php @@ -54,30 +54,32 @@ final class CreateConference extends Command */ private $date; - private function __construct() - { - } - - public static function fromRequestData( + private function __construct( ConferenceId $conferenceId, string $name, string $description, string $author, \DateTimeImmutable $date - ): self { - // @todo move to __constructor + ) { Assertion::notEmpty($name); Assertion::notEmpty($description); Assertion::notEmpty($author); - $self = new self(); - $self->conferenceId = $conferenceId; - $self->name = $name; - $self->description = $description; - $self->author = $author; - $self->date = $date; + $this->conferenceId = $conferenceId; + $this->name = $name; + $this->description = $description; + $this->author = $author; + $this->date = $date; + } - return $self; + public static function fromRequestData( + ConferenceId $conferenceId, + string $name, + string $description, + string $author, + \DateTimeImmutable $date + ): self { + return new self($conferenceId, $name, $description, $author, $date); } /** diff --git a/src/Conference/Infrastructure/Service/ApplicationFactory.php b/src/Conference/Infrastructure/Service/ApplicationFactory.php new file mode 100644 index 0000000..0191b72 --- /dev/null +++ b/src/Conference/Infrastructure/Service/ApplicationFactory.php @@ -0,0 +1,37 @@ + + */ +final class ApplicationFactory implements FactoryInterface +{ + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Application + { + return new Application($container->get(FastRouteRouter::class), $container); + } +} diff --git a/src/Conference/Infrastructure/Service/CommandBusFactory.php b/src/Conference/Infrastructure/Service/CommandBusFactory.php index f448ac5..5e4c090 100644 --- a/src/Conference/Infrastructure/Service/CommandBusFactory.php +++ b/src/Conference/Infrastructure/Service/CommandBusFactory.php @@ -27,13 +27,14 @@ use Prooph\ServiceBus\CommandBus; use Prooph\ServiceBus\MessageBus; use Prooph\ServiceBus\Plugin\ServiceLocatorPlugin; +use Zend\ServiceManager\Factory\FactoryInterface; /** * @author Jefersson Nathan */ -final class CommandBusFactory +final class CommandBusFactory implements FactoryInterface { - public function __invoke(ContainerInterface $container): CommandBus + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): CommandBus { $commandBus = new CommandBus(); $commandBus->utilize(new ServiceLocatorPlugin($container)); diff --git a/src/Conference/Infrastructure/Service/ConnectionFactory.php b/src/Conference/Infrastructure/Service/ConnectionFactory.php index 5a575a7..de99e4a 100644 --- a/src/Conference/Infrastructure/Service/ConnectionFactory.php +++ b/src/Conference/Infrastructure/Service/ConnectionFactory.php @@ -24,21 +24,16 @@ use Doctrine\DBAL\Driver\PDOMySql\Driver; use Doctrine\DBAL\Schema\SchemaException; use Interop\Container\ContainerInterface; -use PDO; use Prooph\EventStore\Adapter\Doctrine\Schema\EventStoreSchema; +use Zend\ServiceManager\Factory\FactoryInterface; -final class ConnectionFactory +final class ConnectionFactory implements FactoryInterface { - public function __invoke(ContainerInterface $container): Connection + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Connection { - // @todo create service for \PDO - $connection = new Connection( + $connection = new Connection( [ - 'pdo' => new PDO( - $container->get('db_dsn'), - $container->get('db_user'), - $container->get('db_password') - ), + 'pdo' => $container->get(\PDO::class) ], new Driver() ); @@ -51,6 +46,7 @@ public function __invoke(ContainerInterface $container): Connection foreach ($schema->toSql($connection->getDatabasePlatform()) as $sql) { $connection->exec($sql); } + } catch (SchemaException $ignored) { // this is ignored for now - we don't want to re-create the schema every time } diff --git a/src/Conference/Infrastructure/Service/EventStoreFactory.php b/src/Conference/Infrastructure/Service/EventStoreFactory.php index c0cb3cb..a6c3f26 100644 --- a/src/Conference/Infrastructure/Service/EventStoreFactory.php +++ b/src/Conference/Infrastructure/Service/EventStoreFactory.php @@ -28,13 +28,14 @@ use Prooph\EventStore\Adapter\Doctrine\DoctrineEventStoreAdapter; use Prooph\EventStore\Adapter\PayloadSerializer\JsonPayloadSerializer; use Prooph\EventStore\EventStore; +use Zend\ServiceManager\Factory\FactoryInterface; /** * @author Jefersson Nathan */ -final class EventStoreFactory +final class EventStoreFactory implements FactoryInterface { - public function __invoke(ContainerInterface $container): EventStore + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): EventStore { return new EventStore( new DoctrineEventStoreAdapter( diff --git a/src/Conference/Infrastructure/Service/PDOFactory.php b/src/Conference/Infrastructure/Service/PDOFactory.php new file mode 100644 index 0000000..526f9dd --- /dev/null +++ b/src/Conference/Infrastructure/Service/PDOFactory.php @@ -0,0 +1,32 @@ +