Skip to content

Commit 5cd9b62

Browse files
Merge pull request #4 from DeGraciaMathieu/refacto/clean-archi
Refactoring into clean architecture
2 parents b85224c + e51b2d5 commit 5cd9b62

37 files changed

Lines changed: 7104 additions & 232 deletions
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Application\UseCases;
4+
5+
use Throwable;
6+
use App\Domain\Ports\FileService;
7+
use App\Domain\Ports\AnalyzeService;
8+
use App\Application\UseCases\InspectInput;
9+
use App\Application\UseCases\InspectOutput;
10+
11+
class InspectHandler
12+
{
13+
public function __construct(
14+
private FileService $fileService,
15+
private AnalyzeService $analyzeService,
16+
) {
17+
}
18+
19+
public function handle(
20+
InspectInput $input,
21+
InspectOutput $output,
22+
): void {
23+
24+
try {
25+
26+
$output->hello();
27+
28+
$fileAggregator = $this->fileService->all(
29+
path: $input->path(),
30+
);
31+
32+
$wordsAggregator = $this->analyzeService->getWords(
33+
fileAggregator: $fileAggregator,
34+
withMethod: $input->withMethod(),
35+
);
36+
37+
$output->present($wordsAggregator);
38+
39+
} catch (Throwable $th) {
40+
$output->error($th);
41+
}
42+
}
43+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Application\UseCases;
4+
5+
use App\Domain\ValueObjects\Path;
6+
7+
interface InspectInput
8+
{
9+
public function path(): Path;
10+
public function withMethod(): bool;
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Application\UseCases;
4+
5+
use Throwable;
6+
use App\Domain\Aggregators\WordAggregator;
7+
8+
interface InspectOutput
9+
{
10+
public function hello();
11+
public function present(WordAggregator $wordAggregator);
12+
public function error(Throwable $th);
13+
}

app/Bags/WordsBag.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

app/Commands/Inspect.php

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Domain\Aggregators;
4+
5+
use App\Domain\Entities\FileEntity;
6+
7+
class FileAggregator
8+
{
9+
private array $files = [];
10+
11+
public function add(FileEntity $fileEntity): void
12+
{
13+
$this->files[] = $fileEntity;
14+
}
15+
16+
public function files(): array
17+
{
18+
return $this->files;
19+
}
20+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Domain\Aggregators;
4+
5+
use App\Domain\Entities\WordEntity;
6+
use App\Domain\Services\WordSorter;
7+
8+
class WordAggregator
9+
{
10+
private array $words = [];
11+
12+
public function add(WordEntity $wordEntity): void
13+
{
14+
if ($wordEntity->canBeIgnored()) {
15+
return;
16+
}
17+
18+
$this->increment($wordEntity);
19+
}
20+
21+
public function sort(): void
22+
{
23+
uasort($this->words, function ($a, $b) {
24+
return $this->sortWordsByDesc($a, $b);
25+
});
26+
}
27+
28+
public function words(): array
29+
{
30+
return $this->words;
31+
}
32+
33+
private function increment(WordEntity $wordEntity): void
34+
{
35+
$word = $wordEntity->value();
36+
37+
/**
38+
* Wow, it works well, don't judge me
39+
*/
40+
@$this->words[$word]++;
41+
}
42+
43+
private function sortWordsByDesc($a, $b): int
44+
{
45+
if ($a == $b) {
46+
return 0;
47+
}
48+
49+
return ($a < $b) ? 1 : -1;
50+
}
51+
}

app/Domain/Entities/FileEntity.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Domain\Entities;
4+
5+
use App\Domain\ValueObjects\Path;
6+
7+
class FileEntity
8+
{
9+
public function __construct(
10+
public Path $fullPath,
11+
public Path $displayPath,
12+
public string $contents,
13+
) {}
14+
15+
public static function from(array $attributes): self
16+
{
17+
return new self(
18+
fullPath: Path::from($attributes['fullPath']),
19+
displayPath: Path::from($attributes['displayPath']),
20+
contents: $attributes['contents'],
21+
);
22+
}
23+
}

app/Domain/Entities/WordEntity.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Domain\Entities;
4+
5+
class WordEntity
6+
{
7+
public function __construct(
8+
private string $value,
9+
) {}
10+
11+
public static function from(string $word): self
12+
{
13+
$word = strtolower($word);
14+
15+
return new self($word);
16+
}
17+
18+
public function canBeIgnored(): bool
19+
{
20+
return $this->value === 'this';
21+
}
22+
23+
public function value(): string
24+
{
25+
return $this->value;
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Domain\Ports;
4+
5+
use App\Domain\Aggregators\FileAggregator;
6+
use App\Domain\Aggregators\WordAggregator;
7+
8+
interface AnalyzeService
9+
{
10+
/**
11+
* Retrieve all words from a set of files.
12+
*
13+
* withMethod argument allows including the words present in the function signatures.
14+
*/
15+
public function getWords(FileAggregator $fileAggregator, bool $withMethod): WordAggregator;
16+
}

0 commit comments

Comments
 (0)