/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $userId = $request->getAttribute('user_id'); $user = $this->userFinder->findById($userId); $todos = $this->todoFinder->findByAssigneeId($userId); return new HtmlResponse($this->templates->render('page::user-todo-list', ['user' => $user, 'todos' => $todos])); }
/** * @test * @dataProvider objectsData * @param \stdClass $todo * @param \stdClass $user */ public function it_sends_email_to_the_assignee(\stdClass $todo, \stdClass $user) { $this->todoFinder->expects($this->once())->method('findById')->with($todo->id)->will($this->returnValue($todo)); $this->userFinder->expects($this->once())->method('findById')->with($user->id)->will($this->returnValue($user)); $this->mailer->expects($this->once())->method('send')->with($this->isInstanceOf(Message::class)); $action = $this->action; $action($this->getEvent($todo->id)); }
/** * @param TodoWasMarkedAsDone $event * @throws \RuntimeException if data of the the assigned user can not be found */ public function onTodoWasMarkedAsDone(TodoWasMarkedAsDone $event) { $user = $this->userFinder->findUserOfTodo($event->todoId()->toString()); if (!$user) { throw new \RuntimeException(sprintf("Data of the assigned user of the todo %s cannot be found", $event->todoId()->toString())); } $stmt = $this->connection->prepare(sprintf('UPDATE %s SET open_todos = open_todos - 1, done_todos = done_todos + 1 WHERE id = :assignee_id', Table::USER)); $stmt->bindValue('assignee_id', $user->id); $stmt->execute(); }
/** * @param TodoAssigneeWasReminded $event */ public function __invoke(TodoAssigneeWasReminded $event) { $user = $this->userFinder->findById($event->userId()->toString()); $todo = $this->todoFinder->findById($event->todoId()->toString()); $mail = new Mail\Message(); $mail->setBody("Hello {$user->name}. This a reminder for '{$todo->text}'. Don't be lazy!"); $mail->setFrom('*****@*****.**', 'Proophessor-do'); $mail->addTo($user->email, $user->name); $mail->setSubject('Proophessor-do Todo Reminder'); $this->mailer->send($mail); }
/** * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return HtmlResponse */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $userId = $request->getAttribute('user_id'); $invalidUser = true; $user = null; if ($userId) { $user = $this->userFinder->findById($userId); if ($user) { $invalidUser = false; } } return new HtmlResponse($this->templates->render('page::user-todo-form', ['invalidUser' => $invalidUser, 'user' => $user])); }
/** * @param TodoWasMarkedAsExpired $event * @return void */ public function __invoke(TodoWasMarkedAsExpired $event) { $todo = $this->todoFinder->findById($event->todoId()->toString()); $user = $this->userFinder->findById($todo->assignee_id); $message = sprintf('Hi %s! Just a heads up: your todo `%s` has expired on %s.', $user->name, $todo->text, $todo->deadline); $mail = new Mail\Message(); $mail->setBody($message); $mail->setEncoding('utf-8'); $mail->setFrom('*****@*****.**', 'Proophessor-do'); $mail->addTo($user->email, $user->name); $mail->setSubject('Proophessor-do Todo expired'); $this->mailer->send($mail); }
/** * @param TodoWasUnmarkedAsExpired $event * @throws UserNotFound if data of the the assigned user can not be found */ public function onTodoWasUnmarkedAsExpired(TodoWasUnmarkedAsExpired $event) { $user = $this->userFinder->findUserOfTodo($event->todoId()->toString()); if (!$user) { throw new UserNotFound(sprintf("Data of the assigned user of the todo %s cannot be found", $event->todoId()->toString())); } $stmt = $this->connection->prepare(sprintf('UPDATE %s SET open_todos = open_todos + 1, expired_todos = expired_todos - 1 WHERE id = :assignee_id', Table::USER)); $stmt->bindValue('assignee_id', $user->id); $stmt->execute(); }
/** * @param RequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface */ public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) { $users = $this->userFinder->findAll(); return new HtmlResponse($this->templates->render('page::user-list', ['users' => $users])); }