Skip to content

Commit

Permalink
Merge 2f1f66e into 22292bf
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc Wollants committed Aug 6, 2018
2 parents 22292bf + 2f1f66e commit 8dfec94
Show file tree
Hide file tree
Showing 58 changed files with 3,535 additions and 45 deletions.
2 changes: 1 addition & 1 deletion bluegreen/docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ services:
redis:
image: redis:alpine
ports:
- 63799:6379
- ${DOCKER_HOST_IP}::6379
restart: always
17 changes: 16 additions & 1 deletion config/routes.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#
# VIEWS
#

accounts_view_register:
path: /{_locale}/view/accounts/register
controller: VSV\GVQ_API\Account\Controllers\AccountViewController::register
Expand Down Expand Up @@ -137,3 +136,19 @@ users_export:
documents_kickoff:
path: /documents/{document}
methods: [GET]

#
# API
#
quiz_start:
path: /quiz
controller: VSV\GVQ_API\Quiz\Controllers\QuizController::start
methods: [POST]
quiz_ask_question:
path: /quiz/{quizId}/question
controller: VSV\GVQ_API\Quiz\Controllers\QuizController::askQuestion
methods: [GET]
quiz_answer_question:
path: /quiz/{quizId}/question/{answerId}
controller: VSV\GVQ_API\Quiz\Controllers\QuizController::answerQuestion
methods: [POST]
93 changes: 93 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ services:
arguments:
- 'Europe/Brussels'

quiz_delay:
class: VSV\GVQ_API\Quiz\ValueObjects\AllowedDelay
arguments:
$value: 40

quiz_year:
class: \VSV\GVQ_API\Question\ValueObjects\Year
arguments:
$value: 2018

quiz_start_date:
class: \DateTimeImmutable
arguments:
Expand All @@ -98,6 +108,80 @@ services:
arguments:
$partnerFile: '%kernel.project_dir%/config/partners.yaml'

redis_service:
class: \Redis
calls:
- method: connect
arguments:
- 'redis'
- 6379

quiz_redis_repository:
class: VSV\GVQ_API\Quiz\Repositories\QuizRedisRepository
arguments:
$redis: '@redis_service'

started_quiz_redis_repository:
class: VSV\GVQ_API\Quiz\Repositories\StartedQuizRedisRepository
arguments:
$redis: '@redis_service'

current_question_redis_repository:
class: VSV\GVQ_API\Quiz\Repositories\CurrentQuestionRedisRepository
arguments:
$redis: '@redis_service'

quiz_started_listener:
class: VSV\GVQ_API\Quiz\EventListeners\QuizStartedListener
arguments:
$quizRepository: '@quiz_redis_repository'
$startedQuizRepository: '@started_quiz_redis_repository'

question_asked_listener:
class: VSV\GVQ_API\Quiz\EventListeners\QuestionAskedListener
arguments:
$currentQuestionRepository: '@current_question_redis_repository'

in_memory_event_store:
class: Broadway\EventStore\InMemoryEventStore

doctrine_event_store:
class: VSV\GVQ_API\Quiz\EventStore\DoctrineEventStore

simple_event_bus:
class: Broadway\EventHandling\SimpleEventBus
calls:
- method: subscribe
arguments:
- '@quiz_started_listener'
- method: subscribe
arguments:
- '@question_asked_listener'

quiz_aggregate_repository:
class: VSV\GVQ_API\Quiz\Repositories\QuizAggregateRepository
arguments:
$eventStore: '@doctrine_event_store'
$eventBus: '@simple_event_bus'

quiz_composition_repository:
class: VSV\GVQ_API\Quiz\Repositories\QuizCompositionYamlRepository
arguments:
$quizCompositionFile: '%kernel.project_dir%/config/quiz_composition.yaml'

team_repository:
class: VSV\GVQ_API\Team\Repositories\TeamYamlRepository
arguments:
$teamFile: '%kernel.project_dir%/config/teams.yaml'

quiz_service:
class: VSV\GVQ_API\Quiz\Service\QuizService
arguments:
$uuidFactory: '@uuid_factory'
$quizCompositionRepository: '@quiz_composition_repository'
$year: '@quiz_year'
$allowedDelay: '@quiz_delay'

### CONTROLLERS ###
VSV\GVQ_API\Account\Controllers\AccountController:
arguments:
Expand Down Expand Up @@ -145,3 +229,12 @@ services:
$imageValidator: '@image_validator'
$uuidFactory: '@uuid_factory'
tags: ['controller.service_arguments']

