Skip to content

Commit

Permalink
Merge 79fb54c into 0f3fd05
Browse files Browse the repository at this point in the history
  • Loading branch information
stejes committed Jun 26, 2018
2 parents 0f3fd05 + 79fb54c commit f78eff6
Show file tree
Hide file tree
Showing 30 changed files with 183 additions and 55 deletions.
15 changes: 10 additions & 5 deletions src/Question/Controllers/QuestionViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function index(): Response
return $this->render(
'questions/index.html.twig',
[
'questions' => $questions ? $questions->toArray() : [],
'questions' => $questions ? $questions->sortByNewest()->toArray() : [],
]
);
}
Expand All @@ -104,18 +104,20 @@ public function print(Request $request): Response
$questions = $this->questionRepository->getAll();

if ($questions) {
$questions = $questions->sortByNewest();

$languageFilter = $this->getLanguageFilter($request);

if ($languageFilter) {
$questions = $this->filterQuestions($questions, $languageFilter);
$questionsArray = $this->filterQuestions($questions, $languageFilter);
} else {
$questions = $questions->toArray();
$questionsArray = $questions->toArray();
}

return $this->render(
'questions/print.html.twig',
[
'questions' => $questions ? $questions : [],
'questions' => $questionsArray,
]
);
}
Expand All @@ -128,6 +130,7 @@ public function print(Request $request): Response
* @param Request $request
* @return Response
* @throws \League\Flysystem\FileExistsException
* @throws \Exception
*/
public function add(Request $request): Response
{
Expand Down Expand Up @@ -162,6 +165,7 @@ public function add(Request $request): Response
* @param Request $request
* @param string $id
* @return Response
* @throws \Exception
*/
public function edit(Request $request, string $id): Response
{
Expand Down Expand Up @@ -362,7 +366,8 @@ public function updateQuestionImage(
$question->getText(),
$imageFileName,
$question->getAnswers(),
$question->getFeedback()
$question->getFeedback(),
$question->getCreatedOn()
);
}
}
8 changes: 6 additions & 2 deletions src/Question/Forms/QuestionFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function configureOptions(OptionsResolver $resolver)
* @param NotEmptyString $imageFileName
* @param array $data
* @return Question
* @throws \Exception
*/
public function newQuestionFromData(
UuidFactoryInterface $uuidFactory,
Expand Down Expand Up @@ -232,7 +233,8 @@ public function newQuestionFromData(
new NotEmptyString($data['text']),
$imageFileName,
new Answers(...$answers),
new NotEmptyString($data['feedback'])
new NotEmptyString($data['feedback']),
new \DateTimeImmutable('now')
);
}

