Skip to content

Improvements/#1 #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DB_DSN=mysql:host=localhost;dbname=conticket
DB_USER=root
DB_PASSWORD=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ composer.phar
composer.lock
bin
vendor/
.env
app/cache/*
app/logs/*
app/phpunit.xml
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 30 additions & 0 deletions config/commands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

use Conticket\Conference\Factory\CommandHandler\CreateConferenceHandlerFactory;
use Conticket\Conference\Domain\Command\CreateConference;

return (function () {
return [
'factories' => [
CreateConference::class => CreateConferenceHandlerFactory::class,
],
];
})();
12 changes: 7 additions & 5 deletions config/middlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
})();
30 changes: 30 additions & 0 deletions config/repositories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

use Conticket\Conference\Domain\Repository\ConferenceRepositoryInterface;
use Conticket\Conference\Factory\Repository\ConferenceRepositoryFactory;

return (function () {
return [
'factories' => [
ConferenceRepositoryInterface::class => ConferenceRepositoryFactory::class,
],
];
})();
4 changes: 3 additions & 1 deletion config/service-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
);
})();
42 changes: 9 additions & 33 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too much space

CommandBus::class => CommandBusFactory::class,
EventStore::class => EventStoreFactory::class,
Connection::class => ConnectionFactory::class,
\PDO::class => PDOFactory::class,
],
];
})();
2 changes: 2 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
30 changes: 16 additions & 14 deletions src/Conference/Domain/Command/CreateConference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions src/Conference/Infrastructure/Service/ApplicationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace Conticket\Conference\Infrastructure\Service;

use Interop\Container\ContainerInterface;
use Zend\Expressive\Router\FastRouteRouter;
use Zend\Expressive\Application;
use Zend\ServiceManager\Factory\FactoryInterface;

/**
* @author Luciano Queiroz <luciiano.queiroz@gmail.com>
*/
final class ApplicationFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): Application
{
return new Application($container->get(FastRouteRouter::class), $container);
}
}
5 changes: 3 additions & 2 deletions src/Conference/Infrastructure/Service/CommandBusFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <malukenho@phpse.net>
*/
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));
Expand Down
16 changes: 6 additions & 10 deletions src/Conference/Infrastructure/Service/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand All @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions src/Conference/Infrastructure/Service/EventStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <malukenho@phpse.net>
*/
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(
Expand Down
32 changes: 32 additions & 0 deletions src/Conference/Infrastructure/Service/PDOFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

declare(strict_types=1);

namespace Conticket\Conference\Infrastructure\Service;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

final class PDOFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): \PDO
{
return new \PDO(getenv('DB_DSN'), getenv('DB_USER'), getenv('DB_PASSWORD'));
}
}