Skip to content
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

add reopen todo #59

Merged
merged 6 commits into from
Feb 17, 2016
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions config/autoload/dependencies.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
\Prooph\ProophessorDo\Model\User\UserCollection::class => \Prooph\ProophessorDo\Container\Infrastructure\Repository\EventStoreUserCollectionFactory::class,
\Prooph\ProophessorDo\Model\Todo\Handler\PostTodoHandler::class => \Prooph\ProophessorDo\Container\Model\Todo\PostTodoHandlerFactory::class,
\Prooph\ProophessorDo\Model\Todo\Handler\MarkTodoAsDoneHandler::class => \Prooph\ProophessorDo\Container\Model\Todo\MarkTodoAsDoneHandlerFactory::class,
\Prooph\ProophessorDo\Model\Todo\Handler\ReopenTodoHandler::class => \Prooph\ProophessorDo\Container\Model\Todo\ReopenTodoHandlerFactory::class,
\Prooph\ProophessorDo\Model\Todo\Handler\AddDeadlineToTodoHandler::class => \Prooph\ProophessorDo\Container\Model\Todo\AddDeadlineToTodoHandlerFactory::class,
\Prooph\ProophessorDo\Model\Todo\TodoList::class => \Prooph\ProophessorDo\Container\Infrastructure\Repository\EventStoreTodoListFactory::class,
// Projections
Expand Down
5 changes: 5 additions & 0 deletions config/autoload/prooph.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
\Prooph\ProophessorDo\Model\User\Command\RegisterUser::class => \Prooph\ProophessorDo\Model\User\Handler\RegisterUserHandler::class,
\Prooph\ProophessorDo\Model\Todo\Command\PostTodo::class => \Prooph\ProophessorDo\Model\Todo\Handler\PostTodoHandler::class,
\Prooph\ProophessorDo\Model\Todo\Command\MarkTodoAsDone::class => \Prooph\ProophessorDo\Model\Todo\Handler\MarkTodoAsDoneHandler::class,
\Prooph\ProophessorDo\Model\Todo\Command\ReopenTodo::class => \Prooph\ProophessorDo\Model\Todo\Handler\ReopenTodoHandler::class,
\Prooph\ProophessorDo\Model\Todo\Command\AddDeadlineToTodo::class => \Prooph\ProophessorDo\Model\Todo\Handler\AddDeadlineToTodoHandler::class,
],
],
Expand All @@ -71,6 +72,10 @@
\Prooph\ProophessorDo\Projection\Todo\TodoProjector::class,
\Prooph\ProophessorDo\Projection\User\UserProjector::class,
],
\Prooph\ProophessorDo\Model\Todo\Event\TodoWasReopened::class => [
\Prooph\ProophessorDo\Projection\Todo\TodoProjector::class,
\Prooph\ProophessorDo\Projection\User\UserProjector::class,
],
\Prooph\ProophessorDo\Model\Todo\Event\DeadlineWasAddedToTodo::class => [
\Prooph\ProophessorDo\Projection\Todo\TodoProjector::class,
],
Expand Down
13 changes: 13 additions & 0 deletions config/autoload/routes.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@
],
],
[
'name' => 'command::reopen-todo',
'path' => '/api/commands/reopen-todo',
'middleware' => [
\Prooph\ProophessorDo\Middleware\JsonPayload::class,
\Prooph\Psr7Middleware\CommandMiddleware::class,
],
'allowed_methods' => ['POST'],
'options' => [
'values' => [
\Prooph\Psr7Middleware\CommandMiddleware::NAME_ATTRIBUTE => \Prooph\ProophessorDo\Model\Todo\Command\ReopenTodo::class,
],
],
], [
'name' => 'command::add-deadline-to-todo',
'path' => '/api/commands/add-deadline-to-todo',
'middleware' => [
Expand Down
2 changes: 1 addition & 1 deletion public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ body {
text-decoration: none;
}

.glyphicon-unchecked {
.glyphicon-unchecked,.glyphicon-check {
cursor: pointer;
}
35 changes: 35 additions & 0 deletions src/Container/Model/Todo/ReopenTodoHandlerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* This file is part of prooph/proophessor.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 2/16/16
*/
namespace Prooph\ProophessorDo\Container\Model\Todo;

use Interop\Container\ContainerInterface;
use Prooph\ProophessorDo\Model\Todo\Handler\ReopenTodoHandler;
use Prooph\ProophessorDo\Model\Todo\TodoList;

/**
* Class ReopenTodoFactory
*
* @package Application\Infrastructure\HandlerFactory
* @author Bas Kamer <[email protected]>
*/
final class ReopenTodoHandlerFactory
{
/**
* @param ContainerInterface $container
* @return ReopenTodoHandlerFactory
*/
public function __invoke(ContainerInterface $container)
{
return new ReopenTodoHandler(
$container->get(TodoList::class)
);
}
}
47 changes: 47 additions & 0 deletions src/Model/Todo/Command/ReopenTodo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* This file is part of prooph/proophessor.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 2/16/16
*/
namespace Prooph\ProophessorDo\Model\Todo\Command;

use Prooph\Common\Messaging\Command;
use Prooph\Common\Messaging\PayloadConstructable;
use Prooph\Common\Messaging\PayloadTrait;
use Prooph\ProophessorDo\Model\Todo\TodoId;

/**
* Class ReopenTodo
*
* @package Prooph\ProophessorDo\Model\Todo
* @author Bas Kamer <[email protected]>
*/
final class ReopenTodo extends Command implements PayloadConstructable
{
use PayloadTrait;

/**
*
* @param TodoId $todoId
* @return ReopenTodo
*/
public static function forTodo($todoId)
{
return new self([
'todo_id' => (string) $todoId,
]);
}

/**
* @return TodoId
*/
public function todoId()
{
return TodoId::fromString($this->payload['todo_id']);
}
}
67 changes: 67 additions & 0 deletions src/Model/Todo/Event/TodoWasReopened.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/*
* This file is part of prooph/proophessor.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 2/16/16
*/
namespace Prooph\ProophessorDo\Model\Todo\Event;

use Prooph\EventSourcing\AggregateChanged;
use Prooph\ProophessorDo\Model\Todo\TodoId;
use Prooph\ProophessorDo\Model\Todo\TodoStatus;

/**
* Class TodoWasReopened
*
* @package Prooph\ProophessorDo\Model\Todo\Event
* @author Bas Kamer <[email protected]>
*/
final class TodoWasReopened extends AggregateChanged
{
private $todoId;

private $status;

/**
* @param TodoId $todoId
* @param TodoStatus $status
* @return TodoWasMarkedAsDone
*/
public static function withStatus(TodoId $todoId, TodoStatus $status)
{
$event = self::occur($todoId->toString(), [
'status' => $status->toString()
]);

$event->todoId = $todoId;
$event->status = $status;

return $event;
}

/**
* @return TodoId
*/
public function todoId()
{
if (null === $this->todoId) {
$this->todoId = TodoId::fromString($this->aggregateId());
}
return $this->todoId;
}

/**
* @return TodoStatus
*/
public function status()
{
if (null === $this->status) {
$this->status = TodoStatus::fromString($this->payload['status']);
}
return $this->status;
}
}
36 changes: 36 additions & 0 deletions src/Model/Todo/Exception/CannotReopenTodo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of prooph/proophessor.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 2/16/16
*/
namespace Prooph\ProophessorDo\Model\Todo\Exception;

use Prooph\ProophessorDo\Model\Todo\Todo;
use Prooph\ProophessorDo\Model\Todo\TodoStatus;

/**
* Class CannotReopenTodo
*
* @package Prooph\ProophessorDo\Model\Todo\Exception
* @author Bas Kamer <[email protected]>
*/
final class CannotReopenTodo extends \RuntimeException
{
/**
* @param TodoStatus $status
* @param Todo $todo
* @return CannotReopenTodo
*/
public static function notMarkedDone(Todo $todo)
{
return new self(sprintf(
'Tried to reopen status of Todo %s. But Todo is not marked as done!',
$todo->todoId()->toString()
));
}
}
31 changes: 31 additions & 0 deletions src/Model/Todo/Exception/TodoNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of prooph/proophessor.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 2/16/16
*/
namespace Prooph\ProophessorDo\Model\Todo\Exception;

use Prooph\ProophessorDo\Model\Todo\TodoId;

/**
* Class TodoNotFound
*
* @package Prooph\ProophessorDo\Model\Todo
* @author Bas Kamer <[email protected]>
*/
final class TodoNotFound extends \InvalidArgumentException
{
/**
* @param TodoId $todoId
* @return TodoNotFound
*/
public static function withTodoId(TodoId $todoId)
{
return new self(sprintf('Todo with id %s cannot be found.', $todoId->toString()));
}
}
4 changes: 4 additions & 0 deletions src/Model/Todo/Handler/MarkTodoAsDoneHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Prooph\ProophessorDo\Model\Todo\Handler;

use Prooph\ProophessorDo\Model\Todo\Command\MarkTodoAsDone;
use Prooph\ProophessorDo\Model\Todo\Exception\TodoNotFound;
use Prooph\ProophessorDo\Model\Todo\TodoList;

/**
Expand Down Expand Up @@ -40,6 +41,9 @@ public function __construct(TodoList $todoList)
public function __invoke(MarkTodoAsDone $command)
{
$todo = $this->todoList->get($command->todoId());
if (! $todo) {
throw TodoNotFound::withTodoId($command->todoId());
}

$todo->markAsDone();
}
Expand Down
50 changes: 50 additions & 0 deletions src/Model/Todo/Handler/ReopenTodoHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* This file is part of prooph/proophessor.
* (c) 2014-2015 prooph software GmbH <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Date: 2/16/16
*/
namespace Prooph\ProophessorDo\Model\Todo\Handler;

use Prooph\ProophessorDo\Model\Todo\Command\ReopenTodo;
use Prooph\ProophessorDo\Model\Todo\Exception\TodoNotFound;
use Prooph\ProophessorDo\Model\Todo\TodoList;

/**
* Class ReopenTodoHandler
*
* @package Prooph\ProophessorDo\Model\Todo
* @author Bas Kamer <[email protected]>
*/
final class ReopenTodoHandler
{
/**
* @var TodoList
*/
private $todoList;

/**
* @param TodoList $todoList
*/
public function __construct(TodoList $todoList)
{
$this->todoList = $todoList;
}

/**
* @param ReopenTodo $command
*/
public function __invoke(ReopenTodo $command)
{
$todo = $this->todoList->get($command->todoId());
Copy link
Member

Choose a reason for hiding this comment

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

check if todo was found, elso throw TodoNotFound

if (! $todo) {
throw TodoNotFound::withTodoId($command->todoId());
}

$todo->reopenTodo();
}
}
18 changes: 18 additions & 0 deletions src/Model/Todo/Todo.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
namespace Prooph\ProophessorDo\Model\Todo;

use Prooph\ProophessorDo\Model\Todo\Event\TodoWasReopened;
use Prooph\ProophessorDo\Model\User\UserId;
use Assert\Assertion;
use Prooph\EventSourcing\AggregateRoot;
Expand Down Expand Up @@ -100,6 +101,15 @@ public function addDeadline(UserId $userId, TodoDeadline $deadline)
$this->recordThat(DeadlineWasAddedToTodo::byUserToDate($this->todoId, $this->assigneeId, $deadline));
}

public function reopenTodo()
{
if (!$this->status->isDone()) {
throw Exception\CannotReopenTodo::notMarkedDone($this);
}

$this->recordThat(TodoWasReopened::withStatus($this->todoId, TodoStatus::fromString(TodoStatus::OPEN)));
}

/**
* @return \DateTimeImmutable
*/
Expand Down Expand Up @@ -159,6 +169,14 @@ protected function whenTodoWasMarkedAsDone(TodoWasMarkedAsDone $event)
$this->status = $event->newStatus();
}

/**
* @param TodoWasReopened $event
*/
protected function whenTodoWasReopened(TodoWasReopened $event)
{
$this->status = $event->status();
}

/**
* @param DeadlineWasAddedToTodo $event
* @return void
Expand Down
Loading