VSV\GVQ_API\Quiz\Controllers\QuizController:
arguments:
$quizService: '@quiz_service'
$quizAggregateRepository: '@quiz_aggregate_repository'
$partnerRepository: '@partner_yaml_repository'
$teamRepository: '@team_repository'
$currentQuestionRepository: '@current_question_redis_repository'
tags: ['controller.service_arguments']
5 changes: 3 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
<directory suffix=".php">./src/*</directory>
<exclude>
<file>./src/Kernel.php</file>
<file>./src/Account/Controllers/AccountViewController.php</file>
<file>./src/Account/Forms/RegistrationFormType.php</file>
<file>./src/Command/RedisCommand.php</file>
<file>./src/Company/Controllers/CompanyViewController.php</file>
<file>./src/Company/Forms/CompanyFormType.php</file>
<file>./src/Question/Controllers/QuestionViewController.php</file>
<file>./src/Question/Forms/QuestionFormType.php</file>
<file>./src/Quiz/Controllers/QuizController.php</file>
<file>./src/User/Controllers/UserViewController.php</file>
<file>./src/User/Forms/UserFormType.php</file>
<file>./src/Account/Controllers/AccountViewController.php</file>
<file>./src/Account/Forms/RegistrationFormType.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
34 changes: 34 additions & 0 deletions src/Question/Repositories/AnswerDoctrineRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Question\Repositories;

use Ramsey\Uuid\UuidInterface;
use VSV\GVQ_API\Common\Repositories\AbstractDoctrineRepository;
use VSV\GVQ_API\Question\Models\Answer;
use VSV\GVQ_API\Question\Repositories\Entities\AnswerEntity;

class AnswerDoctrineRepository extends AbstractDoctrineRepository implements AnswerRepository
{
/**
* @inheritdoc
*/
protected function getRepositoryName(): string
{
return AnswerEntity::class;
}

/**
* @inheritdoc
*/
public function getById(UuidInterface $id): ?Answer
{
/** @var AnswerEntity|null $answerEntity */
$answerEntity = $this->objectRepository->findOneBy(
[
'id' => $id,
]
);

return $answerEntity ? $answerEntity->toAnswer() : null;
}
}
15 changes: 15 additions & 0 deletions src/Question/Repositories/AnswerRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace VSV\GVQ_API\Question\Repositories;

use Ramsey\Uuid\UuidInterface;
use VSV\GVQ_API\Question\Models\Answer;

interface AnswerRepository
{
/**
* @param UuidInterface $id
* @return null|Answer
*/
public function getById(UuidInterface $id): ?Answer;
}
86 changes: 50 additions & 36 deletions src/Quiz/Aggregate/QuizAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class QuizAggregate extends EventSourcedAggregateRoot
*/
private $questionAskedOn;

/**
* @var bool
*/
private $askingQuestion;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -60,20 +65,23 @@ protected function applyQuizStarted(QuizStarted $quizStarted): void
$this->quiz = $quizStarted->getQuiz();

$this->questionIndex = 0;
$this->askingQuestion = false;
}

/**
* @param \DateTimeImmutable $askedOn
*/
public function askQuestion(\DateTimeImmutable $askedOn): void
{
$this->apply(
new QuestionAsked(
$this->quiz->getId(),
$this->getCurrentQuestion(),
$askedOn
)
);
if (!$this->askingQuestion) {
$this->apply(
new QuestionAsked(
$this->quiz->getId(),
$this->getCurrentQuestion(),
$askedOn
)
);
}
}

/**
Expand All @@ -82,6 +90,7 @@ public function askQuestion(\DateTimeImmutable $askedOn): void
protected function applyQuestionAsked(QuestionAsked $questionAsked): void
{
$this->questionAskedOn = $questionAsked->getAskedOn();
$this->askingQuestion = true;
}

/**
Expand All @@ -92,51 +101,56 @@ public function answerQuestion(
\DateTimeImmutable $answeredOn,
Answer $answer
): void {
$currentQuestion = $this->getCurrentQuestion();

if ($this->answeredToLate($this->questionAskedOn, $answeredOn, $this->quiz->getAllowedDelay()) ||
!$this->answeredCorrect($currentQuestion->getAnswers(), $answer)) {
$this->apply(
new AnsweredIncorrect(
$this->quiz->getId(),
$currentQuestion,
$answer,
$answeredOn
)
);
} else {
$this->apply(
new AnsweredCorrect(
$this->quiz->getId(),
$currentQuestion,
$answer,
$answeredOn
)
);
}

if (count($this->quiz->getQuestions()) === $this->questionIndex) {
$this->apply(
new QuizFinished(
$this->quiz->getId()
)
);
if ($this->askingQuestion) {
$currentQuestion = $this->getCurrentQuestion();

if ($this->answeredToLate($this->questionAskedOn, $answeredOn, $this->quiz->getAllowedDelay()) ||
!$this->answeredCorrect($currentQuestion->getAnswers(), $answer)) {
$this->apply(
new AnsweredIncorrect(
$this->quiz->getId(),
$currentQuestion,
$answer,
$answeredOn
)
);
} else {
$this->apply(
new AnsweredCorrect(
$this->quiz->getId(),
$currentQuestion,
$answer,
$answeredOn
)
);
}

if (count($this->quiz->getQuestions()) === $this->questionIndex) {
$this->apply(
new QuizFinished(
$this->quiz->getId()
)
);
}
}
}

protected function applyAnsweredIncorrect(): void
{
$this->questionIndex++;
$this->askingQuestion = false;
}

protected function applyAnsweredCorrect(): void
{
$this->questionIndex++;
$this->askingQuestion = false;
}

protected function applyQuizFinished(): void
{
$this->questionIndex = -1;
$this->askingQuestion = false;
}

/**
Expand Down
Loading

0 comments on commit 8dfec94

Please sign in to comment.