Expand All @@ -241,6 +243,7 @@ public function newQuestionFromData(
* @param Question $question
* @param array $data
* @return Question
* @throws \Exception
*/
public function updateQuestionFromData(
UuidFactoryInterface $uuidFactory,
Expand Down Expand Up @@ -286,7 +289,8 @@ public function updateQuestionFromData(
new NotEmptyString($data['text']),
$question->getImageFileName(),
new Answers(...$answers),
new NotEmptyString($data['feedback'])
new NotEmptyString($data['feedback']),
$question->getCreatedOn()
);
}

Expand Down
18 changes: 17 additions & 1 deletion src/Question/Models/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class Question
*/
private $feedback;

/**
* @var \DateTimeImmutable
*/
private $createdOn;

/**
* @var \DateTimeImmutable
*/
Expand All @@ -63,6 +68,7 @@ class Question
* @param NotEmptyString $imageFileName
* @param Answers $answers
* @param NotEmptyString $feedback
* @param \DateTimeImmutable $createdOn
*/
public function __construct(
UuidInterface $id,
Expand All @@ -72,7 +78,8 @@ public function __construct(
NotEmptyString $text,
NotEmptyString $imageFileName,
Answers $answers,
NotEmptyString $feedback
NotEmptyString $feedback,
\DateTimeImmutable $createdOn
) {
$this->guardAnswers($answers);

Expand All @@ -84,6 +91,7 @@ public function __construct(
$this->imageFileName = $imageFileName;
$this->answers = $answers;
$this->feedback = $feedback;
$this->createdOn = $createdOn;
}

/**
Expand Down Expand Up @@ -150,6 +158,14 @@ public function getFeedback(): NotEmptyString
return $this->feedback;
}

/**
* @return \DateTimeImmutable
*/
public function getCreatedOn(): \DateTimeImmutable
{
return $this->createdOn;
}

/**
* @param \DateTimeImmutable $archiveOn
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Question/Models/Questions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,24 @@ public function toArray(): array
{
return $this->questions;
}

/**
* Short questions based on the creation date.
* The newest question will have index 0.
*/
public function sortByNewest(): Questions
{
usort(
$this->questions,
function (Question $q1, Question $q2) {
if ($q1->getCreatedOn() === $q2->getCreatedOn()) {
return 0;
}

return $q1->getCreatedOn() > $q2->getCreatedOn() ? -1 : 1;
}
);

return $this;
}
}
26 changes: 23 additions & 3 deletions src/Question/Repositories/Entities/QuestionEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ class QuestionEntity extends Entity
*/
private $feedback;

/**
* @var \DateTimeImmutable
*
* @ORM\Column(type="datetime_immutable", name="created_on", nullable=false)
*/
private $createdOn;

/**
* @param string $id
* @param string $language
Expand All @@ -79,6 +86,7 @@ class QuestionEntity extends Entity
* @param string $imageFileName
* @param Collection $answerEntities
* @param string $feedback
* @param \DateTimeImmutable $createdOn
*/
private function __construct(
string $id,
Expand All @@ -88,7 +96,8 @@ private function __construct(
string $text,
string $imageFileName,
Collection $answerEntities,
string $feedback
string $feedback,
\DateTimeImmutable $createdOn
) {
parent::__construct($id);

Expand All @@ -99,6 +108,7 @@ private function __construct(
$this->imageFileName = $imageFileName;
$this->answerEntities = $answerEntities;
$this->feedback = $feedback;
$this->createdOn = $createdOn;

foreach ($answerEntities as $answerEntity) {
$answerEntity->setQuestionEntity($this);
Expand Down Expand Up @@ -127,7 +137,8 @@ function (Answer $answer) {
$question->getText()->toNative(),
$question->getImageFileName()->toNative(),
new ArrayCollection($answerEntities),
$question->getFeedback()->toNative()
$question->getFeedback()->toNative(),
$question->getCreatedOn()
);

return $questionEntity;
Expand Down Expand Up @@ -155,7 +166,8 @@ function (AnswerEntity $answerEntity) {
new NotEmptyString($this->getText()),
new NotEmptyString($this->getImageFileName()),
$answers,
new NotEmptyString($this->getFeedback())
new NotEmptyString($this->getFeedback()),
$this->getCreatedOn()
);
}

Expand Down Expand Up @@ -214,4 +226,12 @@ public function getFeedback(): string
{
return $this->feedback;
}

/**
* @return \DateTimeImmutable
*/
public function getCreatedOn(): \DateTimeImmutable
{
return $this->createdOn;
}
}
4 changes: 3 additions & 1 deletion src/Question/Serializers/QuestionDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(

/**
* @inheritdoc
* @throws \Exception
*/
public function denormalize($data, $class, $format = null, array $context = []): Question
{
Expand Down Expand Up @@ -69,7 +70,8 @@ function (array $answer) use ($format, $context) {
new NotEmptyString($data['text']),
new NotEmptyString($data['imageFileName']),
new Answers(...$answers),
new NotEmptyString($data['feedback'])
new NotEmptyString($data['feedback']),
new \DateTimeImmutable($data['createdOn'])
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Question/Serializers/QuestionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function (Answer $answer) use ($format) {
'imageFileName' => $question->getImageFileName()->toNative(),
'answers' => $answers,
'feedback' => $question->getFeedback()->toNative(),
'createdOn' => $question->getCreatedOn()->format(DATE_ATOM)
];
}

Expand Down
4 changes: 4 additions & 0 deletions templates/questions/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<th data-sortable="true">{% trans %}Jaar{% endtrans %}</th>
<th data-sortable="true">{% trans %}Taal{% endtrans %}</th>
<th data-sortable="true">{% trans %}Thema{% endtrans %}</th>
<th data-sortable="true">{% trans %}CreatedOn{% endtrans %}</th>
<th data-searchable="false">{% trans %}Acties{% endtrans %}</th>
</tr>
</thead>
Expand Down Expand Up @@ -85,6 +86,9 @@
<td>
{{ question.category.name.toNative }}
</td>
<td>
{{ question.createdOn|date('d-m-Y H:i:s', 'Europe/Brussels') }}
</td>
<td>
<a role="button"
class="btn btn-light"
Expand Down
3 changes: 0 additions & 3 deletions tests/Account/Controllers/AccountControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ class AccountControllerTest extends TestCase
*/
private $userAccountController;

/**
* @throws \ReflectionException
*/
protected function setUp(): void
{
/** @var UserRepository|MockObject $userRepository */
Expand Down
3 changes: 0 additions & 3 deletions tests/Account/Serializers/RegistrationJsonEnricherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class RegistrationJsonEnricherTest extends TestCase
*/
private $registrationEnricher;

/**
* @throws \ReflectionException
*/
public function setUp(): void
{
/** @var UuidFactoryInterface|MockObject $uuidFactory */
Expand Down
3 changes: 0 additions & 3 deletions tests/Command/SeedCategoriesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class SeedCategoriesCommandTest extends TestCase
*/
private $seedCategoriesCommand;

/**
* @throws \ReflectionException
*/
protected function setUp(): void
{
/** @var CategoryRepository|MockObject $categoriesRepository */
Expand Down
3 changes: 0 additions & 3 deletions tests/Company/Controllers/CompanyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class CompanyControllerTest extends TestCase
*/
private $companyController;

/**
* @throws \ReflectionException
*/
protected function setUp(): void
{
/** @var CompanyRepository|MockObject $companyRepository */
Expand Down
3 changes: 0 additions & 3 deletions tests/Company/Serializers/CompanyJsonEnricherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ class CompanyJsonEnricherTest extends TestCase
*/
private $companyJsonEnricher;

/**
* @throws \ReflectionException
*/
protected function setUp(): void
{
/** @var UuidFactoryInterface|MockObject $uuidFactory */
Expand Down

0 comments on commit f78eff6

Please sign in to comment.