Skip to content

Commit 503230c

Browse files
committed
#157 Adding tests and refactoring code
1 parent 7df4c98 commit 503230c

21 files changed

+40
-61
lines changed

api/src/Markdown/FetcherInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
interface FetcherInterface
@@ -8,4 +9,4 @@ interface FetcherInterface
89
* @return string[]
910
*/
1011
public function fetch(string $source): array;
11-
}
12+
}

api/src/Markdown/GeneratorInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
interface GeneratorInterface
@@ -14,4 +15,4 @@ public function generate(string $source): array;
1415
* @return array
1516
*/
1617
public function process(array $filePaths): array;
17-
}
18+
}

api/src/Markdown/Model/ModelInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ interface ModelInterface
66
{
77
public function getId(): int;
88
public function getFilePath(): string;
9-
}
9+
}

api/src/Markdown/Model/Question.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
class Question implements ModelInterface
66
{
7-
87
public function __construct(
9-
private readonly int $id,
8+
private readonly int $id,
109
private readonly int $quizID,
1110
private readonly string $filePath,
1211
private readonly string $title,
1312
private readonly array $content,
1413
private readonly array $possibleAnswers,
15-
private readonly array $correctAnswer)
16-
{
14+
private readonly array $correctAnswer
15+
) {
1716
}
1817

1918
/**
@@ -72,6 +71,4 @@ public function getQuizID(): int
7271
{
7372
return $this->quizID;
7473
}
75-
76-
77-
}
74+
}

api/src/Markdown/Model/Quiz.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
class Quiz
66
{
7-
87
public function __construct(
9-
private readonly int $id,
8+
private readonly int $id,
109
private readonly string $name,
1110
private readonly string $filePath
12-
)
13-
{
11+
) {
1412
}
1513

1614
/**
@@ -36,6 +34,4 @@ public function getFilePath()
3634
{
3735
return $this->filePath;
3836
}
39-
40-
41-
}
37+
}

api/src/Markdown/Parser/DocumentExtractor.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class DocumentExtractor
1616

1717
public function __construct(private readonly string $document)
1818
{
19-
2019
}
2120

2221
/**
@@ -25,12 +24,11 @@ public function __construct(private readonly string $document)
2524
public function extract(): void
2625
{
2726
$domDocument = new DOMDocument();
28-
libxml_use_internal_errors(TRUE);
27+
libxml_use_internal_errors(true);
2928
$domDocument->loadHTML($this->document);
3029
libxml_get_errors();
3130

3231
$this->process($domDocument);
33-
3432
}
3533

3634
public function process(DOMNode $domNode): void
@@ -50,7 +48,6 @@ public function process(DOMNode $domNode): void
5048

5149
// Get the question
5250
if (!$this->foundPossibleAnswers && !$this->foundCorrectAnswer) {
53-
5451
if ($node->nodeName !== 'body' && $node->nodeName !== 'html') {
5552
$this->question[] = $node;
5653
}
@@ -87,4 +84,4 @@ public function getCorrectAnswerNodes(): array
8784
{
8885
return $this->correctAnswer;
8986
}
90-
}
87+
}

api/src/Markdown/QuestionFetcher.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
use Symfony\Component\Finder\Finder;
@@ -18,12 +19,10 @@ public function fetch(string $source): array
1819
$filenames = [];
1920
$finder = new Finder();
2021
$files = $finder->files()->in($folderPath)->notName('index.md');
21-
foreach($files as $file){
22+
foreach ($files as $file) {
2223
$filenames[] = $file->getFilename();
2324
}
2425

2526
return $filenames;
2627
}
27-
28-
29-
}
28+
}

api/src/Markdown/QuestionGenerator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Markdown\Model\Question;
66

7-
87
class QuestionGenerator implements GeneratorInterface
98
{
109
public function __construct(private readonly FetcherInterface $fetcher)
@@ -75,4 +74,4 @@ public function process(array $filePaths): array
7574
}
7675
return $dataSets;
7776
}
78-
}
77+
}

api/src/Markdown/QuizFetcher.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
class QuizFetcher implements FetcherInterface
@@ -10,18 +11,16 @@ class QuizFetcher implements FetcherInterface
1011
public function fetch(string $source): array
1112
{
1213
$fullPath = dirname(__DIR__) . '/..' . $source;
13-
$directories = glob($fullPath . '/*' , GLOB_ONLYDIR);
14-
if(!$directories){
14+
$directories = glob($fullPath . '/*', GLOB_ONLYDIR);
15+
if (!$directories) {
1516
return [];
1617
}
1718

1819
$data = [];
19-
foreach($directories as $directory){
20-
$data[] = str_replace('/var/www/html/src/..', '', $directory);
20+
foreach ($directories as $directory) {
21+
$data[] = str_replace('/var/www/html/src/..', '', $directory);
2122
}
2223

2324
return $data;
2425
}
25-
26-
27-
}
26+
}

api/src/Markdown/QuizGenerator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
2+
23
namespace App\Markdown;
34

45
use App\Markdown\Model\Quiz;
56

67
class QuizGenerator implements GeneratorInterface
78
{
89
public function __construct(private readonly FetcherInterface $fetcher)
9-
{ }
10+
{
11+
}
1012

1113
/**
1214
* @param string $source
@@ -34,7 +36,7 @@ public function generateNameFromFilePath(string $filePath): string
3436
$directoryLeaf = basename($filePath);
3537
$name = str_replace('_', ' ', $directoryLeaf);
3638
$firstSpace = strstr($name, ' ');
37-
if($firstSpace) {
39+
if ($firstSpace) {
3840
$name = ltrim($firstSpace, ' ');
3941
}
4042
return ucfirst($name);
@@ -56,4 +58,4 @@ public function process(array $filePaths): array
5658
}
5759
return $dataSets;
5860
}
59-
}
61+
}

0 commit comments

Comments
 (0)